Matlab如何制作和保存gif動圖


  很多時候我們並不只是想在Matlab中把圖動態顯示一下給自己看,而是需要將它保存為gif(為什么是gif呢?因為其小巧方便,容易嵌入到其他東西中),從而用以匯報說明,或者其他用途。
  之前很長一段時間,我嫌matlab保存gif要改動的代碼比較多(Mathematica只要在Animate時,將Animate改成Export就直接出圖了),都是利用gif動畫錄制工具(推薦靈者Gif錄制),在matlab圖在跑的時候,給它錄成gif。
后來,需要的從matlab中搞出來的gif越來越多了,深感錄制方法的瑣碎,便又想到了利用Matlab自身函數,將參數遞變繪制的多張圖,保存為gif動圖。
  一個簡潔而基本的框架如下:

 1 clc
 2 clear
 3 pic_num = 1;
 4 for ***************************
 5     plot(fig(i));%以上代碼為畫圖代碼,以下代碼為保存gif的代碼
 6   
 7     drawnow;%畫gif動圖的時候這個外面的大for循環就存在,直接把以下和for*******以上的代碼放到對應的位置即可
 8     F=getframe(gcf);
 9     I=frame2im(F);
10     [I,map]=rgb2ind(I,256);
11 
12     if pic_num == 1
13         imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.2);
14     else
15         imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
16     end
17 
18     pic_num = pic_num + 1;
19 
20 end  % 這里imwrite寫入時,要保證原來已經有一個文件存在,才能用“append”參數,所以設置了一個pic_num,讓一張圖直接保存,后面用“append”。

  舉個例子:

 1 clc
 2 clear all;
 3 tic
 4 pic_num = 1;
 5 A = 10;
 6 B = 0.5*A;
 7 t = 0:0.01:2;
 8 x = 0:0.1:40;
 9 omega = 10*pi;
10 k = pi/4;
11 for m = 1:length(x)
12     y1 = A*cos(k*x(m) - omega*t);
13     y2 = B*cos(k*x(m) + 0.3*omega*t);
14     figure(2)
15     plot(t,y1+y2);
16     xlabel('t')
17     ylabel('y')
18     title('Freezed time figure')
19     ylim([-(A+B+1),A+B+1])
20     pause(0.2);
21     
22     drawnow;
23     F=getframe(gcf);
24     I=frame2im(F);
25     [I,map]=rgb2ind(I,256);
26     if pic_num == 1
27         imwrite(I,map,'test1.gif','gif','Loopcount',inf,'DelayTime',0.2);
28     else
29         imwrite(I,map,'test1.gif','gif','WriteMode','append','DelayTime',0.2);
30     end
31     pic_num = pic_num + 1;
32 end
33 toc

 

 1 x = 0:0.01:1;
 2 n = 3;
 3 y = x.^n;
 4 plot(x,y,'LineWidth',3)
 5 title(['y = x^n,  n = ' num2str(n) ])
 6 n = 1:0.5:5;
 7 nImages = length(n);
 8 
 9 fig = figure;
10 for idx = 1:nImages
11     y = x.^n(idx);
12     plot(x,y,'LineWidth',3)
13     title(['y = x^n,  n = ' num2str( n(idx)) ])
14     drawnow
15     frame = getframe(fig);
16     im{idx} = frame2im(frame);
17 end
18 close;
19 %%
20 figure;
21 for idx = 1:nImages
22     subplot(3,3,idx)
23     imshow(im{idx});
24 end
25 %%
26 filename = 'testAnimated.gif'; % Specify the output file name
27 for idx = 1:nImages
28     [A,map] = rgb2ind(im{idx},256);
29     if idx == 1
30         imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',1);
31     else
32         imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1);
33     end
34 end

 

關於MATLAB中的imwrite的用法

imwrite(imind,cm,'gif','comet2.gif', 'Loopcount',inf,'DelayTime',0.1);
這句代碼每一個逗號都到底什么意思
能詳細解釋一下嗎
inmind,cm,應該是bai你輸出du圖片之前定義的[inmind,cm]吧。輸出的圖片形式為gif圖片,名字是comet2.gif。 loopcount,inf 可以讓這個gif文件zhi一直播放。delaytime是播dao放時間控制。

參考網址:(Matlab如何制作和保存gif動圖https://blog.csdn.net/lusongno1/article/details/78632457

     (matlab生成gif動圖)https://blog.csdn.net/hezhongla0811/article/details/86368363?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

                  (matlab drawnow命令 刷新屏幕)https://blog.csdn.net/qq278672818/article/details/62038599

      (關於MATLAB中的imwrite的用法)https://zhidao.baidu.com/question/1370259502588323739.html

     (imwrite將圖像寫入圖形文件)https://ww2.mathworks.cn/help/matlab/ref/imwrite.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM