MatLab, how to preallocate for frames to make a movie?
Asked Answered
Q

3

5

Matlab has the following guide to making a movie in avi format. My goal is to be able to play the video in my presentation through powerpoint.

nFrames = 20;
% Preallocate movie structure.
mov(1:nFrames) = struct('cdata', [],...
                    'colormap', []);

% Create movie.
Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');
for k = 1:nFrames 
surf(sin(2*pi*k/20)*Z,Z)
mov(k) = getframe(gcf);
end

% Create AVI file.
movie2avi(mov, 'myPeaks.avi', 'compression', 'None');

I understand this example and that I should have no compression to load into PowerPoint. However I dont understand how to properly preallocate my memory using struct.

Queenstown answered 5/11, 2011 at 19:40 Comment(2)
You probably do want your video compressed, as uncompressed video takes up a lot of disk space. You just need to make sure to choose a container format (eg. avi) and compression method that your version of PowerPoint supports. The pre-allocation should be optional, and only make the process of generating the movie file slightly faster. The code you've posted looks like it only preallocates an array of pointers, instead of preallocating all the memory required to hold the movie, so it probably exists more to suppress code style warnings than to speed things up.Midst
Thank you for your help. Would you happen to know how to use a handle with the addframe command so that getframe does not save my screensaver image while I am away?Queenstown
S
1

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.

Saros answered 6/11, 2011 at 14:5 Comment(0)
S
3

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')
Sculpture answered 7/11, 2011 at 15:22 Comment(0)
S
1

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.

Saros answered 6/11, 2011 at 14:5 Comment(0)
E
1

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.
Embrey answered 7/8, 2015 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.