Reputation: 1153
What are the possibilities to create videos in Matlab? I was searching and found mainly 3 toolboxes that work in this field, image processing, image acquisition and control vision... but how can I do it without them, just to create the video from scratch? I'm interested in various approaches to have an overview, but I was unable to find any decent tutorial or consistent source of info on the internet.
Thanks for the help!
Upvotes: 29
Views: 66624
Reputation: 124553
Here are some of the different ways to create movies in (core) MATLAB.
(deprecated, use VIDEOWRITER instead)
%# figure
figure, set(gcf, 'Color','white')
Z = peaks; surf(Z); axis tight
set(gca, 'nextplot','replacechildren', 'Visible','off');
%# preallocate
nFrames = 20;
mov(1:nFrames) = struct('cdata',[], 'colormap',[]);
%# create movie
for k=1:nFrames
surf(sin(2*pi*k/20)*Z, Z)
mov(k) = getframe(gca);
end
close(gcf)
%# save as AVI file, and open it using system video player
movie2avi(mov, 'myPeaks1.avi', 'compression','None', 'fps',10);
winopen('myPeaks1.avi')
(deprecated, use VIDEOWRITER instead)
%# figure
figure, set(gcf, 'Color','white')
Z = peaks; surf(Z); axis tight
set(gca, 'nextplot','replacechildren', 'Visible','off');
%# create AVI object
nFrames = 20;
aviobj = avifile('myPeaks2.avi', 'fps',10);
%# create movie
for k=1:nFrames
surf(sin(2*pi*k/20)*Z, Z)
aviobj = addframe(aviobj, getframe(gca));
end
close(gcf)
%# save as AVI file, and open it using system video player
aviobj = close(aviobj);
winopen('myPeaks2.avi')
%# figure
figure, set(gcf, 'Color','white')
Z = peaks; surf(Z); axis tight
set(gca, 'nextplot','replacechildren', 'Visible','off');
%# create AVI object
nFrames = 20;
vidObj = VideoWriter('myPeaks3.avi');
vidObj.Quality = 100;
vidObj.FrameRate = 10;
open(vidObj);
%# create movie
for k=1:nFrames
surf(sin(2*pi*k/20)*Z, Z)
writeVideo(vidObj, getframe(gca));
end
close(gcf)
%# save as AVI file, and open it using system video player
close(vidObj);
winopen('myPeaks3.avi')
(technically not a movie, but an animated GIF image)
%# figure
figure, set(gcf, 'Color','white')
Z = peaks; surf(Z); axis tight
set(gca, 'nextplot','replacechildren', 'Visible','off');
%# preallocate
nFrames = 20;
f = getframe(gca);
[f,map] = rgb2ind(f.cdata, 256, 'nodither');
mov = repmat(f, [1 1 1 nFrames]);
%# create movie
for k=1:nFrames
surf(sin(2*pi*k/20)*Z, Z)
f = getframe(gca);
mov(:,:,1,k) = rgb2ind(f.cdata, map, 'nodither');
end
close(gcf)
%# create GIF and open
imwrite(mov, map, 'myPeaks4.gif', 'DelayTime',0, 'LoopCount',inf)
winopen('myPeaks4.gif')
Upvotes: 50
Reputation: 18484
To export QuickTime movies, my own QTWriter is available: http://horchler.github.io/QTWriter/. It works very similarly to Matlab's VideoWriter
class, but has both lossy and lossless still image codecs (compression formats) that work well with typical data in Matlab plots (i.e., no inter-frame compression). Notably it also supports alpha channel transparency ('Photo PNG' codec), looping (two kinds), and continuously variable frame rates. QTWriter is written as a single Matlab class file and should work on all platforms, but I have not tested it on Windows.
Here's some example code illustrating how a simple looping, variable frame-rate QuickTime movie can be generated:
% Prepare new movie file using the default PNG compression
movObj = QTWriter('peaks.mov');
% Create an animation
hf = figure; Z = peaks; surfc(Z); frames = 100;
axis tight; set(hf,'DoubleBuffer','on');
set(gca,'nextplot','replacechildren');
% Animate plot and write movie
for k = 0:frames
hs = surfc(sin(2*pi*k/frames)*Z,Z);
set(hs,'FaceColor','interp','FaceLighting','phong');
light('Position',[0 0 4]);
movObj.FrameRate = k; % Vary the frame-rate
writeMovie(movObj,getframe(hf)); % Write each frame to the file
end
movObj.Loop = 'backandforth'; % Set palindromic looping flag
close(movObj); % Finish writing movie and close file
The output movie, another more complex demo, and further details are available on the project website. QTWriter is open source (BSD license) and the code repository is hosted on GitHub.
Upvotes: 5
Reputation: 4340
Matlab has a built in 'movie' command to play movies. I find it pretty easy to work with. I've used it on plots to show changes in time, as well as individual images to make a real movie.
http://www.mathworks.com/help/techdoc/ref/movie.html
I believe the general procedure is:
for ii=1:100
plot(something(ii))
F = getframe;
end
movie(F)
To save a movie, you can use a similar procedure as above, but use the
writeVideo
command.
http://www.mathworks.com/help/techdoc/ref/videowriterclass.html
Upvotes: 7
Reputation: 2621
There is http://www.mathworks.de/help/techdoc/ref/videowriterclass.html
My approach is to print the single frames/figures to png files using the print
function giving them filenames like 1.png, 2.png, ...
and then I use the free FFMPEG converter to make a video.
ffmpeg -r 20 -i %d.png foo.avi
This allows for a lot of finetuning, when it comes to the parameters of the conversion (bitrate, codec, geometry etc.).
Upvotes: 12