1#include <iostream>2#include <opencv2/opencv.hpp>3 4 #defineWindow_name "Mouse Event window"5 6 using namespacestd;7 using namespaceCV;8 9 Rect G_rectangle;Ten BOOLG_bdrawingbox =false; OneRNG G_rng (12345); A - voidDrawRectangle (Mat &img, Rect box) - { theRectangle (img, box.tl (), box.br (), Scalar (G_rng.uniform (0,255), G_rng.uniform (0,255), G_rng.uniform (0,255))); - } - - /*Note: You cannot write an uninitialized matrix class in the Destroy function, so you need to write a flag bit and then use it within the while (1) loop .*/ + voidOnmouse (int Event,intXintYintFlagvoid*param) - { +Mat &img = * (cv::mat*) param; A at Switch(Event) - { - //when moving the mouse - CaseCv_event_mousemove: - { - if(G_bdrawingbox) in { -G_rectangle.width = x-g_rectangle.x; toG_rectangle.height = y-g_rectangle.y; + } - } the Break; * //When you click the left mouse button $ CaseCv_event_lbuttondown:Panax Notoginseng { -G_bdrawingbox =true; theG_rectangle = Rect (x, Y,0,0); + } A Break; the //when you release the left mouse button + CaseCv_event_lbuttonup: - { $G_bdrawingbox =false; $ - DrawRectangle (IMG, g_rectangle); - } the Break; - }Wuyi } the - intMain () Wu { -Mat Srcimage ( -, -, CV_8UC3); About Mat tempimage; $ - Srcimage.copyto (tempimage); - -Srcimage = Scalar::all (0); A + Namedwindow (window_name); theSetmousecallback (Window_name, Onmouse, (void*) &srcimage); - $ while(1) the { the Srcimage.copyto (tempimage); the the if(G_bdrawingbox) - DrawRectangle (Tempimage, g_rectangle); in the imshow (Window_name, tempimage); the About if(Waitkey (Ten) == -) the Break; the the /*DrawRectangle (Srcimage, g_rectangle); + imshow (Window_name, srcimage); - if (waitkey () = =) the Break ;*/Bayi } the the return 0; -}
Mouse (Draw rectangle event)-not very good