如何阅读带alpha通道的动画gif

时间:2016-01-23 23:08:39

标签: image matlab gif alpha

在MATLAB中使用.gif动画进行一些测试时,我意识到我无法读取gif的透明度。

示例:

enter image description here

(Original source of the gif)

如果我这样做

[img,cmap]=imread('Finnandjake.gif');

img是4D,具有冗余的第三维(很奇怪)。压缩后(img=squeeze(img);),如果我显示它(imshow(img(:,:,30),cmap)):

enter image description here

透明度消失了,使用图像中的另一种颜色作为背景,从而删除了特征。然而

[img,cmap,alpha]=imread('Finnandjake.gif');

返回空alpha。显然alpha的信息在图像的某处,我怎样才能在MATLAB中读取它?

1 个答案:

答案 0 :(得分:9)

/更新:我在MATLAB file exchange处提供了代码。已发布的版本与OCTAVE兼容,并附带一些文档。

我提出了这个解决方案。返回参数是堆叠图像,色彩映射和与透明度对应的索引。

%do not use, instead use: http://www.mathworks.com/matlabcentral/fileexchange/55693-transparentgifread-filename-
function [stack,map,transparent]=transparentGifRead(filename)
if ~exist(filename,'file')
    error('file %s does not exist',filename);
end
info=imfinfo(filename);
%Check if color map for all frames is the same
if any(any(any(diff(cat(3,info.ColorTable),[],3))))
    error('inconsistent color map')
else
    map=info(1).ColorTable;
end
%Check if transparent color for all frames is the same
if any(diff([info.TransparentColor]))
    error('inconsistent transparency information')
else
    transparent=info(1).TransparentColor-1;
end
import java.io.*
str = javax.imageio.ImageIO.createImageInputStream(java.io.File(filename));
t = javax.imageio.ImageIO.getImageReaders(str);
reader = t.next();
reader.setInput(str);
numframes = reader.getNumImages(true);
for imageix = 1:numframes
    data = reader.read(imageix-1).getData();
    height = data.getHeight();
    width = data.getWidth();
    data2 = reader.read(imageix-1).getData().getPixels(0,0,width,height,[]);
    if imageix == 1
        stack=zeros(height,width,1,numframes,'uint8');
    end
    %row major vs column major fix
    stack(:,:,1,imageix) = reshape(data2,[width height]).';%'
end
str.close();
end

一些演示代码,用于将透明像素着色为绿色:

[stack,map,transparent]=transparentGifRead('tr.gif');
map(transparent+1,:)=[0,1,0] %offset 1 because uint8 starts at 0 but indices at 1
for frame=1:size(stack,ndims(stack))
    imshow(stack(:,:,frame),map);
    pause(1/25);
end