Mainly introducing
Function prototype and parameters of mouse event
Callback function prototype and parameters
Example of drawing rectangle with mouse event
The function that specifies the mouse operation message callback function is the SetMouseCallback function.
Function prototype:
void SetMouseCallback(const string & winname,MouseCallback onMouse,void* userdata=0)
parameter:
The first parameter is the name of the window,
The second parameter is used to specify the function pointer to be called every time the window is clicked.
The third parameter is the user-defined parameter passed to the callback function.
where the function pointer prototype:
void Foo(int event ,int x ,int y ,int flags ,void * param)。
parameter:
event is the mouse response type, one of the CV_EVENT_* variables:
EVENT_MOUSEMOVE swipe
EVENT_LBUTTONDOWN 左击
EVENT_RBUTTONDOWN Right click
EVENT_MBUTTONDOWN middle click
EVENT_LBUTTONUP left button release
EVENT_RBUTTONUP right click and release
EVENT_LBUTTONDBLCLK left double click
EVENT_RBUTTONDBLCLK right-click
EVENT_MBUTTONDBLCLK middle button double click
x and y are the coordinates of the mouse pointer in the image coordinate system (not the window coordinate system).
flags is a combination of CV_EVENT_FLAG, and the status of flags are:
EVENT_FLAG_LBUTTON left drag
EVENT_FLAG_RBUTTON right drag
EVENT_FLAG_MBUTTON Middle button drag
EVENT_FLAG_CTRLKEY Hold down Ctrl
EVENT_FLAG_SHIFTKEY Hold down Shift
EVENT_FLAG_ALTKEY Hold down Alt
param is a user-defined parameter passed to the setMouseCallback function call.
The code for drawing a rectangle through the mouse callback function is as follows:
void MouseEvent(); //Main function
void on_MouseHandle(int event,int x, int y, int flag, void* param); //Callback function
void DrawRectangle(Mat& img,Rect box); //Draw a rectangle
/********************Mouse operation****************/ Rect g_rect; bool g_DrawFlag = false; RNG g_rng(12345); void MouseEvent() { // prepare parameters g_rect = Rect(-1, -1, 0, 0); Mat srcImage (600, 600, CV_8UC3), tempImage; srcImage.copyTo (tempImage); g_rect = Rect(-1, -1, 0, 0); srcImage = Scalar :: all (0); //Set the mouse operation callback function namedWindow("Win"); setMouseCallback("Win", on_MouseHandle, (void*)&srcImage); //painting while (1) { srcImage.copyTo (tempImage); if (g_DrawFlag) { DrawRectangle(tempImage, g_rect); } imshow("Win", tempImage); if (waitKey(10) == 27) break;//ESC 退出 } } //mouse callback event void on_MouseHandle(int event, int x, int y, int flag, void* param) { Mat& image = *(Mat*)param; switch (event) { case EVENT_MOUSEMOVE://move if (g_DrawFlag) { //Calculation, g_rect width and height = mouse current position coordinates - coordinates of the upper left corner of g_rect g_rect.width = x - g_rect.x; g_rect.height = y - g_rect.y; } break; case EVENT_LBUTTONDOWN://left button down g_DrawFlag = true; //Set the initial value of g_rect at the same point g_rect = Rect(x, y, 0, 0); break; case EVENT_LBUTTONUP://left button up g_DrawFlag = false; //When g_rect width and height is less than 0 //The xy coordinate of the starting point is set to the smaller point near the upper left corner // absolute value of width and height if (g_rect.width < 0) { g_rect.x += g_rect.width; g_rect.width *= -1; } if (g_rect.height < 0) { g_rect.y += g_rect.height; g_rect.height *= -1; } // draw rectangle DrawRectangle(image, g_rect); break; } } //Rectangle drawing function void DrawRectangle(Mat& img, Rect box) { //rectangle draw rectangle //tl point in the upper left corner, br point in the lower right corner //Scalar sets the color, set to 3 channels //g_rng.uniform(0, 255) random color rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255))); }
Effect: