【发布时间】:2019-05-15 08:36:04
【问题描述】:
我想以编程方式覆盖图像,例如幸福熟悉的 Windows XP 壁纸:
与另一个包含黑色像素的非矩形图像,例如一个标准的大光标图标:
复制粘贴来自this 和this 教程的代码,它们都使用了我到达的OpenCV 位掩码魔法:
import cv2 as cv
# Load two images
img1 = cv.imread('bliss.png') # The image I want the overlay to be diplayed on.
img2 = cv.imread('cursor.png') # The image I want to overlay with, containing black pixels.
# I want to put logo on top-left corner, So I create a ROI.
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols ]
# Now create a mask of logo and create its inverse mask also.
img2gray = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
ret, mask = cv.threshold(img2gray, 20, 255, cv.THRESH_BINARY)
mask_inv = cv.bitwise_not(mask)
# Now black-out the area of logo in ROI.
img1_bg = cv.bitwise_and(roi, roi, mask = mask_inv)
# Take only region of logo from logo image.
img2_fg = cv.bitwise_and(img2, img2, mask = mask)
# Put logo in ROI and modify the main image
dst = cv.add(img1_bg, img2_fg)
img1[0:rows, 0:cols ] = dst
cv.imshow('res',img1)
cv.waitKey(0)
cv.destroyAllWindows()
在尝试为cv.threshold(包括thres and maxval arguments 和thresholding types)寻找正确参数的天真的排列过程中,我总是发现原始图像中存在大量黑色像素从覆盖的图像中丢失.在左下方的放大图片中,您可以看到重叠的光标,右侧是原始复制的:
我认为这种像素损失是由于灰度转换和/或反向 (?) 掩码造成的,但无法弄清楚如何或在上面的代码中进行更改。在教程中,我将上面链接的不包含黑色像素的图像用于叠加,结果看起来很好。有没有办法对包含黑色像素的图像做同样的事情?
【问题讨论】:
标签: python opencv image-processing overlay