parfor for movie writeVideo

24 visualizaciones (últimos 30 días)
student student
student student el 21 de Feb. de 2022
Comentada: student student el 22 de Feb. de 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 comentarios
Catalytic
Catalytic el 21 de Feb. de 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 el 22 de Feb. de 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 !

Iniciar sesión para comentar.

Respuestas (1)

Edric Ellis
Edric Ellis el 22 de Feb. de 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 comentario
student student
student student el 22 de Feb. de 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!

Iniciar sesión para comentar.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by