为什么我用 MatLab 编写的 .GIF 总是跳过第一个显示循环中的第二个图像帧?

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

考虑以下 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

每张图片显示立方体的一层B。我写了一些代码来制作一个 .gif 文件,以便于共享。我遇到的问题是:每次我打开 .gif 文件时,它都会在第一个循环显示时跳过第二帧(即与 B(:,:,2) 关联的帧)。本质上,.gif 按时间顺序显示以下帧:

1、3、4、1、2、3、4、1、2、3、4 等

这不是什么大问题,只是当我与他人分享一些结果时有点尴尬。我似乎找不到关于类似问题的任何话题(无论是在这里还是在 Matlab 的网站上),所以我很想知道您在使用上述代码制作 gif 时是否看到同样的问题,以及您是否愿意知道它起源于哪里。

仅供参考:我在 Windows 机器上使用 Matlab R2018a。

编辑:这是我创建的示例图像:

只是对未来读者评论的总结...

您可以在命令行中使用 ImageMgick 检查动画 GIF 中的延迟和细节,如下所示:

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

示例输出

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

此命令类似 - 在 Windows 上使用 FINDSTR 代替 grep:

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

如果您想调试动画 GIF 但速度太快而看不到,您可以重置所有计时 - 例如每帧 3 秒 - 如下所示:

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


请注意,某些应用程序无法正确显示 GIF 动画,因此请尝试在网络浏览器中使用 Open->File 进行检查。尝试 Chrome、Firefox、Opera、Safari 等


如果您真的无法将 GIF 传递给同事并难以理解,您可以用这样的动画制作卡通漫画:

magick input.gif -coalesce +append result.gif

或者,您可以像这样在网格上制作蒙太奇:

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