在MATLAB中使用.gif动画进行一些测试时,我意识到我无法读取gif的透明度.
例:
如果我做
[img,cmap]=imread('Finnandjake.gif');
img是4D,具有冗余的第三维(奇怪).挤压它(img = squeeze(img);),如果我显示它(imshow(img(:,:,30),cmap)):
透明度消失,使用图像中的另一种颜色作为背景,从而删除功能.然而
[img,cmap,alpha]=imread('Finnandjake.gif');
返回一个空的alpha.显然,Alpha的信息在图像的某处,我如何在MATLAB中读取它?
解决方法
/更新:我在
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,width,height,[]); if imageix == 1 stack=zeros(height,1,numframes,'uint8'); end %row major vs column major fix stack(:,:,imageix) = reshape(data2,[width height]).';%' end str.close(); end
[stack,transparent]=transparentGifRead('tr.gif'); map(transparent+1,:)=[0,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