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
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
student student
student student on 22 Feb 2022
many thanks ! I did open right before the code like this:
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;
axis tight manual
% 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
% Add the frame to temporary cell array `kFrames`
kFrames{j} = getframe(gcf);
% Accumulate all frames using concatenation
% `parfor` will guarantee that `frames` is in the correct
% order.
frames = [frames, kFrames];
% Back at the client, write the frames to the file
vidObj = VideoWriter('peaks.avi');
for idx = 1:numel(frames)
  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.




Community Treasure Hunt

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

Start Hunting!

Translated by