OpenCV:如何在图像上绘制圆形轮廓 [英] Opencv: How to draw a circle contour on an image
问题描述
我已经编写了一个python函数,该函数应该在图像上绘制一个圆,该圆的中心点已使用鼠标回调来定义:
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)
现在,当我将theta指定在np.linspace(0,360,360)
之内时,这将非常有效.但是,我现在只想绘制一个圆的1/10. IE.我想绘制一个圆形轮廓,其中被遮盖的角度为36°.我认为这段代码应该可以工作,但是由于某种原因,运行它时,结果看起来像这样:
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:
这是怎么回事?谁能告诉?
What is going on here? Can anyone tell?
正如您所指出的:除了绘制小圆圈/点外,我还可以使用cv2.polylines
绘制圆弧段.只需将最后两行替换为:
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)
但是,我仍然面临着这个线段被绘制多次的问题,而我希望在[0,36]度之间仅绘制一次!
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!
...,我只是发现了原因:角度theta
需要以弧度而不是度为单位.哎呀
... and I just found the reason for that: the angle theta
needs to be given in radians, not in degrees. Whops.
最终结果如下:
推荐答案
正在发生的事情是,您在discretized_circle_contour
.
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
.
如果我正确理解了您的问题,那么您真正想要的只是一个跨过半径1/10的弧形. 您已经有了[0,36]度的拱门点,只需将其绘制为多段线即可:
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)
这篇关于OpenCV:如何在图像上绘制圆形轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!