Winname is the name of the window, callback is the set callback function, userdata is the parameter of the callback function, and the default value is None.
Callback (event, x, y, flags, userdata): callback function, event is an event, whether the mouse is left or right or middle button; x. Y is the position of the mouse, and the event is the coordinate point of the mouse; Flags mainly use combination keys, such as pressing the left mouse button of ctrl.
import cv2 import numpy as np # 创建一张全黑的图片 img=np.zeros((360,640,3),np.uint8) def mousecallback(event,x,y,flag,userdata): print(event,x,y,flag,userdata) # mousecallback(1,20,30,16,'666') # 创建窗口 cv2.namedWindow('窗口',cv2.WINDOW_NORMAL) cv2.resizeWindow('窗口',640,360) # 设置鼠标回调 cv2.setMouseCallback('窗口',mousecallback,'123') while 1: cv2.imshow('窗口',img) key=cv2.waitKey(1) if key==ord('q'): break cv2.destroyAllWindows