将图像透明堆叠在背景图像上

时间:2020-03-04 15:45:37

标签: python opencv computer-vision

什么是堆叠以下两个图像的最佳方法:

enter image description here

enter image description here

例如在这样的背景上,以便两个输入上的黑色像素是透明的? Alpha混合是我的最初方法,但是由于我需要在背景上堆叠的图像是numpy数组,而没有Alpha通道,所以这是一个很大的问题。

这是其他图像需要堆叠的背景层。

enter image description here

这是预期的结果:

enter image description here

1 个答案:

答案 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"

enter image description here