python opencv读取图像tif_如何在Python OpenCV中读取TIFF图像的Alpha通道?

在Python的OpenCV中尝试读取一个TIFF图像的Alpha通道时,发现使用cv2.imread只能获取到3通道的图像。通过设置cv2.IMREAD_UNCHANGED参数可以成功读取4通道图像,包括Alpha通道。读取后可以使用cv2.split分离通道,并访问Alpha层作为掩模。
摘要由CSDN通过智能技术生成

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.

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

What I have now is:

import cv2

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

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).

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

Thanks in advance!!

Nan

解决方案

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

yields (212,296,4) as expected.

If you then use

channels = cv2.split(img)

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值