Why does "getframe" capture frames with different sizes when the Camera properties have been modified in MATLAB R2014a?

21 views (last 30 days)
I am trying to make a movie by rotating a 3D surface. I do this by using the "view" function within a "for" loop. However, when I use "getframe" to capture the frames that will be passed to the "movie2avi" function, I get an error saying that not all the frames have the same size.
My code is:
% Draw a sphere
sphere
% Make the current axis box square in size
axis('square')
% Define title and labels for reference
title('Rotation of a sphere...')
xlabel('x')
ylabel('y')
zlabel('z')
j = 1;
% Modify azimuth (horizontal rotation) and update drawing
for az = -50 : .2 : 30
view(az, 40)
F(j) = getframe;
j = j+1;
end
% Modify elevation (vertical rotation) and update drawing
for el = 40 : -.2 : -30
view(30, el)
F(j) = getframe;
j = j+1;
end
movie2avi(F,'movie.avi','compression','none')
Which leads to the following error:
Error using avifile/addframe>ValidateFrame (line 289)
Frame must be 290 by 343.
Error in avifile/addframe (line 157)
ValidateFrame(aviobj,width, height,dims);
Error in movie2avi (line 66)
avimov = addframe(avimov,mov);

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Oct 2013
When the Camera properties change, the dimensions of the axes in which the 3D plot is rendered change slightly. By default, the "getframe" function captures the current axes and, therefore, not all frames will have the same size. Then, the "movie2avi" function throws an error because a movie file can only be created if all frames have the same size.
The "getframe" function can be used to capture the entire figure, instead of only capturing the axes. Using "getframe" with a figure handle ensures that all frames have the same size. In addition, the title of the plot and eventual other elements in the figure will be also included in the movie.
The following piece of code uses the "getframe" function on the handle to the figure containing the 3D plot. Also, note that it is recommended to use "VideoWriter" instead of "movie2avi." "VideoWriter" is faster and more reliable. Finally, if you are using MATLAB R2014a or earlier, the renderer should be set to Z-Buffer to avoid a bug in OpenGL that affects MATLAB R2014a and earlier versions.
% Create a VideoWriter object
writerObj = VideoWriter('movie.avi');
open(writerObj);
% Create a new figure
f = figure;
% Set a size if desired
width = 800;
height = 600;
set(f,'Position',[15 15 width height])
% Change the renderer to avoid bugs due to OpenGL
set(f,'Renderer','ZBuffer')
% Draw a sphere
sphere
% Make the current axis box square in size
axis('square')
% Define title and labels for reference
title('Rotation of a sphere...')
xlabel('x')
ylabel('y')
zlabel('z')
% Modify azimuth (horizontal rotation) and update drawing
for az = -50 : .2 : 30
view(az, 40)
% Capture the entire figure window
frame = getframe(f);
writeVideo(writerObj,frame);
end
% Modify elevation (vertical rotation) and update drawing
for el = 40 : -.2 : -30
view(30, el)
% Capture the entire figure window
frame = getframe(f);
writeVideo(writerObj,frame);
end
% Finish creating the AVI file
close(writerObj);

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!