用 alpha 通道合并两个图像

nun*_*usa 2 python scikit-image cv2

我有两张图片,一张有 Alpha 通道,另一张没有。因此, imageA和 分别B具有(x,y,4)(x,y,3)的形状。

我想使用 将两个图像合并到一个张量中python,其中B是背景,A是上图。最终图像的形状必须为 (x, y, 3)。我试过 scikit-image 或 cv2 是否能够做到这一点,但我找不到任何解决方案。

use*_*410 7

这是python中的alpha混合

import numpy as np
import cv2

alpha = 0.4

img1 = cv2.imread('Desert.jpg')
img2 = cv2.imread('Penguins.jpg')

#r,c,z = img1.shape

out_img = np.zeros(img1.shape,dtype=img1.dtype)
out_img[:,:,:] = (alpha * img1[:,:,:]) + ((1-alpha) * img2[:,:,:])
'''
# if want to loop over the whole image
for y in range(r):
    for x in range(c):
        out_img[y,x,0] = (alpha * img1[y,x,0]) + ((1-alpha) * img2[y,x,0])
        out_img[y,x,1] = (alpha * img1[y,x,1]) + ((1-alpha) * img2[y,x,1])
        out_img[y,x,2] = (alpha * img1[y,x,2]) + ((1-alpha) * img2[y,x,2])
'''

cv2.imshow('Output',out_img)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)