要执行你想要的操作,首先确保你的图片尺寸相同。
为了合并这两张图片,我使用了cv2.bitwise_or()
Python代码:
img_a = cv2.imread("a.png", 1)
img_b = cv2.imread(".png", 1)
ret, thresh = cv2.threshold(img_b, 10, 255, cv2.THRESH_BINARY)
rows,cols,channels = img_b.shape
roi = img_a[0:rows, 0:cols ]
img2gray = cv2.cvtColor(img_b,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(roi, roi, mask = mask_inv)
img2_fg = cv2.bitwise_or(img_b, img_b, mask = mask)
dst = cv2.add(img1_bg,img2_fg)
img_a[0:rows, 0:cols ] = dst
cv2.imshow('res',img_a)
以下是我得到的结果:
cv2.bitwise_xor
。 - Jeru Luke