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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32290096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:21:25  来源:igfitidea点击:

python OpenCV - add alpha channel to RGB image

pythonopencv

提问by Oleg

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

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

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)

另一个是带形状的 alpha 蒙版 (185, 198)

How to merge them and save to file?

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

采纳答案by ZdaR

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 Bchannels, as per the documentation:

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

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.

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

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

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

回答by snoob dogg

Here is an another simple example using Grabcut, it helps to get the right order of channels when saving the image on disk vs pyplot.

这是另一个使用 Grabcut 的简单示例,它有助于在将图像保存到磁盘与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()

回答by kaanoner

With opencv3, this should work:

使用 opencv3,这应该可以工作:

Python

Python

# 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

C++

C++

# First create the image with alpha channel
cv::cvtColor(rgb_data, rgba , cv::COLOR_RGB2RGBA);

# Split the image for access to alpha channel
std::vector<cv::Mat>channels(4);
cv::split(rgba, channels);

# Assign the mask to the last channel of the image
channels[3] = alpha_data;

# Finally concat channels for rgba image
cv::merge(channels, 4, rgba);

回答by Mark Setchell

Since OpenCV images are just Numpy arrays, you can do this in one-line, nice and fast with Numpy. So here is the setup code:

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

And here is the solution which is simply to stack the alpha channel onto the image in the "depth"axis, hence dstack():

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

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