2016-01-22 112 views
-1

我有问题产生动画的情节,使用MATLAB。动画剧情MATLAB

我要绘制我的信号y作为我的时间x的函数(即我保持在两个单独的变量),然后产生它的一个动画,根据时间看到我的信号的变化。

最后,我想生成一系列“.tif”图像(用于在imageJ中读取它)和“.avi”电影文件。

如果有人能告诉我方法,这真的会帮助我,因为我尝试使用MATLAB帮助和论坛自己做,但我每次都失败。

在此先感谢!

回答

1

这样做的标准方法是在一个循环内更新您的绘图数据,并使用getframe或类似的函数来抓取当前屏幕并将其保存到文件imwriteVideoWriter

imwrite

随着imwrite是很重要的,如果你想要写的多帧数据(无论是TIFF或GIF),你要使用的'WriteMode'参数,它只是设置为'append'让你将新框架添加到图像。在第一次通过循环你不要想追加,因为这将追加到现有的图像,可能已经存在。

的getFrame

至于getframe,它抓住指定数字的屏幕截图,并返回包含颜色表和RGB图像作为cdata一个结构。这是您想要写入视频或多帧图像的东西。

VideoWriter

用于写入视频,你将使用VideoWriter类的行为稍有不同。其主要步骤是:

  1. 创建对象

    vid = VideoWriter('filename.avi');

  2. 打开对象

    vid.open() % or open(vid)

  3. 使用写一些数据writeVideo

    vid.writeVideo(data)

  4. 关闭视频

    vid.close()

然后就可以调用writeVideo多次,只要你想每一次都将增加一个额外的框架。

摘要

这里是一个将所有的,它们一起并写入一个多帧TIFF以及一个AVI演示。

% Example data to plot 
x = linspace(0, 2*pi, 100); 
y = sin(x); 


% Set up the graphics objects 
fig = figure('Color', 'w'); 
hax = axes(); 
p = plot(NaN, NaN, 'r', 'Parent', hax, 'LineWidth', 2); 
set(hax, 'xlim', [0, 2*pi], 'ylim', [-1 1], 'FontSize', 15, 'LineWidth', 2); 

% Create a video object 
vid = VideoWriter('video.avi') 
vid.open() 

% Place to store the tiff 
tifname = 'image.tif'; 

for k = 1:numel(x) 
    set(p, 'XData', x(1:k), 'YData', y(1:k)); 

    % Grab the current screen 
    imdata = getframe(fig); 

    % Save the screen grab to a multi-frame tiff (using append!) 
    if k > 1 
     imwrite(imdata.cdata, tifname, 'WriteMode', 'append') 
    else 
     imwrite(imdata.cdata, tifname); 
    end 

    % Also write to an AVI 
    vid.writeVideo(imdata.cdata); 
end 

% Close the video 
vid.close() 

结果(如GIF动画)

enter image description here

+0

谢谢Suever它非常好!我只需要添加'压缩','无'来编写.tif。 (imdata.cdata,tifname,'Compression','none','WriteMode','append')。 – BenD