Clicking on images
You can program your mouse to perform specific operations when it is over one of the image windows you created. This is done by defining an appropriate callback function. A callback function is a function that you do not explicitly call but which is called by your application in response to specific events (here, the events that concern the mouse interacting with an image window). To be recognized by applications, callback functions need to have a specific signature and must be registered. In the case of the mouse event handler, the callback function must have the following signature:
void onMouse( int event, int x, int y, int flags, void* param);
The first parameter is an integer that is used to specify which type of mouse event has triggered the call to the callback function. The other two parameters are simply the pixel coordinates of the mouse location when the event occurred. The flags are used to determine which button was pressed when the mouse event was triggered. Finally, the last parameter is used to send an extra parameter to the function in the form of a pointer to an object. This callback function can be registered in the application through the following call:
cv::setMouseCallback("Original Image", onMouse, reinterpret_cast<void*>(&image));
In this example, the onMouse function is associated with the image window called Original Image, and the address of the displayed image is passed as an extra parameter to the function. Now, if we define the onMouse callback function as shown in the following code, then each time the mouse is clicked, the value of the corresponding pixel will be displayed on the console (here, we assume that it is a gray-level image):
void onMouse( int event, int x, int y, int flags, void* param) { cv::Mat *im= reinterpret_cast<cv::Mat*>(param); switch (event) { // dispatch the event case cv::EVENT_LBUTTONDOWN: // left mouse button down event // display pixel value at (x,y) std::cout << "at (" << x << "," << y << ") value is: " <<
static_cast<int>(im->at<uchar>(cv::Point(x,y))) << std::endl; break; } }
Note that in order to obtain the pixel value at (x,y), we used the at method of the cv::Mat object here; this is discussed in Chapter 2, Manipulating the Pixels. Other possible events that can be received by the mouse event callback function include cv::EVENT_MOUSE_MOVE, cv::EVENT_LBUTTONUP, cv::EVENT_RBUTTONDOWN, and cv::EVENT_RBUTTONUP.