I am using the openCV library in python. I want to open an image, click three distinct points, and from these generate a circle segment.
The first point clicked is the center of the circle. The next point clicked defines the beginning of the segment and the last point clicked defines the end of the segment.
I am a little bit lost as on how to do this, since the cv2.ellipse
function requires angle information, which I am not sure how to acquire from my mouse clicks.
Here is my initial idea:
def draw_circleSegment(event, x, y, flags, param):
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
So when running this code, I get the error message "TypeError: ellipse() takes at most 5 arguments (7 given)" ... and I think this means at least one of the arguments of ellipse has a wrong type. That's why I performed these weird casts to integer values for both center_pt
and upper_pt
and radius
. But it didn't help.
Can anyone help?