python OpenCV - 将alpha通道添加到RGB图像

时间:2015-08-29 19:49:46

标签: python opencv

使用opencv在python中将RGB图像转换为RGBA的最佳方法是什么?

假设我有一个形状为

的数组
(185, 198, 3) - it is RGB

另一个是形状为(185, 198)

的alpha蒙版

如何合并它们并保存到文件?

5 个答案:

答案 0 :(得分:20)

您可以使用cv2.merge()将Alpha通道添加到给定的RGB图像,但首先需要根据documentation将RGB图像拆分为R, G and B个通道:

  

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

     
      
  • mv - 输入要合并的矩阵的数组或向量; mv中的所有矩阵必须具有相同的大小和相同的深度。
  •   

这可以这样做:

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))

答案 1 :(得分:12)

使用opencv3,这应该有效:

# First create the image with alpha channel
rgba = cv2.cvtColor(rgb_data, cv2.COLOR_RGB2RGBA)

# Then assign the mask to the last channel of the image
rgba[:, :, 3] = alpha_data

答案 2 :(得分:3)

这是使用Grabcut的另一个简单示例,它有助于在磁盘上保存图像时获得正确的频道顺序vs pyplot

from matplotlib import pyplot as plt
import numpy as np
import cv2

img = cv2.imread('image.jpg')

mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1,65), np.float64)
fgdModel = np.zeros((1,65), np.float64)
rect = (50, 50, 450, 290)

# Grabcut 
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)

r_channel, g_channel, b_channel = cv2.split(img) 
a_channel = np.where((mask==2)|(mask==0), 0, 255).astype('uint8')  

img_RGBA = cv2.merge((r_channel, g_channel, b_channel, a_channel))
cv2.imwrite("test.png", img_RGBA)

# Now for plot correct colors : 
img_BGRA = cv2.merge((b_channel, g_channel, r_channel, a_channel))

plt.imshow(img_BGRA), plt.colorbar(),plt.show()

答案 3 :(得分:2)

由于OpenCV图像只是Numpy数组,因此您可以使用Numpy以单行,美观又快速的方式完成此操作。所以这是设置代码:

import numpy as np

# We'll synthesise a random image and a separate alpha channel full of 128 - semitransparent
im    = np.random.randint(0,256,(480,640,3), dtype=np.uint8)
alpha = np.full((480,640), 128, dtype=np.uint8)

这是解决方案,它只是将alpha通道沿“深度” 轴堆叠到图像上,因此dstack()

result = np.dstack((im, alpha))

答案 4 :(得分:0)

import cv2
import numpy as np
import skimage.exposure



path_input_image="./input_image.png"


input_image = cv2.imread(path_input_image2, cv2.IMREAD_UNCHANGED)


input_image_alphachann = np.full((input_image.shape[0],input_image.shape[1]), 128, dtype=np.uint8)

output_image = np.dstack((input_image, input_image_alphachann))


print(input_image.shape)
print(output_image.shape)
#(400, 200, 3); 3 channell rgb
#(400, 200, 4); 4c channel rgba



print(input_image.dtype)
print(output_image.dtype)
# uint8




path_output_image=path_input_image+'.alpha.png'
cv2.imwrite(path_output_image, output_image)

相关问题