What is the best way to draw multi-color segmented circle using OpenCV like below?
What I found, it can be:
- Using cv.fillPoly Many points are required for an arcs accurate drawing, the number of segments is several hundred;
- Using cv.line by rotating the line in a circle;
- Using cv.line by rotating whole image like in this similar case.
Advertisement
Answer
Using cv.ellipse you can draw segments pretty easily:
from matplotlib import pyplot as plt
import cv2
import numpy as np
import random
ANGLE_DELTA = 360 // 8
img = np.zeros((700, 700, 3), np.uint8)
img[::] = 255
for size in range(300, 0, -100):
for angle in range(0, 360, ANGLE_DELTA):
r = random.randint(0, 256)
g = random.randint(0, 256)
b = random.randint(0, 256)
cv2.ellipse(img, (350, 350), (size, size), 0, angle, angle + ANGLE_DELTA, (r, g, b), cv2.FILLED)
plt.gcf().set_size_inches((8, 8))
plt.imshow(img)
plt.show()
gives

