MatLab, how to preallocate for frames to make a movie?

戏子无情 提交于 2019-12-05 18:43:48

You don't need to pre-allocate. Just initialize mov = []. Also getframe assumes gcf, so you can just use mov(k) = getframe(). I agree that you want an uncompressed video. The codecs that come with Matlab are pretty limited. You could use an open source tool to compress the video if space is important.

You could use avifile to create the movie or even the newer VideoWriter:

avifile

Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');

vid = avifile('myPeaks.avi', 'fps',15, 'quality',100);
for k = 1:20 
    surf(sin(2*pi*k/20)*Z,Z)
    vid = addframe(vid, getframe(gcf));
end
vid = close(vid);

winopen('myPeaks.avi')

VideoWriter

Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');

vid = VideoWriter('myPeaks2.avi');
vid.Quality = 100;
vid.FrameRate = 15;
open(vid);
for k = 1:20 
    surf(sin(2*pi*k/20)*Z,Z)
    writeVideo(vid, getframe(gcf));
end
close(vid);

winopen('myPeaks2.avi')

You don't have to pre-allocate. It used to help to pre-allocate using moviein command, but it no longer provides any performance improvements. Here is the quote from MATLAB:

>> help moviein
moviein Initialize movie frame memory.
moviein is no longer needed as of MATLAB Release 11 (5.3).  
In previous revisions, pre-allocating a movie increased 
performance, but there is no longer a need to pre-allocate 
movies. To create a movie, use something like the 
following example:

  for j=1:n
     plot_command
     M(j) = getframe;
  end
  movie(M)

For code that is compatible with all versions of MATLAB, 
including versions before Release 11 (5.3), use:

  M = moviein(n);
  for j=1:n
     plot_command
     M(:,j) = getframe;
  end
  movie(M)

See also movie, getframe.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!