首页 > 解决方案 > 除了在 OpenCV (Python) 中作为坐标提供的多边形之外的遮光图像

问题描述

除了由提供的坐标形成的多边形外,我需要将图像涂黑。

例如:

./maskout_image.py --input input.jpg --coords "11,924 1255,934 1063,738, 216,711"
--coords "524,267 984,275 1276,910 69,926" 
--coords  "304,203 405,184 472,705 367,716"

可能有多个这样的坐标。

每个坐标代表多个多边形点(X/Y 像素)

openCV 代码应该基本上采用 input.jpg 并屏蔽掉属于所提供坐标形成的区域内的所有内容(将其涂黑)。

我知道 OpenCV 的多边形 API,但我不确定如何排除

将不胜感激一些方向/帮助。

这是在 Python 中。

标签: pythonopencv

解决方案


谢谢@Miki 和@Trigary 的提示。我没有这样想。你的方法奏效了。最后结果:

import cv2
import numpy as np

# original image
image = cv2.imread('image.jpg')
contours = np.array ([[418,368], [885,365], [953,562], [361,569]])

mask = np.zeros(image.shape, dtype=np.uint8)
cv2.fillPoly(mask, pts=[contours], color=(255,255,255))

# apply the mask
masked_image = cv2.bitwise_and(image, mask)

# save the result
cv2.imwrite('image_masked.png', masked_image)

推荐阅读