如何读取带有alpha通道的动态gif

15

在MATLAB中测试.gif动画时,我发现无法读取gif的透明度。

例如:

enter image description here

(gif的原始来源)

如果我执行以下操作

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

img是有冗余第三维的四维图像(很奇怪)。在挤压它(img=squeeze(img);)之后,如果我展示它(imshow(img(:,:,30),cmap)):

enter image description here

透明度消失了,使用图像中的另一种颜色作为背景,因此删除了特征。但是

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

返回一个空的alpha。显然,alpha的信息在图像中的某个地方,我如何在MATLAB中读取它?


1
我认为Matlab无法处理.gif图像的透明度。来自mathworks.com的信息:imread(___)还会返回图像的透明度。这种语法仅适用于PNG、CUR和ICO文件。 - obchardon
2
我想建议使用Imagemagick将GIF转换为PNG,并在循环中读取PNG。但是,我甚至无法让MATLAB识别PNG中的Alpha层,尽管它确实存在于文件中。 :-( - hbaderts
2
@Daniel,但是这很奇怪,因为.gif的第一帧给出了53个透明区域,而最后一帧则给出了0...没有一致性。因此,对于第一帧,您可以修改cmap。cmap(53:end,:) == 1;但对于最后一帧,它不起作用。 - obchardon
@obchardon:我注意到handle_positive_base_frame函数的行为,但我并不理解它。这就是为什么我在那个函数调用之前停止了,那里我明白数据仍然存在的原因。 - Daniel
1
通过这里的图像测试似乎表明Matlab要么存在漏洞,不支持某些功能,或者与大多数常见的GIF解析器有所不同。 - horchler
显示剩余5条评论
1个回答

9

/更新:我已经在MATLAB文件交换中心上提供了代码。 发布版本与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

1
两个问题:x = fullfile(matlabroot,'toolbox\matlab\imagesci','private','imgifinfo.m'); 应该修改为 x = fullfile(matlabroot,'toolbox','matlab','imagesci','private','imgifinfo.m'); 以便跨平台工作。 另外,您似乎在将图像数据索引到颜色映射中时出现了偏移量错误。 - horchler
1
你应该能够使用 info = iminfo(filename); 来获取每个帧的 TransparentColor 属性等细节,而不需要调用私有函数。 - horchler
改用 imfinfo - Daniel
1
使用uint8时,映射从索引1开始,但图像从索引0开始,存在偏移量为1。正确的代码是map(transparent+1,:)=1,请参见我的答案末尾的示例。 - Daniel
1
@Daniel 是的,那绝对就是了!感谢你的努力! - Ander Biguri
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接