Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Make movie without displaying figures using VideoWriter?

Asked by Mitch on 16 Aug 2013

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)

0 Comments

Mitch

Products

No products are associated with this question.

1 Answer

Answer by 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

Walter Roberson on 21 Aug 2013

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

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.

Walter Roberson on 21 Aug 2013

export_fig is better at being used headless.

Image Analyst

Contact us