Using Matlab to Create a Sequence of gif Files and Then How to Convert Them to MPEG

Abstract
A brief description of the steps required to use Matlab to create a sequence of gif files and how to convert those files into an mpeg movie. The method discussed converts the image files to mpeg using the mpeg_encode program which is available at s2k-ftp.cs.berkeley.edu. To create gif files in a Matlab script it is assumed that you are working in unix and have the ghostscript distribution installed. Matlab creates gif files by first creating postscript output and converting to gif using ghostscript . (at least that is my understanding)

Local note: If you are on UNH's spice machines we already have all of the conversion programs installed.

If you already have the gif files you want to convert skip to the section on file names.


Required Conversion Programs

The conversion routines discussed here use the mpeg_encode program. A copy of the program can be found at s2k-ftp.cs.berkeley.edu. To use this conversion utility you must be able to convert your image files to either ppm, pnm, or yuv format. If you do not have the netpbm conversion utilities you may need to install them. The necessary files can be found at ftp.cs.ubc.ca.

Creating gif files in Matlab

To create gif files in Matlab it is assumed that you are working under the unix operating system and have ghostscript installed. Matlab will automatically call ghostscript to convert its postscript output to gif.

In order to create numbered files Matlabs string replace function, strrep, is used to update the filename. Once a plot is produced it can be written to a gif file using the print command with the -dgif8 option. Because the string is updated the command is executed using the eval function.

As a simple example, a sequence of gif files can be written out with the file names numbered in order. The example will create 81 gif files named curve1.gif, curve2.gif, curve3.gif, curve4.gif,... curve81.gif. Because of memory limitations we were only able to use the first 60 frames for the example mpeg given below.


File names for the image files.

Before encoding you must first have the image files that you would like to convert to mpeg. To let the encoder know the correct order you can specify the files in a couple of different ways. The easiest way is to generate a sequence of image files which are numbered sequentially. For example, for the curve in the matlab example a simple periodic curve is evolved in time. If I start with the base name curve1.gif the following files are to be converted to mpeg:

curve1.gif
curve2.gif
curve3.gif
...
curve35.gif
curve36.gif
curve37.gif
...
curve80.gif
curve81.gif

There is one problem with this arrangement if you are working on an SGI workstation. The movieconvert program that is now part of OS 5.3 requires file names where the numbers are in fixed field. For example, the pressure field at the 5th step should be in a file called curve5.gif, but in your program you may have generated the file and called it curve05.gif so that you could use the movieconvert program. Since the program converts to quicktime and not mpeg you may want to create two movie versions (or use mpeg since it compresses MUCH better). To convert file names from the SGI format to the format that mpeg_encode prefers try this perl script that we use. You can then use mpeg_encode on the new sequence of files.


Converting the Files

Once you have the image files and have installed the conversion software the conversion process itself is not too complicated. Before the conversion can take place, however, you must specify the files and certain parameters for the conversion process. Since the files are assumed to be in gif format you must also specify how each file is to be converted. We have found that the simplest way is to use the giftopnm program found in the netpbm distribution.

The parameters that are used in the conversion process are specified in a separate file. The file must follow a very specific format. For an example of the format file see matlab.param. We have found this to give decent results. Because we used small numbers for IQSCALE, PQSCALE, and BQSCALE the compression is not as tight as some would like. However, it does offer relatively sharp output. Of course, your mileage will vary!

The parameter file matlab.param, does the following things:

  1. Output will be an mpeg file called curve.mpeg
  2. Files will be found in directory ./plot/base
  3. Files will be called curve1.gif through curve60.gif.
  4. The image files will be converted to PNM format
  5. The image files will be converted with the command giftopnm
Play with remaining parameters to get the movie you are looking for. Be sure to make RANGE divisible by SLICES_PER_FRAME. To increase the sharpness of the images make IQSCALE, PQSCALE, and BQSCALE smaller. To increase the efficiency of the compression increase these variables.

Once all these things are in place and you have a parameter file (pressure.param) you can encode your files with the following command:
mpeg_encode matlab.param

After you have gone out for a cup of coffee you will find a new mpeg file.

Warning! The parameter file given in this example seems to do a better job of using less memory than the parameter file that is given in the rgb to mpeg tutorial. If you run out of swap space using these parameter files you may have to play around with the parameters to reduce the required memory. Look at both and compare for the best results.


Bibliography

Graphics and Visualization Lab Manual, Army High Performance Computing Research Center, University of Minnesota.

mpeg_encode man pages.

MPEG Technical Info, Honolulu Community College's Web Site.
Kelly Black