My code is down below. This codewill show the image and click and it'll show position. The question is I don't understand how the function know that x , y will return the position of image
import cv2
img = cv2.imread("image/tree.jpg")
def clickPosition(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
text = str(x) "," str(y)
cv2.putText(img,text,(x,y),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),cv2.LINE_4)
cv2.imshow("Output",img)
#showimage
cv2.imshow("Output",img)
#workwithmouse Mouse
cv2.setMouseCallback("Output",clickPosition)
cv2.waitKey(0)
cv2.destroyAllWindows()
CodePudding user response:
It's how setMouseCallback is defined. See its documentation:
void cv::setMouseCallback ( const String & winname, MouseCallback onm ouse, void * userdata = 0 )
This is for C , not Python, but it's useful to learn how to interpret this, because there's no comprehensive Python-specific OpenCV reference (as far as I know).
The MouseCallback type is defined as follows:
typedef void(* cv::MouseCallback) (int event, int x, int y, int flags, void *userdata)Callback function for mouse events. see
cv::setMouseCallback.Parameters
eventone of the cv::MouseEventTypes constants.
xThe x-coordinate of the mouse event.
yThe y-coordinate of the mouse event.
flagsone of the cv::MouseEventFlags constants.
userdataThe optional parameter.
So this defines the signature that is used when calling your mouse callback: first argument is the event, then come x and y, then flags. The last argument, userdata, is simply a copy of the userdata argument passed to setMouseCallback, which you can use for your own purposes.
