Matlab:如何保存具有透明度的TIFF或无压缩的PNG?

4

我需要在Matlab中处理大量图像,并将结果保存为具有透明度的图像文件。但PNG压缩对我来说时间太长了。如何保存没有压缩或具有透明度的TIFF格式的PNG?还有其他方法可以保存不带压缩和透明度的图像吗?

这是我在这里的第一个问题,如果问题中有任何错误,请原谅我的糟糕英语和错误的提问方式。


“compression is too long for me” 这句话是什么意思?你的意思是它花费了太多时间吗? - leonbloy
@leonboy 是的,那意味着“花费太多时间”,当然,谢谢(已修复) - Daniil Sizov
3个回答

4
使用Matlab中的TIFF类,您可以编写带有透明度的TIFF文件:
%# create a synthetic RGBA image
ch = checkerboard(100);
rgba = repmat(ch,[1,1,4]);
rgba(:,:,4) = rgba(:,:,4)==0;
rgba = uint8(round(rgba*255));

%# create a tiff object
tob = Tiff('test.tif','w');

%# you need to set Photometric before Compression
tob.setTag('Photometric',Tiff.Photometric.RGB)
tob.setTag('Compression',Tiff.Compression.None)

%# tell the program that channel 4 is alpha
tob.setTag('ExtraSamples',Tiff.ExtraSamples.AssociatedAlpha)

%# set additional tags (you may want to use the structure
%# version of this for convenience)
tob.setTag('ImageLength',size(ch,1));
tob.setTag('ImageWidth',size(ch,2));
tob.setTag('BitsPerSample',8);
tob.setTag('RowsPerStrip',16);
tob.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
tob.setTag('Software','MATLAB')
tob.setTag('SamplesPerPixel',4);

%# write and close the file
tob.write(rgba)
tob.close

%# open in Photoshop - see transparency!

1

Matlab的imwrite没有PNG压缩级别的参数。如果有的话,您可以将其设置为零以进行无压缩。虽然对于TIFF,它确实具有Compressionnone选项,但没有alpha通道。您可以将其写入带有alpha通道且不压缩的旧Sun Raster(RAS)格式中。尽管很可能没有任何东西能够读取它。


使用TIFF类(但显然不使用imwrite),您可以指定存在alpha通道。 - Jonas

0

"PNG没有未压缩的变种。通过仅使用未压缩的deflate块,可以存储未压缩数据"

未压缩的deflate块使用一个5字节的头部+每个块最多65535字节的未压缩数据。

http://www.w3.org/TR/PNG-Rationale.html


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