Next, we need the function that registers the callback. That function is called
cv::setMouseCallback(), and it requires three arguments.
void cv::setMouseCallback(
const string& windowName, // Handle used to identify window
cv::MouseCallback on_mouse, // Callback function
void* param = NULL // Additional parameters for callback fn.
);
The first argument is the name of the window to which the callback will be attached. Only events in that
particular window will trigger this specific callback. The second argument is your callback function.
Finally, the third param argument allows us to specify the param information that should be given to the
callback whenever it is executed. This is, of course, the same param we were just discussing with the
callback prototype.
In Example 4-3, we write a small program to draw boxes on the screen with the mouse. The function
my_mouse_callback() responds to mouse events, and it uses the event to determine what to do when
it is called.
Example 4-3. Toy program for using a mouse to draw boxes on the screen
#include <opencv2/opencv.hpp>
// Define our callback which we will install for
// mouse events
//
void my_mouse_callback(
int event, int x, int y, int flags, void* param
);
Rect box;
bool drawing_box = false;
// A litte subroutine to draw a box onto an image
//
void draw_box( cv::Mat& ...