How to make a gif of an already animated figure

2 次查看(过去 30 天)
Hello,
I have been looking around for a bit but I cannot seem to find a solution which works for me.
So a colleague of mine send me a code which explains a proposal he made. This explaination is done by updating a figure using a loop, thus animating it. I would like to save this as animation as a .gif file, but I cannot get it to work. As soon as I open the file, I see the first frame and then the gif turns black.
So this is the code:
clear all
close all
C=imread('belletje.tif');
C=double(C);
%[x,y]=ndgrid(1:128);
%[X,Y]=ndgrid(28+(1:100));
% cut image to 100 by 100 pixels
Ctx=C(29:128,29:128);
clear x y X Y
Cmax=max(Ctx(:));
Cmin=min(log10(Ctx(:)/Cmax));
figure(1)
image(255*(1-log10(Ctx/Cmax)/Cmin))
axis equal
colormap bone(256)
% Ftx is the 2D Fourier transform of the original image Ctx
Ftx=fftshift(fft2(Ctx));
Fmax=max(20*log10(abs(Ftx(:)+1)));
Fscale=255/Fmax;
figure(2)
image(Fscale*20*log10(abs(Ftx+1)))
axis equal
colormap bone(256)
% Acc1-3 are the resulting (integrated) CCD images of the 3 succesive camera's
Acc1=zeros(size(Ctx));
Acc2=zeros(size(Ctx));
Acc3=zeros(size(Ctx));
% per timestep Ftx shifts along the 3 camera postions with aperture stops
% in between. The sweep is performed from the bottom to the top.
for timestep=1:460 % in ns at 10 Mfps
% LB is the total plane in front of the lens bank
% 3 x (100x100) plus aperture stops of 3 x (20x100)
LB=zeros(360,100);
% in principle 1 tot 100
Start_Row_Index=1;
End_Row_Index=100;
% exception at begin and end
if timestep < 100
End_Row_Index=timestep;
elseif timestep > 360
Start_Row_Index=timestep-359;
end
% let Ftx shift over the entrance of the lens bank
Row_Index=Start_Row_Index:End_Row_Index;
LB(360-timestep+Row_Index,:)=Ftx(Row_Index,:);
% devide LB over 3 camera channels
N1=LB(241:340,:);
N2=LB(121:220,:);
N3=LB(1:100,:);
% get image from each Fourier transform
B_blind=zeros(20,100); % the aperture stop
B1=abs(ifft2(N1));
B2=abs(ifft2(N2));
B3=abs(ifft2(N3));
% assemble the total image at the image plane (the CCD camera's)
B=cat(1,B3,B_blind,B2,B_blind,B1, B_blind);
% determine the integrated image on the CCD's per frame
Acc1=Acc1+B1/100;
Acc2=Acc2+B2/100;
Acc3=Acc3+B3/100;
% image on the CCD's
Acc=cat(1,Acc3,B_blind,Acc2,B_blind,Acc1,B_blind);
figure(3)
% plot
% left row: sweep of the Fourier transform
% middle row: resulting image on the plane of the CCD camera's
% right row: integrated pixel values on the CCD's
subplot(1,3,1)
image(Fscale*20*log10(abs(LB)+1))
axis equal
axis([0 100 0 360])
subplot(1,3,2)
image(B)
axis equal
axis([0 100 0 360])
subplot(1,3,3)
image(Acc)
axis equal
axis([0 100 0 360])
colormap bone(256)
drawnow
pause(.1)
end
% normalization of the images in column 3
subplot(1,3,3)
AccMax=max(Acc(:));
AccMin=min(Acc(:));
image(255*(Acc/AccMax))
axis equal
axis([0 100 0 360])
colormap bone(256)
The code requires the following image
I tried adding
f = getframe(figure(3));
[im,map] = rgb2ind(f.cdata,256,'nodither');;
im(:,:,1,timestep) = rgb2ind(f.cdata,map,'nodither');
in the loop followed by
imwrite(im,map,'Fourier.gif','gif','LoopCount',Inf,'DelayTime',1);
but this gave me the one-frame followed by black gif
I want the gif to be an animated image of the full figure with timestep (from 1 to 460).
Could anyone help me to a solution?
Thanks, Maarten

采纳的回答

Mike Garrity
Mike Garrity 2016-1-21
You also need WriteMode=append or the imwrite keeps creating a new file.
See the example at the end of this blog post .
  1 个评论
Adam Danz
Adam Danz 2022-3-23
Starting in Matlab R2022a, another option is to use exportgraphics with append=true. See this Community Highlight for an example.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by