Make movie without displaying figures using VideoWriter?

40 views (last 30 days)
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)

Image Analyst
Image Analyst on 18 Aug 2013
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
Image Analyst
Image Analyst on 21 Aug 2013
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.

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!