简体   繁体   中英

Drawing a circle in python using open cv but don't understand the mechanism

i am new to open cv. i am drawing a circle on frame using the function. it works perfectly but dont understand how it works as i am passing no parameters to function. Below is the code. the function is draw_circle in the cv2.setMouseCallback.

import cv2
import numpy as np
def draw_circle(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img,(x,y),100,(0,255,0),-1)

img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow(winname='my_drawing')

#doesn't understand the below code how can we call the draw_circle without passing parameters
cv2.setMouseCallback('my_drawing',draw_circle)

while True: 
    cv2.imshow('my_drawing',img)

    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

That's because when you set a callback, you're not actually calling the function yourself, but plugging it in somewhere else that will call the function for you. In this case, whenever there is a mouse event, opencv internals will automatically call your function draw_circle . The arguments aren't provided by you, but by opencv, which keeps track of mouse movements and position and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM