Can imread input animated gifs written using imwrite?

조회 수: 7 (최근 30일)
Alec Jacobson
Alec Jacobson 2017년 6월 14일
댓글: DGM 2022년 2월 12일
I've written an animated gif test.gif using imwrite:
sphere
frame = getframe(gcf);
[SIf,cm] = rgb2ind(frame.cdata,256);
imwrite(SIf,cm,'test.gif','Loop',Inf,'Delay',0.5);
view(-37.5,40);
frame = getframe(gcf);
[SIf,cm] = rgb2ind(frame.cdata,256);
imwrite(SIf,cm, 'test.gif','WriteMode','append','Delay',0.5);
If I try to read this back in using
[X,M] = imread('test.gif');
I see that X has 4 dimensions and the size of the fourth dimension is 2 (good, one for each frame). The colormap M has only 2 dimensions #colors by 3.
Plotting the first frame I see the correct image:
imshow(X(:,:,:,1),M)
Plotting the second frame I see nonsense:
imshow(X(:,:,:,2),M)
My guess is that the colormap is wrong/shouldn't be the same for every frame of the animation. Any idea what's going on here? The documentation for imread suggests that it should be able to handle animated gifs.

답변 (1개)

Alec Jacobson
Alec Jacobson 2017년 6월 14일
It seems like this is broken ... on purpose. In
/Applications/MATLAB_R2017a.app/toolbox/matlab/imagesci/private/readgif.m
There's a commented line:
% This only returns the color table for the first frame in the GIF file.
% There is an enhancement (g1055265) to make this return all color tables.
map = info.ColorTable;
I'm not sure why the map is set only to the first frame's colormap. If you're willing to modify built in matlab files then you can "fix" this so that map is a #colors by 3 by #frames matrix then change this line to:
map = reshape([info(:).ColorTable],[],3,n);
  댓글 수: 2
Shaw Connor
Shaw Connor 2019년 12월 15일
%There is an enhancement (g1055265) to make this return all color tables.
sry to interupt ,but where can i get this enhancement
DGM
DGM 2022년 2월 12일
There is no enhancement. Depending on your version, imread() can no longer correctly read multiframe GIF files.
The legacy method for reading multiframe GIF files is to get the image data from imread() and get the color tables from imfinfo(). If that's too inconvenient, there are tools on the FEX to streamline the process. I'm not going to link to them, because I doubt anyone is still using R2018a or older. If you are, you can find them.
If you're running R2018b or newer, depending on the specific version and the image at hand, imread() and imfinfo() will either give you subtly corrupted images, grossly corrupted images, or it will not even give you a complete image at all.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by