我想拍摄一张图片,并将其覆盖为其轮廓,而不是背景/填充。我有一个图像,它是一个PNG格式的轮廓,它的背景和轮廓中的内容都被删除了,所以当打开时,除了轮廓之外,所有的都是透明的,类似于这个图像:

然而,当我打开图像并试图在OpenCV中覆盖它时,轮廓内的背景和区域显示为全白,显示图像尺寸的整个矩形,并模糊背景图像。
然而,我想做的是如下所示,其中只有轮廓覆盖在背景图像上,如下所示:

加分,如果你可以帮助我改变轮廓的颜色以及。
我不想处理任何与alphas的混合,因为我需要背景显示完整,并希望轮廓非常清晰。
发布于 2021-01-19 21:29:40
在这种特殊情况下,您的图像具有一些可以使用的alpha通道。使用Boolean array indexing,可以访问alpha通道中的所有值255。剩下要做的是“背景”图像w.r.t中的setting up some region of interest (ROI)。在ROI中,您再次使用布尔数组索引将所有像素设置为某种颜色,即红色。
下面是一些代码:
import cv2
# Open overlay image, and its dimensions
overlay_img = cv2.imread('1W7HZ.png', cv2.IMREAD_UNCHANGED)
h, w = overlay_img.shape[:2]
# In this special case, take the alpha channel of the overlay image, and
# check for value 255; idx is a Boolean array
idx = overlay_img[:, :, 3] == 255
# Open image to work on
img = cv2.imread('path/to/your/image.jpg')
# Position for overlay image
top, left = (50, 50)
# Access region of interest with overlay image's dimensions at position
# img[top:top+h, left:left+w] and there, use Boolean array indexing
# to set the color to red (for example)
img[top:top+h, left:left+w, :][idx] = (0, 0, 255)
# Save image
cv2.imwrite('output.png', img)这是一些随机“背景”图像的输出:

对于一般情况,即没有适当的alpha通道,您可以设置覆盖图像的阈值,以便为布尔数组索引设置适当的蒙版。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
OpenCV: 4.5.1
----------------------------------------https://stackoverflow.com/questions/65791502
复制相似问题