【问题标题】:Matlab: how to save TIFF with transparency or PNG with no compression?Matlab:如何保存透明的 TIFF 或不压缩的 PNG?
【发布时间】:2012-11-19 13:15:59
【问题描述】:

我必须在 Matlab 中处理大量图像并将结果保存到具有透明度的图像文件中。但是PNG压缩对我来说太费时间了。如何保存不压缩的 PNG 或透明的 TIFF?有没有其他方法可以在不压缩和透明的情况下保存图像?

这是我的第一个问题,如果有任何问题,请原谅我的英语不好和错误的问题风格。

【问题讨论】:

  • "压缩对我来说太长了" 这是什么意思?你的意思是这需要太多时间?
  • @leonboy 是的,当然,这意味着“需要太多时间”,谢谢(已修复)

标签: matlab compression png transparency tiff


【解决方案1】:

使用 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!

【讨论】:

    【解决方案2】:

    Matlab 的imwrite 没有PNG 压缩级别的参数。如果是这样,您可以将其设置为零以不进行压缩。虽然对于 TIFF,它确实有 Compressionnone 选项,但没有 alpha 通道。您可以写入带有 Alpha 通道且无压缩的旧 Sun Raster (RAS) 格式。虽然没有什么可能能够阅读它。

    【讨论】:

    • 使用 TIFF 类(但显然不使用 imwrite),您可以指定有一个 alpha 通道。
    【解决方案3】:

    “PNG 没有未压缩的变体。仅使用未压缩的 deflate 块即可存储未压缩的数据”

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

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

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 1970-01-01
      • 2023-03-08
      • 2011-06-11
      • 2011-10-20
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多