Вот два других метода:
Метод № 1: cv2.findContours
+ cv2.drawContours
Найдите контуры, затем заполните контур
cnts = cv2.findContours(circle_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(circle_mask, [c], -1, (255,255,255), -1)
Метод № 2: cv2.findContours
+ cv2.fillPoly
Снова найдите контуры, затем заполните, используя другую функцию заполнения
cv2.fillPoly(circle_mask, cnts, (255,255,255))
Полный код
import cv2
import numpy as np
# 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].astype(np.uint8)
# Method #1
cnts = cv2.findContours(circle_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(circle_mask, [c], -1, (255,255,255), -1)
# Method #2
# cv2.fillPoly(circle_mask, cnts, (255,255,255))
# Show the image
cv2.imshow('circle_mask', circle_mask)
cv2.waitKey()