[英]python OpenCV - add alpha channel to RGB image
使用 opencv 在 python 中將 RGB 圖像轉換為 RGBA 的最佳方法是什么?
假設我有一個具有形狀的數組
(185, 198, 3) - it is RGB
另一個是形狀為(185, 198)
的 alpha 蒙版
如何合並它們並保存到文件?
使用 opencv3,這應該可以工作:
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++
# 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);
您可以使用cv2.merge()
將 alpha 通道添加到給定的 RGB 圖像,但首先您需要根據文檔將 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))
這是另一個使用 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()
由於 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))
我將在 C++ 中發布我的答案,因為它可能對其他人有幫助(python 中已經有足夠的答案):
std::vector<cv::Mat> matChannels;
cv::split(mat, matChannels);
// create alpha channel
cv::Mat alpha(...);
matChannels.push_back(alpha);
cv::merge(matChannels, dst);
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)
如何在打開的簡歷中添加Alpha Cheel或將其透明度值設置為255
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.