标签:amp copy auto ack flags 位置 stat dem nbsp
参考:https://blog.csdn.net/dcrmg/article/details/52027847
setMouseCallBack(1,2,3);
一般有三个参数
第一个是 要操作的窗口
第二个是 回调的函数
第三个是 要操作的图像——(void*)(&image)
setMouseCallBack() 创建了一个鼠标回调函数,每次在图像上单击鼠标左键再抬起的过程
一般分为三次
1.单击按下
2.单击抬起
3.鼠标指针位置移动(即使原地单击)
1 void on_mouse(int event, int x, int y, int flags, void* userdata) 2 { 3 if (event == CV_EVENT_LBUTTONDOWN) 4 cout << "触发左键按下" << endl; 5 if (event == CV_EVENT_MOUSEMOVE) 6 cout << "触发鼠标移动事件" << endl; 7 if (event == CV_EVENT_RBUTTONDOWN) 8 cout << "触发右键按下" << endl; 9 if (event == CV_EVENT_LBUTTONUP) 10 cout << "触发左键抬起" << endl; 11 if (event == CV_EVENT_RBUTTONUP) 12 cout << "触发右键抬起" << endl; 13 if (event == CV_EVENT_LBUTTONDBLCLK) 14 cout << "触发左键双击" << endl; 15 if (event == CV_EVENT_RBUTTONDBLCLK) 16 cout << "触发右键双击" << endl; 17 }
1 Point sp(-1, -1); 2 Point ep(-1, -1); 3 Mat temp; 4 static void on_draw(int event, int x, int y, int flag,void *userdata)//每次操作鼠标位置对应x y 5 { 6 Mat image = *((Mat*)userdata); 7 if (event == EVENT_LBUTTONDOWN)//点击时 鼠标位置 赋值给 sp.x sp.y 并打印出来 8 { 9 sp.x = x; 10 sp.y = y; 11 cout << "start point:" << sp << endl; 12 } 13 else if (event == EVENT_LBUTTONUP)//当鼠标抬起时 对应鼠标的位置传给 ep 14 { 15 //cout << "鼠标抬起"; 16 ep.x = x; 17 ep.y = y; 18 //求出点击和抬起时的距离 19 int dx = ep.x - sp.x; 20 int dy = ep.y - sp.y; 21 if (dx > 0 && dy > 0) 22 { 23 Rect rect(sp.x, sp.y, ep.x - sp.x, ep.y - sp.y);//制作一个矩形 24 imshow("ROI区域", image(rect)); 25 rectangle(image, rect, Scalar(0, 0, 255), 2, 8, 0);//将矩形绘制在图像上 26 imshow("鼠标绘制", image);//打印该图像 27 //为下一次准备——更新数据——防止一直跟随 28 sp.x = -1; 29 sp.y = -1; 30 } 31 } 32 else if (event == EVENT_MOUSEMOVE) 33 { 34 if (sp.x > 0 && sp.y > 0) 35 { 36 ep.x = x; 37 ep.y = y; 38 int dx = ep.x - sp.x; 39 int dy = ep.y - sp.y; 40 if (dx > 0 && dy > 0) 41 { 42 Rect box(sp.x, sp.y, dx, dy); 43 temp.copyTo(image);//防止打印成填充图形 44 rectangle(image, box, Scalar(0, 255, 0), 2, 8, 0); 45 imshow("鼠标绘制", image); 46 } 47 } 48 49 } 50 } 51 void QuickColor::mouse_draw_demo(Mat& src) 52 { 53 temp = src.clone(); 54 namedWindow("鼠标绘制", WINDOW_AUTOSIZE); 55 setMouseCallback("鼠标绘制", on_draw,(void *)(&src)); 56 imshow("鼠标绘制", src); 57 }
opencv-setMouseCallBack 鼠标事件响应
标签:amp copy auto ack flags 位置 stat dem nbsp
原文地址:https://www.cnblogs.com/Alan-Wangyoubiao/p/14584130.html