MATLAB 制作gif动画

1、已有一系列时序图片的基础上,可借助matlab制作gif图片,虽然比不上专业绘图制作的精良,好在比较方便。

2、假定时序图片的命名为:dens000000.tiff,dens000500,dens0010000,……

3、效果展示

MATLAB 制作gif动画_第1张图片

4、代码呈现

% make gif
clc;clear;
close all;

% fileneme for gif
prefix = 'dens';
targetname = [ prefix '.gif' ];

% get information of the source file
file_info = dir( strcat( prefix, '*.tiff' ) );
file_num = length( file_info );
nameindex = zeros( 1, file_num );

for i = 1:file_num
    temp = strsplit( file_info(i).name, '.' );
    strformat = strcat( prefix, '%d' );
    temp = sscanf( temp{1,1}, strformat );
    nameindex(i) = temp;
end
nameindex = sort( nameindex );

% get the size of screen
set( 0, 'units', 'pixels' );
scnsize = get( 0, 'MonitorPosition' );

% get the size of figure
img = imread( file_info(1).name );

% creat figure
figure( 'units', 'pixels', 'WindowStyle', 'modal', 'color', 'w' )

% set the figure position at the center of screen
startx = floor( 0.5 * ( scnsize(3) - size(img,2) ) );
starty = floor( 0.5 * ( scnsize(4) - size(img,1) ) );
width = size( img, 2 );
high = size( img, 1 );
set( gcf, 'position', [ startx, starty, width, high ] );

% deal with the source file one by one
for i = 1:file_num  
    % read data
    strformat = strcat( prefix, '%06d.tiff');
    str = sprintf( strformat, nameindex(i) );
    img = imread( str );
    
    % open picture
    imshow( img, 'border', 'tight', 'initialmagnification', 'fit' )
    
    frame = getframe( gcf ); 
    im = frame2im( frame );
    [ I, map ] = rgb2ind( im, 1024 );
    
    % close current figure
    cla;
    
    % write file
    if i == 1
        imwrite( I, map, targetname, 'gif', 'Loopcount', inf, 'DelayTime', 0.05 );
    else
        imwrite( I, map, targetname, 'gif', 'WriteMode', 'append', 'DelayTime', 0.05 );
    end
end

% close figure
close all;

 

转载于:https://www.cnblogs.com/kljfdsa/p/10598672.html

你可能感兴趣的:(MATLAB 制作gif动画)