0
votes

I have written a python function, which is supposed to draw a circle on an image, where the center point of the circle has been defined using a mouse call back:

def draw_circle_contour(frame, pt, radius):
    cv2.circle(frame, center=pt, radius=3, color=(255,0,0), thickness=-1)
    discretized_circle_contour_x = [pt[0] + radius*np.cos(theta) for theta in np.linspace(0, 36, 36)]
    discretized_circle_contour_x = map(int, discretized_circle_contour_x)
    discretized_circle_contour_y = [pt[1] + radius*np.sin(theta) for theta in np.linspace(0, 36, 36)]
    discretized_circle_contour_y = map(int, discretized_circle_contour_y)
    discretized_circle_contour = zip(discretized_circle_contour_x, discretized_circle_contour_y)
    for el in discretized_circle_contour:
        cv2.circle(frame, center=el, radius=2, color=(255,0,0), thickness=-1)

Now, this works perfectly when I specify theta to be within np.linspace(0,360,360). However, I only want to draw 1/10 of a circle now. I.e. I want to draw a circle contour where the angle that is being covered is 36°. I figured this code should work, but for some reason when I run it, the result looks like this:

myCircle

What is going on here? Can anyone tell?

As you have pointed out: Instead of drawing little circles/points I could also use cv2.polylines to draw a circle segment. This simply requires the last two lines to be replaced with:

for el in discretized_circle_contour:
        help.append(list(el))
    cv2.polylines(frame, [np.asarray(help)], isClosed=False, color=(255,0,0), thickness=1) 

However, I am still facing the issue that this segment gets drawn multiple times, while I want it to be drawn only once in between [0,36] degrees!

... and I just found the reason for that: the angle theta needs to be given in radians, not in degrees. Whops.

Final result looks like this: polyline

1

1 Answers

1
votes

What's happening is, you're drawing a filled circle with radius 2 (which looks like a dot, because the radius is very small) in each point in discretized_circle_contour.

If I understand your question correctly, what you actually want is just an arch spanning 1/10th of the radius. You already have the points for the arch in [0, 36] degrees, just draw it as a polyline instead:

cv2.polylines(frame, [discretized_circle_contour], False, 1)