Search code examples
matlabanimated-gif

Why do my .GIFs written with MatLab always skip the second image frame in the first display loop?


Consider the following MCVE:

B = 50 - randi(100,100,100,4);

% Show each of the 4 layers of A for 0.50 seconds each, and save image frames:
fig=figure();
for idx = 1:size(B,3)
    imagesc( B(:,:,idx) ); title(num2str(idx)); caxis([-50 50]); drawnow; 
    frame = getframe(fig);
    img{idx} = frame2im(frame);
    pause(0.50);
end

% Write a .gif file, show each image 1 second in infinite loop.
filename = 'whatsgoingon.gif'; dlyt = 1;
for idx=1:length(img)
    [A,map]=rgb2ind(img{idx},256);
    if idx==1;  imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',dlyt);
    else;       imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',dlyt);
    end
end

Each image shows a layer of the cube B. I wrote some code to make a .gif file out of this to make it easier to share. The problem I have with that is: each time I open the .gif file, it will skip the second frame (i.e. the one associated with B(:,:,2)) on the first loop of showing. Essentially, the .gif shows the following frames in chronological order:

1, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, etc.

This is not a huge problem, just a bit embarassing when I am sharing some results with others. I can't seem to find any topic on a similar issue (neither here nor on Matlab's website), so I would be very curious to hear if you see the same issue when you make a gif using the above code, and if you would have any idea where it originates.

FYI: I am using Matlab R2018a on a Windows machine.

EDIT: here is an example image I created:

here's a gif I create in this manner:


Solution

  • Just a summary of the comments for future readers...

    You can check the delays and details in an animated GIF with ImageMgick on the command line like this:

    magick identify -format "%f[%s] %T\n" matlab.gif 
    

    Sample Output

    matlab.gif[0] 100      <--- this frame has a 100 centisecond delay
    matlab.gif[1] 100
    matlab.gif[2] 100
    matlab.gif[3] 100
    

    This command is similar - use FINDSTR on Windows in place of grep:

    magick identify -verbose matlab.gif | grep Delay
    Delay: 100x100
    Delay: 100x100
    Delay: 100x100
    Delay: 100x100
    

    If you want to debug an animated GIF but it is too fast to see, you can reset all the timings - say to 3s per frame - like this:

    magick input.gif -coalesce -set delay 300 slooooow.gif
    

    enter image description here


    Note that some applications display animated GIFs incorrectly, so try using Open->File in a web-browser to check. Try Chrome, Firefox, Opera, Safari etc.


    If you are really having problems passing GIFs to colleagues and getting understood, you can make a cartoon-strip out of an animation like this:

    magick input.gif -coalesce +append result.gif
    

    enter image description here

    Or, you can make a montage on a grid like this:

    magick input.gif -coalesce miff:- | magick montage -geometry +10+10 -  result.gif
    

    enter image description here