查找形状内的像素索引:Opencv和Python

问题描述 投票:0回答:2

假设我有一个从预处理步骤中收到的空心,弯曲(不一定是凸形)形状的蒙版:

Hollow circle mask

我现在想尝试选择出现在形状中的所有像素[[inside并将它们添加到蒙版中,如下所示:

Filled circle mask

如何在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()

python numpy opencv image-processing scikit-image
2个回答
1
投票
使用floodFill填充圆的外部。然后使用np.where查找圆圈内的像素

0
投票
这里还有另外两种方法:
© www.soinside.com 2019 - 2024. All rights reserved.