在OpenFOAM计算中,paraFoam生成动画时,产生一系列图片,用于后期合成动画。起初采用Ulead GIF Animator 5,但是当需要调整,或按照一定规则挑出一些图片进行合成时,就很费时费力,况且我安装了一个没有破解,卸载时还不痛快,所以就采用matlab就来解决这个问题。
% write the picto Gif
% 2019-03-04
clear;closeall;clc;
filename='prescribedMotionSST_0.gif'; %gif文件名
filePath='H:\MATLAB_Work\GifMake';
namelist =dir(strcat(filePath,'\pic\*.png'));
nn=floor(length(namelist)/10);
for i=1%:nn
str=sprintf('pic\\prescribedMotionSST_0.%04d.png',10*(i-1));
Img=imread(str);%Read image from graphicsfile
imshow(Img);%Display image
frame=getframe(i);%Capture axes or figureas movie frame
im=frame2im(frame);%Return image dataassociated with movie frame, indexed image data X and associated colormap Map
[Idx,map]=rgb2ind(im,256);%Convert RGBimage to indexed image
imwrite(Idx,map,filename,'gif','Loopcount',inf,...
'DelayTime',0.1);%Number of timesto repeat the animation, A LoopCount value of Inf causes the animation tocontinuously loop.
imwrite(I,map,filename,'gif','WriteMode','append',...
'DelayTime',0.1);% imwrite adds asingle frame to the existing file,layTime用于设置gif文件的播放快慢
总思路:读图像-获取帧-帧转换成图像数据(RGB)-RGB转换成indexed图像数据-写Gif。
另外,DelayTime也会影响Gif文件大小。