Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with movie file creation in MATLAB

I attempt to create a movie by looping through frames in MATLAB.

Refering to mathworks.com documentation at http://www.mathworks.com/help/techdoc/ref/movie.html, I've managed to animate a plot. However, issues arise when I attempt to save the movie in an avi file.

Both the avifile and VideoWriter methods from https://stackoverflow.com/a/8038540/818608, resulted in the same errors.

Although the animation runs fine, the saved movie consists of only one fixed frame, and at times, the screen capture includes an overlay of my background web browser.

Thank you in advance.

Below is the code I used, and the frame that's frozen on the avi is linked below.

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')

The frame that's frozen on the avi is linked below

like image 822
flamearchon Avatar asked Dec 21 '11 00:12

flamearchon


Video Answer


2 Answers

I had this (well, a closely related) problem today. This stackoverflow topic was one of the top search engine results, so I thought I'd provide future searchers with some further info.

I was using a VideoWriter object, and calling frame=getframe(fig_handle) to save each frame to the video. As in the question to this topic, only 1 frame was saved. In addition, the background behind the figure could be seen through it, as if the figure was partially transparent.

Changing renders to painters or zbuffer worked. (set(gcf,'renderer','zbuffer') for example.)

I needed openGL rendering though, since the movie used transparency. The key to making this work was to use

opengl('software')

This circumvented what was probably an issue with sending the graphics to and from the video card (I don't know for sure... it worked, and I moved on).

like image 129
tmpearce Avatar answered Sep 19 '22 01:09

tmpearce


Try the following:

    f = figure();
    Z = peaks; surf(Z);
    a = axes('Parent',f);
    axis(a,'tight');
    set(a,'nextplot','replacechildren');
    vid = VideoWriter('myPeaks2.avi');
    vid.Quality = 100;
    vid.FrameRate = 15;
    open(vid);
    for k = 1:20
        surf(a,sin(2*pi*k/20)*Z,Z)
        writeVideo(vid, getframe(f));
    end
    close(vid);

    winopen('myPeaks2.avi')

It contains explicit handles using instead of implicit. Many chaos is caused in Matlab because people tend to use the implicit ones, like "gcf", "gca" which should have been removed completely from the language, IMHO.

like image 28
Andrey Rubshtein Avatar answered Sep 20 '22 01:09

Andrey Rubshtein