Make movie without displaying figures using VideoWriter?

I would like to make a MATLAB movie without displaying figures, so that I can still use my computer as I am making the movie. I am trying to make a movie using VideoWriter without using the getframe command. getframe causes the figure to pop up in your window even if visible is set to 'off.'
I managed to get it work using aviobj (which is currently being phased out by MathWorks), but cannot figure it out with VideoWriter. Does anybody have any ideas? My code using aviobj is below.
Thanks, Mitch
aviobj = avifile('example.avi','compression','None');
h=figure(1)
for i=1:100
set(h,'Visible','off')
pcolor(x,z,vort_series(:,:,i))
colorbar
shading interp
drawnow
aviobj = addframe(aviobj,h);
end
close(h) aviobj=close(aviobj)

Answers (1)

Mitch, where are the images coming from? If they're combing from disk, you can adapt this part of my demo (that I've posted before). The image files are not displayed before writing.
% Create a VideoWriter object to write the video out to a new, different file.
writerObj = VideoWriter('NewRhinos.avi');
open(writerObj);
% Read the frames back in from disk, and convert them to a movie.
% Preallocate recalledMovie, which will be an array of structures.
% First get a cell array with all the frames.
allTheFrames = cell(numberOfFrames,1);
allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
% Next get a cell array with all the colormaps.
allTheColorMaps = cell(numberOfFrames,1);
allTheColorMaps(:) = {zeros(256, 3)};
% Now combine these to make the array of structures.
recalledMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps)
for frame = 1 : numberOfFrames
% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Read the image in from disk.
thisFrame = imread(outputFullFileName);
% Convert the image into a "movie frame" structure.
recalledMovie(frame) = im2frame(thisFrame);
% Write this frame out to a new video file.
writeVideo(writerObj, thisFrame);
end
close(writerObj);
% Get rid of old image and plot.
delete(hImage);
delete(hPlot);
% Create new axes for our movie.
subplot(1, 3, 2);
axis off; % Turn off axes numbers.
title('Movie recalled from disk', 'FontSize', fontSize);
% Play the movie in the axes.
movie(recalledMovie);
% Note: if you want to display graphics or text in the overlay
% as the movie plays back then you need to do it like I did at first
% (at the top of this file where you extract and imshow a frame at a time.)
msgbox('Done with this demo!');

7 Comments

Looking at the original code, the images are coming from pcolor() then "shading interp". The pcolor() itself would be easy enough to handle without displaying, but the "shading interp" would not be easy.
I wasn't sure if that was some way that he said worked (with some bogus data) but he didn't want to do it that way, or if that actually had the data he wanted. Anyway, pcolor() displays. If he wants no display then he'll have to figure out how to make an image out of what he's displaying, and not call pcolor(). For example instead of using imread() like I did, he can do
thisFrame = vort_series(:,:, frame)
The calculations required to do an RGB lookup to create an image array equivalent to what pcolor() would display are not all that difficult. Emulating shading would not be as easy.
Thanks for the reply. The images are not coming from disk, rather they are coming from the pcolor() call.
pcolor doesn't necessarily cause the image to display. In the code I included using aviobj it doesn't. My problem is that the only way I know how to use VideoWriter is to use a getframe() command (as opposed to addframe as above), which causes the image to pop up on screen.
Thanks again for the help!
If you were not using shading, then it would be possible to construct the data array that would be added with VideoWriter. The shading is more difficult to compute.
Possibly you could do something using the FEX contribution export_fig
export_fig does, or requires, a displayed image doesn't it? He's trying to avoid that. What kind of data is vort_series(:,:,i) anyway? Is it a 2D grayscale image? If so, you could just write that out directly without calling pcolor() to display it, or if you want some special colormap applied to the grayscale image you can create the colormap and then use ind2rgb() apply it and then write it. Again, that can all be done directly, without displaying anything.
export_fig is better at being used headless.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 16 Aug 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!