python - 查找形状内的像素索引:Opencv 和 Python
问题描述
假设我有一个空心、弯曲(不一定是凸面)形状的面具,我从我的预处理步骤中收到:
我现在想尝试选择出现在该形状内的所有像素并将它们添加到蒙版中,如下所示:
我怎样才能在 Python 中做到这一点?
生成示例的代码:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Parameters for creating the circle
COLOR_BLUE = (255, 0, 0)
IMAGE_SHAPE = (256, 256, 3)
CIRCLE_CENTER = tuple(np.array(IMAGE_SHAPE) // 2)[:-1]
CIRCLE_RADIUS = 30
LINE_THICKNESS = 5 # Change to -1 for example of filled circle
# Draw on a circle
img = np.zeros(IMAGE_SHAPE, dtype=np.uint8)
img_circle = cv2.circle(img, CIRCLE_CENTER, CIRCLE_RADIUS, COLOR_BLUE, LINE_THICKNESS)
circle_mask = img_circle[:, :, 0]
# Show the image
plt.axis("off")
plt.imshow(circle_mask)
plt.show()
解决方案
用于floodFill
填充圆圈的外部。然后用于np.where
查找圆圈内的像素
cv2.floodFill(circle_mask, None, (0, 0), 1)
np.where(circle_mask == 0)
推荐阅读
- ruby - 在 ruby 中生成 OpenSSL subject_name 哈希
- azure - Azure 门户中的 Azure B2C 刀片丢失
- ms-access - 使用匹配的外键访问下一条记录
- c# - 在 VisualStudio 中调用期间引发异常
- c# - 与 Microsoft.Build.Execution.BuildManager 并行构建解决方案
- math - 脑筋急转弯:在六边形中找到正确的旋转(顺时针/逆时针)
- javascript - 加载ajax请求后jQuery PreventDefault()不起作用
- c++ - OpenCV -SolvePNP 如何从世界坐标计算猜测
- javascript - 使用 CSS 计数器值作为相应元素 id 属性的 id?
- xml - Go 中的 XML Unmarshalling 给出了带有导出字段的空结果