内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/34970064/how-to-read-an-animated-gif-with-alpha-channel
在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中读取它?
提供了代码.已发布的版本与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
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/34970064/how-to-read-an-animated-gif-with-alpha-channel
以上所述就是小编给大家介绍的《图像 – 如何使用Alpha通道读取动画GIF》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to the Design and Analysis of Algorithms
Anany Levitin / Addison Wesley / 2011-10-10 / USD 117.00
Based on a new classification of algorithm design techniques and a clear delineation of analysis methods, Introduction to the Design and Analysis of Algorithms presents the subject in a coherent a......一起来看看 《Introduction to the Design and Analysis of Algorithms》 这本书的介绍吧!