简体   繁体   English

将 Alpha 通道添加到 RGB 图像

[英]Add alpha channel to RGB image

How can I add alpha channel to png image?如何将 alpha 通道添加到 png 图像? Precisely I have the background using grabcut function, now I want to add it to the png image.确切地说,我有使用grabcut function的背景,现在我想将它添加到png图像中。

在此处输入图像描述

After that I want to set the alpha channel to transparent by use of a mask之后,我想通过使用蒙版将 Alpha 通道设置为透明

img[np.all(img == [0, 0, 0, 255], axis=2)] = [0, 0, 0, 0]

Here is fill channel approach.这是填充通道方法。

import cv2 

# path to your own images
rgb = cv2.imread('images/5v63T_RGB.png')
alpha = cv2.imread('images/5v63T_Alpha.png')
 
rgba = cv2.cvtColor(rgb, cv2.COLOR_RGB2RGBA)
one_channel_alpha = cv2.cvtColor(alpha, cv2.COLOR_RGB2GRAY)
 
# fill alpha channel with image (needs to be same size)
rgba[:, :, 3] = one_channel_alpha

cv2.imshow("rgb", rgb)
cv2.imshow("alpha", one_channel_alpha)
cv2.imshow("rgba", rgba)
# you can't visualize alpha channel as transparency.
# the 32 bits PNG image, so let's save and use other view. 
cv2.imwrite("images/result_rgba.png", rgba)

cv2.waitKey(0)
cv2.destroyAllWindows()

You get results like this:你会得到这样的结果: 在此处输入图像描述

Good Luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM