Answer a question

What is the best way to convert RGB image to RGBA in python using opencv?

Let's say I have one array with shape

(185, 198, 3) - it is RGB

and the other is alpha mask with shape (185, 198)

How to merge them and save to file?

Answers

You may use cv2.merge() to add the alpha channel to the given RGB image, but first you need to split the RGB image to R, G and B channels, as per the documentation:

Python: cv2.merge(mv[, dst])

  • mv – input array or vector of matrices to be merged; all the matrices in mv must have the same size and the same depth.

And this can be done as:

b_channel, g_channel, r_channel = cv2.split(img)

alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 50 #creating a dummy alpha channel image.

img_BGRA = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐