parfor for movie writeVideo

21 views (last 30 days)
student student
student student on 21 Feb 2022
Commented: student student on 22 Feb 2022
Dear Mathlabers,
I would like to use parfor loops to write movie, using a code like this:
---------------------------------------------------------------------------
% Code:
for AmFrame=1:length(imgBinRS) % loop over amount of images
frame = im2frame(imgBinRS{1,AmFrame}.cdata);% load into frames the images
for v=1:DurStim(1,AmFrame) %loop over the duration of each images (different for different images)
writeVideo(StimVid, frame);%write the images into the movie
end
end
% END
---------------------------------------------------------------------------
Ideally, I would like to use the parfor on the first for: parfor (AmFrame=1:length(imgBinRS),20)
And when I do, I get this error message:
"ror using VideoWriter/writeVideo (line 359)
OBJ must be open before writing video. Call open(obj) before calling writeVideo."
Would anyone have a good idea to use parfor with writeVideo ?
Many thanks in advance
Best
  2 Comments
Catalytic
Catalytic on 21 Feb 2022
Did you do as the error message instructed? I don't see anywhere in your code where you actually create (and open) a videoWriter object.
student student
student student on 22 Feb 2022
many thanks ! I did open right before the code like this:
StimVid=VideoWriter(strcat([pathL,num2str(amImage),'.avi']));open(StimVid);
I tried again with the help of the next comment, thanks again !

Sign in to comment.

Answers (1)

Edric Ellis
Edric Ellis on 22 Feb 2022
The problem is that you're trying to send a VideoWriter object to the workers, and that's not allowed. A secondary problem is that the iterations of the parfor loop are not necessarily executed in order - so even if you could write to the VideoWriter this way, the order of the frames would be scrambled. The simplest approach is probably simply to generate the frames on the workers, and then return them to the client where you add them to a VideoWriter. Like this (adapted from the example in the VideoWriter help text).
% Set up figure on each worker
Z = peaks;
spmd
surf(Z);
axis tight manual
set(gca,'nextplot','replacechildren');
end
% We are going to store the frames generated by the workers here
frames = {};
parfor k = 1:20
% Generate a random number of frames per parfor iteration
numFrames = randi(10);
% `kFrames` is a cell array for this value of `k`.
kFrames = cell(1, numFrames);
for j = 1:numFrames
% Use `surf` to plot a single frame
surf(sin(2*pi*(k+rand)/20)*Z,Z)
% Add the frame to temporary cell array `kFrames`
kFrames{j} = getframe(gcf);
end
% Accumulate all frames using concatenation
% `parfor` will guarantee that `frames` is in the correct
% order.
frames = [frames, kFrames];
end
% Back at the client, write the frames to the file
vidObj = VideoWriter('peaks.avi');
open(vidObj);
for idx = 1:numel(frames)
writeVideo(vidObj,frames{idx});
end
close(vidObj);
  1 Comment
student student
student student on 22 Feb 2022
many many thanks for all your inputs: I will try again tomorrow to debug this with your way again for the Frame matrix, I had some errors tonight. thanks again!

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!