H_E_A_D
H_E_A_D

Reputation: 111

python OpenCv IMREAD_UNCHANGED only return three channels

I'm trying to figure out what's wrong with my code.

I want to load my image containing the alpha channel and the description from the official website says that:

cv.IMREAD_UNCHANGED: If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).

Here's my try:

import cv2 as cv 

img2 = cv.imread( 'lbj.jpg' , cv.IMREAD_UNCHANGED)

img2.shape

And the result shows : (350, 590, 3)

Isn't it supposed to be (350,590,4)?

Thanks!

Upvotes: 5

Views: 29033

Answers (1)

Mick
Mick

Reputation: 910

The reason there are only three channels is that the image is in jpg format, which does not have an alpha channel. If you were to load e.g. a png format image which had an alpha channel then

img2 = cv.imread( 'lbj.png' , cv.IMREAD_UNCHANGED)

with 'lbj.png' would load the image with the alpha channel included, and then

img2.shape

would show (350, 590, 4).

If you convert a jpg to png then you will still, at that point, have only three channels because the image would only have the BGR channels that were in the original jpg. However, you could at this point add an alpha channel to make it BGRA and then proceed to work with transparency options.

Adding an alpha channel is answered in python-opencv-add-alpha-channel-to-rgb-image

Upvotes: 11

Related Questions