parfor for movie writeVideo
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Catalytic
le 21 Fév 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.
Réponses (1)
Edric Ellis
le 22 Fév 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);
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!