【问题标题】:How to read a tif image with transparent pixels in opencv如何在opencv中读取具有透明像素的tif图像
【发布时间】:2014-04-05 21:47:53
【问题描述】:

我有一个具有透明像素的 tif 图像(我可以在paint.net 中将它们视为透明像素)。

我正在尝试将它们读入 openCV 中的 Mat 并且我正在使用以下代码:

Mat image=imread(imagePathname,CV_LOAD_IMAGE_UNCHANGED);
auto x=image.channels();

根据我的理解,由于输入图像具有透明度,因此 channels() 应该返回 4,但它返回 3。

如何读取具有透明像素的 tif 图像并检查像素在 opencv 中是否透明?

编辑1

imagemagick 运行结果:

Image: layer0003.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 10000x5000+0+0
  Resolution: 150x150
  Print size: 66.6667x33.3333
  Units: PixelsPerInch
  Type: TrueColorAlpha
  Base type: TrueColor
  Endianess: MSB
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
    alpha: 1-bit
  Channel statistics:
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 23.6472 (0.0927342)
      standard deviation: 37.6851 (0.147785)
      kurtosis: 8.93054
      skewness: 2.28009
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 22.8353 (0.0895504)
      standard deviation: 37.6516 (0.147653)
      kurtosis: 10.4255
      skewness: 2.52881
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 22.798 (0.0894041)
      standard deviation: 37.6575 (0.147677)
      kurtosis: 10.9059
      skewness: 2.58999
    Alpha:
      min: 0 (0)
      max: 255 (1)
      mean: 89.055 (0.349235)
      standard deviation: 121.566 (0.476728)
      kurtosis: -1.59995
      skewness: -0.632496
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 58.8064 (0.230613)
      standard deviation: 68.9821 (0.270518)
      kurtosis: 8.35337
      skewness: 3.53852
  Alpha: none   #00000000
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgba(223,223,223,1)
  Matte color: grey74
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 10000x5000+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: LZW
  Orientation: TopLeft
  Properties:
    date:create: 2014-03-01T13:11:12+00:00
    date:modify: 2014-02-28T17:48:41+00:00
    signature: dfa3e35c35345ef3440ff15d15ad37222f9cf0376bed7b7710dd95f4e537e210
    tiff:alpha: unassociated
    tiff:endian: lsb
    tiff:photometric: RGB
    tiff:rows-per-strip: 1
    tiff:timestamp: 2014:02:28 17:48:38
    xmp:CreatorTool: Microsoft Windows Live Photo Gallery 15.4.3555.308
  Profiles:
    Profile-xmp: 12702 bytes
  Artifacts:
    filename: layer0003.tif
    verbose: true
  Tainted: False
  Filesize: 20.42MB
  Number pixels: 50M
  Pixels per second: 60.24MB
  User time: 0.827u
  Elapsed time: 0:01.829
  Version: ImageMagick 6.8.8-7 Q16 x64 2014-02-13 http://www.imagemagick.org

【问题讨论】:

  • 尝试使用 ImageMagick 并运行“ide​​ntify -verbose yourimage.tif”来查看它是否具有 Alpha 通道。 imagemagick.org/script/identify.php
  • @MarkSetchell:结果显示它有 alpha 通道(见我编辑的问题),但它只有一位深度。重要吗?
  • 你能张贴你的图片吗?
  • @Haris:是的,我可以做到,但是当我尝试这样做时,它转换为 bmp 失去了它的 alpha。有什么方法可以将其作为附件而不是图像发布?这是我前几天在此论坛上发布的示例图片:stackoverflow.com/questions/22119246/…loo 用于 tif 图片和问题结束。
  • 我怀疑 OpenCV 需要 8 位 alpha 通道,而不是 1 位。也许您可以使用 ImageMagick 提取 alpha 通道并通过乘以 255 对其进行缩放。您可以尝试使用此命令查看 alpha 通道的直方图... convert image.tif -alpha extract -format "%c" histogram:info:

标签: c++ image opencv image-processing


【解决方案1】:

我只是有时间重新审视这个:-)

基本上,我认为问题在于 ImageMagick 在编写其输出文件时将采用最经济的文件大小。因此,如果您的图像基本上是灰度的,但 alpha(不透明度)通道仅为 0 或 1,ImageMagick 将使用 16 位灰度的数据和一位 alpha 通道对其进行编码。我猜这超出了 OpenCV,因为它似乎期望 alpha 以与数据相同的深度进行编码。

所以,问题就变成了...... “如何强制组合所需的数据/alpha(均为 8 位,而不是 8 位加 1 位)灰度/彩色(8 位灰度还是 8 位颜色)?”

可能还有其他方法,但就目前而言,我可以设想在图像底部添加一行来强制 ImageMagick 的手,并且希望一旦它在 OpenCV 中为您完成了技巧,您就可以轻松地将其移除。

所以,让我们创建一个 16 位灰度图像,其中包含一位 alpha,它在中间形成一个方形透明孔:

convert -size 300x300 gradient:black-white -alpha set -region 100x100+100+100 -alpha transparent image.tif

让我们检查一下我们有什么:

identify -verbose image.tif | head -14

Image: image.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 300x300+0+0
  Units: PixelsPerInch
  Type: GrayscaleAlpha
  Base type: Grayscale
  Endianess: LSB
  Colorspace: Gray
  Depth: 16-bit
  Channel depth:
    gray: 16-bit          <--- 16-bit greyscale data
    alpha: 1-bit          <--- 1-bit alpha

现在让我们将其强制为 8 位 alpha 和 8 位数据:

# Force to at least 8-bit greyscale (but maybe RGB depending on rest of image) plus 8-bit alpha...
# ... by adding a line across the bottom with alpha varying from 0 to 1 (i=x coordinate,w=width)
convert image.tif                                \
   \( +clone -resize x1! -channel A -fx "i/w" \) \
   -append -depth 8 result.tif

让我们检查一下它是否有效:

identify -verbose result.tif | head -16

Image: result.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 300x301+0+0      <--- one extra row
  Units: PixelsPerInch
  Type: GrayscaleAlpha
  Base type: Grayscale
  Endianess: LSB
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 8-bit              <--- 8-bit data
    alpha: 8-bit             <--- 8-bit alpha

现在让我们强制 8 位 RGB 加上 8 位 alpha:

# Force to 8-bit RGB plus 8-bit alpha...
# ...  by adding a coloured line across the bottom with alpha varying from 0 to 1 (i=x coordinate,w=width)
convert image.tif                                      \
   \( +clone -resize x1! -channel RGBA -fx "rand()" \) \
   -append -depth 8 result.tif

让我们再检查一次:

Image: result.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 300x301+0+0       <--- 1 extra row
  Units: PixelsPerInch
  Type: TrueColorAlpha
  Base type: TrueColor        <--- Truecolour
  Endianess: LSB
  Colorspace: sRGB            <--- RGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit                <--- 8-bit red
    green: 8-bit              <--- 8-bit green
    blue: 8-bit               <--- 8-bit blue
    alpha: 8-bit              <--- 8-bit alpha

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-13
    • 2014-01-17
    • 2010-12-08
    • 2011-03-02
    • 2014-12-05
    • 1970-01-01
    • 2016-07-13
    • 2014-06-01
    相关资源
    最近更新 更多