假设我有一个从预处理步骤中收到的空心,弯曲(不一定是凸形)形状的蒙版:
我现在想尝试选择出现在形状中的所有像素[[inside并将它们添加到蒙版中,如下所示:
如何在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
查找圆圈内的像素