zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    句法分析树标注集
    LaTeX入门教程(二)
    LaTeX入门教程(一)
    汉语词性对照表[北大标准/中科院标准]
    Python版C语言词法分析器
    QT5.4 计算器程序 打包&发布,解决dll的最新解决方案
    解决Android SDK Manager更新(一个更新Host的程序的原理实现和源码)
    增加个人博客地址,欢迎访问
    Matlab R2012b启动出现License Manager Error -15
    C++中二维数组的动态创建与处理
  • 原文地址:https://www.cnblogs.com/chuqianyu/p/13438463.html
Copyright © 2011-2022 走看看