[英]Opencv: How to draw a circle contour on an image
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: 我已经编写了一个python函数,该函数应该在图像上绘制一个圆,该圆的中心点已使用鼠标回调来定义:
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)
. 现在,当我将theta指定在
np.linspace(0,360,360)
内时,这将非常np.linspace(0,360,360)
。 However, I only want to draw 1/10 of a circle now. 但是,我现在只想绘制一个圆的1/10。 Ie I want to draw a circle contour where the angle that is being covered is 36°.
即我想绘制一个圆形轮廓,其中被遮盖的角度为36°。 I figured this code should work, but for some reason when I run it, the result looks like this:
我认为这段代码应该可以工作,但是由于某种原因,运行它时,结果看起来像这样:
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. 正如您所指出的:除了绘制小圆圈/点外,我还可以使用
cv2.polylines
绘制一个圆弧段。 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! 但是,我仍然面临该段被多次绘制的问题,而我希望在[0,36]度之间仅绘制一次!
... and I just found the reason for that: the angle theta
needs to be given in radians, not in degrees. ……我才发现原因:角度
theta
需要以弧度而不是度为单位。 Whops. Whops。
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
. 发生的情况是,您在
discretized_circle_contour
中的每个点上绘制了一个半径为2的填充圆(看起来像一个点,因为半径很小)。
If I understand your question correctly, what you actually want is just an arch spanning 1/10th of the radius. 如果我正确理解了您的问题,那么您真正想要的只是一个跨过半径1/10的弧形。 You already have the points for the arch in [0, 36] degrees, just draw it as a polyline instead:
您已经有了[0,36]度的拱门点,只需将其绘制为折线即可:
cv2.polylines(frame, [discretized_circle_contour], False, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.