Approaches to create a video in matlab
在Matlab中创建视频的可能性是什么? 我经过搜索后发现,主要有3个工具箱可用于该领域,包括图像处理,图像采集和视觉控制...但是如果没有它们,我如何才能做到这一点呢? 我对概述的各种方法感兴趣,但无法在互联网上找到任何不错的教程或一致的信息来源。
谢谢您的帮助!
以下是在(核心)MATLAB中创建电影的一些不同方法。
电影2AVI
(已弃用,请改用VIDEOWRITER)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | %# 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') |
AVIFILE
(已弃用,请改用VIDEOWRITER)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | %# 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') |
视频制作人
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | %# 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') |
写入
(从技术上讲不是电影,而是GIF动画图片)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | %# 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') |
有http://www.mathworks.de/help/techdoc/ref/videowriterclass.html
我的方法是使用
当涉及到转换参数(比特率,编解码器,几何等)时,这可以进行很多微调。
Matlab具有内置的"电影"命令来播放电影。我发现使用它非常容易。我在绘图中使用它来显示时间的变化,并使用单个图像制作一部真实的电影。
http://www.mathworks.com/help/techdoc/ref/movie.html
我相信一般程序是:
1 2 3 4 5 |
要保存电影,可以使用与上述类似的步骤,但是请使用
1 | writeVideo |
命令。
http://www.mathworks.com/help/techdoc/ref/videowriterclass.html
QTW作家
要导出QuickTime电影,可以使用我自己的QTWriter:http://horchler.github.io/QTWriter/。它的工作原理与Matlab的
这是一些示例代码,说明如何生成简单的循环,可变帧率的QuickTime电影:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | % 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 |
输出的影片,另一个更复杂的演示以及更多详细信息可在项目网站上找到。 QTWriter是开源的(BSD许可证),代码存储库托管在GitHub上。