python alpha通道_使用Open CV Python将Alpha通道添加到单色图像

I have been working on colour images(RGB) and color images with an alpha channel(RGBA) . Reading an alpha channel from an RGBA image is pretty easy and I can even split the 4 channels of the image. Is there any method by which I can add an alpha channel to a monochrome or a grayscale image? Also, can alpha channel be separately added to the R,G,B Channels individually ?

The code I am using to read a transparent image and split the channels is as follows -

import cv2

img = cv2.imread(image1_path,-1)

b = img[:,:,0]

g = img[:,:,1]

r = img[:,:,2]

a = img[:,:,3]

img_merge = cv2.merge((b,g,r,a))

cv2.imshow("img_merge",img_merge)

cv2.imshow("r channel",r)

cv2.imshow("g channel",g)

cv2.imshow("b channel",b)

cv2.imshow("a channel",a)

cv2.waitKey(0)

cv2.destroyAllWindows()

The image I am using is -

解决方案

You cannot create a 2-channel "luminance-alpha" image, however you can convert the 1-channel grayscale image to BGRA using only gray values by duplicating the grayscale channel and adding the alpha channel to that. Let l be the grayscale image:

img_3gray = cv2.merge((l,l,l,a))

Nor can you apply an alpha channel to just one channel of an image, but you can take a single channel of the image (say, blue) and turn it into a grayscale image as we did before:

img_3blue = cv2.merge((b,b,b,a))

or you can display only the blue channel with alpha:

img_bzz = cv2.merge((b,z,z,a))

where z is all zeroes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值