我正在python中使用openCV库。我要打开图像,单击三个不同的点,然后从中生成一个圆弧段。
单击的第一点是圆的中心。单击的下一个点定义段的起点,单击的最后一个点定义段的终点。
由于cv2.ellipse
函数需要角度信息,我对此有点迷茫,我不确定如何从鼠标单击中获取角度信息。
这是我最初的想法:
def draw_circleSegment(事件,x,y,标志,参数):
global center_pt, upper_pt, center_clicked, upper_clicked
#right mouse click
if event == cv2.EVENT_RBUTTONDOWN:
#reset
if center_clicked and upper_clicked:
center_clicked = False
upper_clicked = False
center_pt = (0,0)
upper_clicked = (0,0)
#get coordinates of circle center
if not center_clicked:
center_pt = tuple(int(el) for el in (x,y))
center_clicked = True
#get coordinates of upper point of the circle segment
if not upper_clicked:
upper_pt = tuple(int(el) for el in(x,y))
upper_clicked = True
#initial values of the circle segment
center_pt = (0,0)
upper_pt = (0,0)
center_clicked = False
upper_clicked = False
#capture video
cap = cv2.VideoCapture(r'Path')
cv2.namedWindow(winname='Window Name')
cv2.setMouseCallback('Window Name', draw_circleSegment)
while True:
ret_frame, frame = cap.read()
#draw a circle segment whose points are defined by right mouse click
if center_clicked and upper_clicked:
cv2.line(frame, center_pt, upper_pt, (255,0,0), 2)
SegmentAngle=math.degrees(math.atan2(upper_pt[1]-center_pt[1], upper_pt[0]-center_pt[0]))
radius = int(math.sqrt((upper_pt[0]-center_pt[0])**2 + (upper_pt[1]-center_pt[1])**2))
cv2.ellipse(frame, center_pt, axes=(radius, radius), angle=SegmentAngle, startAngle=SegmentAngle, endAngle=SegmentAngle+180, thickness=2)
lower_pt = (upper_pt[0], upper_pt[1]-radius)
cv2.line(frame, center_pt, lower_ppt, (255,0,0), 2)
break
因此,在运行此代码时,我收到错误消息“ TypeError:ellipse()最多包含5个参数(给定7个)”……并且我认为这意味着ellipse的至少一个参数具有错误的类型。这就是为什么我对center_pt
和upper_pt
和radius
都将这些奇怪的转换转换为整数值的原因。但这没有帮助。
有人可以帮忙吗?