Error using Video Writer
12 views (last 30 days)
Show older comments
Matthias Heindl
on 10 Aug 2020
Commented: Image Analyst
on 12 Aug 2020
Hey there,
I work on doing a simplified thermodynamic animation and I use the Video Writer object to accomplish that.
writeVideo requires each frame to be the exact same size. My frames are not. The reason for that may simply be, that it's a three dimensional simulation.
Is there a way, I can unify or fix the size for each frame?
The plot is created by using the 'surf' command and the 'patch' command. Since it's quite some lines of code, I decided not to share it here.
avi_video = VideoWriter('process_1.avi');
open(avi_video);
allTheFrames = cell(timeframes,1);
allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
allColorMaps = cell(timeframes,1);
allColorMaps(:) = {zeros(256, 3)};
recalledmie = struct('cdata', allTheFrames, 'colormap', allColorMaps);
F_cell = struct2cell(F);
thisFirstFrame = cell2mat(F_cell(1, 1, calculations_per_frame));
continue_counter = 0;
for frame = 1 : timeframes
thisFrame = cell2mat(F_cell(1, 1, frame * calculations_per_frame));
if any(size(thisFrame) ~= size(thisFirstFrame))
continue_counter = continue_counter + 1;
continue
end
recalledmie(frame) = im2frame(thisFrame);
writeVideo(avi_video, thisFrame);
end
close(avi_video);
I have been trying for some hours and I have found a very general statement on what to do here: https://de.mathworks.com/matlabcentral/answers/229630-why-do-i-get-error-in-videowriter-as-error-using-videowriter-writevideo-frame-must-be-436-by-344-e
Furthermore I tried to work my way around that problem by setting up an if condition (Is the size of the current frame equal to the size of the first frame, and if not 'continue'), but too many frames seem to be of a slightly different size. I was not able to get a proper video from that.
2 Comments
Sudheer Bhimireddy
on 10 Aug 2020
Did you try limiting the axis or axis length based on your values?
Accepted Answer
Image Analyst
on 10 Aug 2020
If your subsequent images are not the same size as the first frame, you could call imresize() to make them the same size.
2 Comments
Image Analyst
on 12 Aug 2020
Thanks for accepting the answer. By the way,
if any(size(thisFrame) ~= size(thisFirstFrame))
could also be done like this:
if ~isequal(size(thisFrame), size(thisFirstFrame))
which is how I'd do it.
More Answers (1)
Sudheer Bhimireddy
on 10 Aug 2020
Your code looks strange. Do you have 'hold on' before the loop?
Also if you have complete extents of your x,y variables, you can find the global maximums and use them with xlim and ylim as shown below.
fig = figure(1);
f = gcf;
f.Position=[100 100 1050 340]; % <-this will be figure window display size in pixels
f.PaperUnits='inches';
f.PaperPosition=[0 0 4 3]; %<-this will be print size, all the frames will have this size
for t = 1:time_iterations
clf;
hold on;
for 1:number_objects_1
surf(x,y,z,'red')
end
for 1:number_objects_3
surf(x,y,z,'blue')
end
...
xlim([x_min x_max]);
ylim([y_min y_max]);
end
3 Comments
Sudheer Bhimireddy
on 10 Aug 2020
As long as your framesize is fixes, it should not matter. If the framesize is not fixed, the figure axis object might be trying to adjust the limits to include the objects. More than xlim and ylim; try the first five lines of my code and see what happens (note that I have moved your figure initiation line outside the loop). If you are still getting an error, try to attach two consecutive frames as pictures.
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!