12/12/2000

Creating Animated Gifs and AVIs from the Matlab “Getframe” Command
by Richard Hagl

UPDATE:
    08/07/2001

This tutorial briefly explains how to create animated gif and avi files suitable to be imported to a Microsoft Power Point presentation or a web site. Usually, simulations can be played back in Matlab using the “movie” command, however, if one wishes to use the obtained movie in a presentation without having access to Matlab, exporting the movie becomes necessary.

It is assumed that the reader is familiar with playing, and creating Matlab animations using the getframe and movie commands. Further, two freeware programs are used:

  1. Ulead GIF Animator LE (v 1.0)
  2. Fast Movie Processor 1.41

Ulead GIF Animator LE (LE = Lite Edition) is freeware and can be found by a simple web search. Finding Fast Movie Processor 1.41 will be harder since it is basically replaced by its successor VideoMach 2.3.3. Both Fast Movie Processor 1.41 and VideoMach 2.3.3 are free to non-commercial uses, but require registration for commercial use. VideoMach 2.3.3 can be found at http://www.gromada.com/VideoMach_dl.html. This tutorial will refer to Ulead GIF Animator LE and Fast Movie Processor 1.41 only.

  For now, can also be obtained at the following FTP site: ftp.hagl.net. Login as tutorial, password: tutorial (Distribution is permitted under copyright law).

 When creating the desired animated gif and avi file we will perform the following steps:

  1. Create individual images in Matlab
  2. Combine the individual images to an animated gif file
  3. Create an avi file from the animated gif image.

 

1. Creating the individual images in Matlab 

Consider the following code sequence:

1.     for (n = ...)
2.    
      <Create the matrix "frame" that will represent one movie frame>

3.    
   pcolor(frame);               % create psudo color image of current frame
4.    
   Image = getframe;
       % get current pseudo color image
5.        M(:,n) = I;                     % store the current image in an array (this is

6.    
                                         % only needed for playback in Matlab)
7.        P = frame2im(Image)    % Convert to a image representation that  Matlab can handle
8.      

9.    
   directory = 'images/';  % The next four lines parse and assemble file
10.     number = num2str(n);    % names for the current image (Note the need
11.     extension = '.bmp';     % for a directory called "images"
12.     filename = [directory;number;extension];

13.     imwrite(P,eval('filename'), 'bmp') % Finally write individual images
14.  end

How exactly the individual frames are created is up to the reader, since – as mentioned earlier – familiarity with getframe is assumed. After creating the pseudo color representation of our result with the pcolor command, we create a single image as follows:

Line 5:
Store the single image for playback with the Matlab movie command. Be aware that this may lake a lot of memory!

Line 7:
Capture the frame to image format.

Lines 9-12:
Build the filename we want to store the image under. For example let’s s say that the first time through the loop n will contain the numeric value of 1. We convert this to the string ‘1’ and concatenate it with the words ‘images/’ and ‘.bmp’ to the string ‘images/1.bmp’. Note that before the code is run a directory called ‘images’ (a subdirectory of the directory that contains the m-file) must be created. After execution the directory will contain the following files: 1.bmp, 2.bmp, … .

Line 13:
Here the actual image files are written in bitmap (bmp) format. 

After step 1 we have a directory containing individual images of each frame in a directory called ‘images’.

 2. Combining individual bmps to animated gif

 This step can easily done with Ulead GIF Animator LE.

  1. Open the program

  2. Open a 'blank animation'

  3. Click on the 'add images' symbol (next to the paste button)
                                  

  4. Navigate to the directory that contains the images: .../images

  5. Select all of the created bitmap files and click on 'open'. Note how Gif Animator correctly preserves the ordering of the images (e.g. it does not put 10.bmp before 2.bmp).

  6. By using the preview button, the animation can be seen, and on the left individual frames can be edited.

  7. Click on 'Save' and the animated gif is ready

The animated gif we just created is now suitable for many applications, like web sites. It should also be usable for Microsoft Power Point if it were not for a bug in this software:

The Power Point version of Office 97 does not support animated gifs, so that only the very first frame would appear. This is supposedly fixed in Office 2000, however, as soon as the animated gif file exceeds a certain number of frames it begins to play very choppy up to the point were it becomes useless. This problem can be resolved by converting the animated gif file to a movie file format that is supported by Power Point such as "avi". How this is done is explained in step 3.

 

3. Converting animated gif to avi

 To do this we use Fast Movie Processor 1.41 (see below for image):

1.       Open the program
2.       Click on the yellow '+' symbol under 'input files' and navigate to the animated gif file.
3.       Click on the diskette symbol under 'output' and choose a filename and the 'avi' file type option.
4.       Change the output file options if desired (e.g. 24 bit color for better quality and/ or rotation).
5.       Click ‘Ok’
6.       Click on the flash symbol to begin processing the file.

         

This should then conclude the creation of the 'avi' file, which can easily be inserted into Power Point Note that some settings still need to be adjusted within Power Point such as playing continuously and/or playing immediately when the slide open. These options are found when right clicking on the imported avi file in Power Point and exploring the ‘action settings’ menu. 

Please feel free to email Richard Hagl (rhagl@students.wisc.edu) with any questions or problems that may arise. Also, feedback about the usefulness of this tutorial is welcome.