python - 除了在 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 中。
解决方案
谢谢@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)
推荐阅读
- java - 杰克逊的 ObjectMapper 失败:套接字关闭
- angular - 有谁知道为什么 mat-sort 在这件作品中不起作用?
- duplicates - 具有条件的重复行电源 BI
- javascript - 分别检查数组中的一项与所有其他项
- python - jupyter vs script 中的异步行为
- kubernetes - Kubectl top 在节点和 pod 之间提供不同的结果
- filesystems - 我们可以使用 Azure 数据工厂将 .BAK(SQL Server 备份文件)文件从本地服务器文件夹复制到 Azure Blob 存储吗?
- c# - Kubernetes 内部 App Fuctions 启动操作时发生主机错误
- c++ - 将 const char * 传递给函数
- android - 如何增加android语音识别监听的时间?