2016-01-22 111 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