Read animated gif and convert to rgb frames

Alec Jacobson

June 14, 2017

weblog/

Matlab's built in imread (as of 2017) doesn't load animated gifs correctly. You can fix this by changing the line:

map = info.ColorTable;

in /Applications/MATLAB_R2017a.app/toolbox/matlab/imagesci/private/readgif.m with

map = reshape([info(:).ColorTable],[],3,n);

For a single frame indexed image you can use ind2rgb to convert to an rgb image. To do this on entire animated gif. You can use an arrayfun for-loop hack:

[X,M] = imread('input.gif');
Y = cell2mat(permute(arrayfun(@(C) ind2rgb(X(:,:,:,C),M(:,:,C)),1:size(X,4),'UniformOutput',false),[1 4 3 2]));

Update:

Use this instead:

map = zeros(0,3,n);
for j = 1:n
  map(1:size(info(j).ColorTable,1),:,j) = info(j).ColorTable;
end