内容简介:代码日志版权声明:翻译自: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
像计算机科学家一样思考Python
(美)Allen B.Downey / 赵普明 / 人民邮电出版社 / 2013-8 / 49
《像计算机科学家一样思考python》按照培养读者像计算机科学家一样的思维方式的思路来教授python语言编程。全书贯穿的主体是如何思考、设计、开发的方法,而具体的编程语言,只是提供一个具体场景方便介绍的媒介。《像计算机科学家一样思考python》并不是一本介绍语言的书,而是一本介绍编程思想的书。和其他编程设计语言书籍不同,它不拘泥于语言细节,而是尝试从初学者的角度出发,用生动的示例和丰富的练习来......一起来看看 《像计算机科学家一样思考Python》 这本书的介绍吧!