什么是堆叠以下两个图像的最佳方法:
例如在这样的背景上,以便两个输入上的黑色像素是透明的? Alpha混合是我的最初方法,但是由于我需要在背景上堆叠的图像是numpy数组,而没有Alpha通道,所以这是一个很大的问题。
这是其他图像需要堆叠的背景层。
这是预期的结果:
答案 0 :(得分:2)
您可以这样做:
#!/usr/bin/env python3
import cv2
# Load all three images - "c" is the background
a= cv2.imread('a.png')
b= cv2.imread('b.png')
c= cv2.imread('c.png')
# Chop all to same size as smallest
a=a[:632,:474]
b=b[:632,:474]
c=c[:632,:474]
# Mask where a is not black, and copy those bits to c
mask = np.all(a[...,:]>0,axis=2)
c[mask]=a[mask]
# Mask where b is not black, and copy those bits to c
mask = np.all(b[...,:]>0,axis=2)
c[mask]=b[mask]
Your result is now in "c"