在RGB色彩空间三个通道的基础上,还可以加上一个A通道,也叫alpha通道,表示透明度
alpha通道的赋值范围是[0,1],或者[0,255],表示从透明到不透明
1 import cv2 2 import numpy as np 3 img = cv2.imread("/home/miao/dog.jpg" ) 4 bgra = cv2.cvtColor(img , cv2.COLOR_BGR2BGRA) 5 b,g,r,a = cv2.split(bgra) 6 a[:,:] = 125 7 bgra125 = cv2.merge([b,g,r,a]) 8 a[:,:] = 0 9 bgra0 = cv2.merge([b,g,r,a]) 10 cv2.imshow("img" , img) 11 cv2.imshow("bgra" , bgra) 12 cv2.imshow("bgra125" , bgra125) 13 cv2.imshow("bgra0" , bgra0) 14 cv2.waitKey() 15 cv2.destroyAllWindows() 16 cv2.imwrite("/home/miao/bgra.png" , bgra) 17 cv2.imwrite("/home/miao/bgra125.png" , bgra125) 18 cv2.imwrite("/home/miao/bgra0.png" , bgra0)
bgra
bgra125
bgra0
这里图片看不到,它实际上是纯色透明的
这里的三个图片保存时,需要通过cv2.imwrite函数保存