pythoneer
pythoneer

Reputation: 1

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()

Upvotes: 0

Views: 231

Answers (1)

Mateus Reis
Mateus Reis

Reputation: 306

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.

Upvotes: 1

Related Questions