鼠标操作

1、setMouseCallback函数

用于创建鼠标响应函数

调用形式:

C++: void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata=0 )

const string& winname:窗口名称

MouseCallback onMouse:鼠标响应调用的函数

void* userdata=0:一般选择默认0


2、opencv例子:

程序如下,已经附上说明:

[cpp]  view plain copy
  1. #include <opencv2/core/core.hpp>  
  2. #include <opencv2/highgui/highgui.hpp>  
  3. #include <stdio.h>  
  4.   
  5. using namespace cv;  
  6.   
  7. cv::Mat org,dst,img,tmp;  
  8. void on_mouse(int event,int x,int y,int flags,void *ustc)//event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号  
  9. {  
  10.     static Point pre_pt = (-1,-1);//初始坐标  
  11.     static Point cur_pt = (-1,-1);//实时坐标  
  12.     char temp[16];  
  13.     if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆  
  14.     {  
  15.         org.copyTo(img);//将原始图片复制到img中  
  16.         sprintf(temp,"(%d,%d)",x,y);  
  17.         pre_pt = Point(x,y);  
  18.         putText(img,temp,pre_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255),1,8);//在窗口上显示坐标  
  19.         circle(img,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);//划圆  
  20.         imshow("img",img);  
  21.     }  
  22.     else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))//左键没有按下的情况下鼠标移动的处理函数  
  23.     {  
  24.         img.copyTo(tmp);//将img复制到临时图像tmp上,用于显示实时坐标  
  25.         sprintf(temp,"(%d,%d)",x,y);  
  26.         cur_pt = Point(x,y);  
  27.         putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));//只是实时显示鼠标移动的坐标  
  28.         imshow("img",tmp);  
  29.     }  
  30.     else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//左键按下时,鼠标移动,则在图像上划矩形  
  31.     {  
  32.         img.copyTo(tmp);  
  33.         sprintf(temp,"(%d,%d)",x,y);  
  34.         cur_pt = Point(x,y);  
  35.         putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));  
  36.         rectangle(tmp,pre_pt,cur_pt,Scalar(0,255,0,0),1,8,0);//在临时图像上实时显示鼠标拖动时形成的矩形  
  37.         imshow("img",tmp);  
  38.     }  
  39.     else if (event == CV_EVENT_LBUTTONUP)//左键松开,将在图像上划矩形  
  40.     {  
  41.         org.copyTo(img);  
  42.         sprintf(temp,"(%d,%d)",x,y);  
  43.         cur_pt = Point(x,y);  
  44.         putText(img,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));  
  45.         circle(img,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);  
  46.         rectangle(img,pre_pt,cur_pt,Scalar(0,255,0,0),1,8,0);//根据初始点和结束点,将矩形画到img上  
  47.         imshow("img",img);  
  48.         img.copyTo(tmp);  
  49.         //截取矩形包围的图像,并保存到dst中  
  50.         int width = abs(pre_pt.x - cur_pt.x);  
  51.         int height = abs(pre_pt.y - cur_pt.y);  
  52.         if (width == 0 || height == 0)  
  53.         {  
  54.             printf("width == 0 || height == 0");  
  55.             return;  
  56.         }  
  57.         dst = org(Rect(min(cur_pt.x,pre_pt.x),min(cur_pt.y,pre_pt.y),width,height));  
  58.         namedWindow("dst");  
  59.         imshow("dst",dst);  
  60.         waitKey(0);  
  61.     }  
  62. }  
  63. void main()  
  64. {  
  65.     org = imread("1.jpg");  
  66.     org.copyTo(img);  
  67.     org.copyTo(tmp);  
  68.     namedWindow("img");//定义一个img窗口  
  69.     setMouseCallback("img",on_mouse,0);//调用回调函数  
  70.     imshow("img",img);  
  71.     cv::waitKey(0);  
  72. }  



你可能感兴趣的:(鼠标操作)