MATLAB movies

When using 3D figures in MATLAB, it is useful to be able to generate a movie of the scene for demonstration, publication, etc..

Make a figure

Start by making a figure in MATLAB. Here I used the trk_plot function in the along-tract-stats package to display a correspondence plot of a white matter tract group derived from DTI data. Of course any other 3D (or 2D) figure could also be used.

trk_plot(header, tracks_interp_str, volume, [95 78 4], 'rainbow')

Set options

Once the basic figure is open, there are a few figure settings that should be adjusted.

Aspect ratio

When you save an image from MATLAB, the output dimensions are set independently of the on-screen window dimensions. I like to set the aspect ratio to be equivalent to 1280×720 so it's easy to make 720p HD movies.

set(gcf, 'PaperSize', [12.8 7.2])
set(gcf, 'PaperPosition', [0 0 12.8 7.2])

View angle

The view angle adjusts how much of the scene fills the viewport. This works like a zoom.

set(gca, 'CameraViewAngleMode', 'manual')
set(gca, 'CameraViewAngle', 4)

Figure background

For neuroimaging plots, sometimes it is nice to have a black background.

set(gcf, 'Color', [0 0 0])
set(gcf,'InvertHardcopy','off')

Axis labels

Similarly, it is often useful to omit the axis labels with axis off.

Projection type

If you want your figure to show perspective, you can use a builtin convenience function to do this. See here.

Slice transparency

When rotating around a 3D object, sometimes it can be useful to make the object transparent. Do this like alpha(0.85).

Anti-aliasing

This gets rid of the “jaggies” in exchange for a bit of smoothing in the image. Use the excellent myaa function from MATLAB Central.

Example

Here is what the figure looks like after I set these options:

set(gcf, 'PaperSize', [12.8 7.2])
set(gcf, 'PaperPosition', [0 0 12.8 7.2])
set(gca, 'CameraViewAngleMode', 'manual')
set(gca, 'CameraViewAngle', 4)
set(gcf, 'Color', [0 0 0])
set(gcf,'InvertHardcopy','off')
axis off
alpha(0.85)
myaa

Save out image files

Now it's time to save out the image sequence. For this example, we simply use a for loop to change the azimuth rotation about the z axis in 1 degree increments. This has the effect of shifting from a coronal view to a left sagittal view.

for i=0:90
  view([i 0])
  print(gcf, '-djpeg100', '-r100', sprintf('image_%d', i));
end

If you want to use anti-aliasing for the movie, then you must do a couple things differently:

  • Set the window size of the initial image, since these pixels are what gets anti-aliased.
  • Set the paper size/position of the original and anti-aliased figures, since these get reset each time.
  • Close each new anti-aliased image after printing.
set(gcf, 'Position', [0 0 1280 720])
for i=0:90
  view([i 0])
  set(gcf, 'PaperSize', [12.8 7.2])
  set(gcf, 'PaperPosition', [0 0 12.8 7.2])
  myaa(3)
  set(gcf, 'PaperSize', [12.8 7.2])
  set(gcf, 'PaperPosition', [0 0 12.8 7.2])
  print(gcf, '-djpeg100', '-r100', sprintf('image_%d', i));
  close
end

As stated in the myaa help, it will fail if the super-sampling factor is set too high. For example, with a figure sized 1280×720, I can use K=2 or 3, but not K=4. If you want better anti-aliasing, you can change the renderer with set(gcf, 'Renderer', 'zbuffer'), but this means you won't be able to use transparency. I usually keep the OpenGL renderer and settle for a super-sampling of 3.

Compile movie in Quicktime

Finally, this image sequence can be compiled into a movie with Quicktime. See wiki entry: Quicktime.

computing/matlab-movies.txt · Last modified: 2011/03/26 10:03 am PDT by John Colby
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki