11

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

Nan An
  • 633
  • 3
  • 7
  • 11
  • Not sure if following answer to ["Alpha channel in OpenCV"](http://stackoverflow.com/q/1451021/2419207) is still relevant, but it may be: http://stackoverflow.com/a/2111434/2419207 – iljau Feb 09 '14 at 00:39
  • Thanks, iljau! If the same image is PNG file, I can read it using cv2.imread and I can see there are 4 layers. But if the format is TIFF which has the alpha channel (Matlab can show it), it only shows 3 layers. I am wondering why?? – Nan An Feb 10 '14 at 21:56
  • Not sure if it's related, but: ["OpenCV issue: alpha channel support for 8-bit tiffs (Patch #2791)"](http://code.opencv.org/issues/2791) – iljau Feb 10 '14 at 22:17
  • And not an answer to the question, but I have found .. https://github.com/luispedro/imread .. to be most the convenient method of converting images to numpy arrays. – iljau Feb 10 '14 at 22:21
  • Or [`scipy.ndimage.imread`](http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.imread.html#scipy.ndimage.imread), which uses [`PIL`](http://effbot.org/imagingbook/pil-index.htm) under covers. – iljau Feb 10 '14 at 22:30

3 Answers3

16

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.

Community
  • 1
  • 1
GT.
  • 1,140
  • 13
  • 20
  • 1
    Tried the flag, still got 3 channels. My cv2 version is 2.4.9. Maybe it depends on cv2 build flags? Tiff image was generated with Photoshop by adding channel "Alpha" to "Channels" tab. No transparency or layers were used, just background layer. Maybe it matters too. – ogurets Feb 06 '17 at 06:30
  • Does Photoshop save the image as RGBA (i.e., specifically registering the layer named 'Alpha' as an actual alpha layer)? It would make sense if it didn't, since not every 4-channel image is RGBA. I often get images that have 4 layers (one named 'Alpha'), but need to be explicitly 'converted' to RGBA. In https://gis.stackexchange.com/questions/229758/gdal-python-api-warp-and-translate-struggling-with-rgba I had this issue, and wanted to avoid a 'convert to RGBA' step (takes ~60ms/image, but there are ~3m images... so it adds **stupid** amounts of time). – GT. May 05 '17 at 22:23
  • I don't have this option in CS4 (Image->Mode has nothing like "RGBA"). Also checked this thread - https://forums.adobe.com/thread/1587246, those guys are confused too. I had to save the files as Tiffs and use Xnview to batch convert them into Png (alpha becomes transparency that way) and use Png's in OpenCV. Also there's just nothing to work with at OpenCV, even internal vars contain only 3 channels and nothing else. – ogurets May 06 '17 at 00:35
1

I found a solution this problem in converting the original image to RBGA format through PIL library.

from PIL import Image
import numpy as np
import cv2

path_to_image = 'myimg.png'
image = Image.open(path_to_image).convert('RGBA')
image.save(path_to_image)

image = cv2.imread(path_to_image, cv2.IMREAD_UNCHANGED)
print image.shape

out > (800, 689, 4)

  • PIL is what I am using now. But I was thinking if OpenCV can directly read RGBA image directly. – Nan An Jun 29 '14 at 22:27
-3

I solved the same problem with:

pip install --upgrade opencv-python
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Li Pi
  • 1