OpenCV中读取TIFF图像的Alpha通道

OpenCV中读取TIFF图像的Alpha通道

本文介绍了如何在Python OpenCV中读取TIFF图像的Alpha通道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python OpenCV从tiff图像中读取alpha通道。我正在使用带有OpenCV 2.4.5-3模块的Enthought Canopy。

I want to read the alpha channel from a tiff image using Python OpenCV. I am using Enthought Canopy with OpenCV 2.4.5-3 module.

我遵循了OpenCV网站的教程,使用的是cv2.imread,但似乎不起作用。

I followed the OpenCV website's tutorial using cv2.imread, but it doesn't seem to work.

我现在拥有的是:

import cv2
image = cv2.imread('image.tif', -1)

然后我用了: print(image.shape),它仍显示(8192,8192,3)。但是我用Matlab读取相同的图像,我可以看到该图像的尺寸为(8192,8192,4)。

Then I used: print (image.shape), it still shows the (8192, 8192, 3). But I used Matlab to read the same image, I can see the dimension of this image is (8192, 8192, 4).

我不确定应该怎么做才能读取此图像的Alpha通道。

I am not sure what should I do to read the alpha channel of this image.

谢谢!!
Nan

Thanks in advance!!Nan

推荐答案

这是一个老问题,但以防万一有人偶然发现它:if img.tiff是4通道TIFF,则

This is an old question, but just in case someone else stumbles on it: if img.tiff is a 4-channel TIFF, then

import cv2
img = cv2.imread('img.tiff', cv2.IMREAD_UNCHANGED)
print img.shape

产量(212,296, 4 )。

yields (212,296,4) as expected.

如果您随后使用

channels = cv2.split(img)

您可以引用alpha层( channel [3] )-例如,作为遮罩。

you can reference the alpha layer (channels[3]) - for instance, as a mask.

这个想法来自巧妙地使用假层和合并可以恢复单个RGB图层的实际颜色。

The idea for this was taken from How do I use Gimp / OpenCV Color to separate images into coloured RGB layers? which cleverly uses a fake layer and merge to enable recovery of the individual RGB layers in their actual colours.

这篇关于如何在Python OpenCV中读取TIFF图像的Alpha通道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:51