[转]OpenCV学习笔记】之鼠标的调用

       记录自己学习opencv里面如何调用鼠标完成一些事件。在完成跟踪算法时常常会在第一帧框选出目标,这时候就需要调用鼠标来完成。

setMouseCallback()函数原型

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

(1)winname:窗口的名字

(2)onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。 这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);

(3)userdate:传给回调函数的参数

on_Mouse()函数原型:

C++:void on_Mouse(int event, int x, int y, int flags, void* param);

(1)event是 CV_EVENT_*变量之一

(2)x和y是鼠标指针在图像坐标系的坐标(不是窗口坐标系)

(3)flags是CV_EVENT_FLAG的组合, param是用户定义的传递到setMouseCallback函数调用的参数。

下面程序中on_MouseHandle()的思路:当左键按下时->赋予矩形ROI变量(x,y)->鼠标移动距离减去原先x,y值得到宽和高;->最后左键抬起时,判断鼠标的移动方向即矩形ROI区域的宽和高的正负。

  1 #include "stdafx.h"
  2 #include<opencv2/opencv.hpp>
  3  
  4 using namespace cv;
  5  
  6 #define WINDOW_NAME "实验窗口"
  7  
  8 //定义全局函数
  9 void on_MouseHandle(int evev, int x, int y, int flags, void* param);
 10 void DrawRectangle(Mat& img, Rect box);
 11  
 12  
 13 //定义全局变量
 14 Rect g_rectangle;
 15 bool g_bDrawingBox = false;//是否进行绘制
 16 RNG g_rng(12345);
 17  
 18  
 19 int main(int argc, char** argv)
 20 {
 21  
 22     //准备参数
 23     g_rectangle = Rect(-1, -1, 0, 0);
 24     Mat srcImage(600, 800, CV_8UC3), tempImage;
 25     srcImage.copyTo(tempImage);
 26     g_rectangle = Rect(-1, -1, 0, 0);
 27     srcImage = Scalar::all(0);
 28  
 29     //设置鼠标回调函数
 30     namedWindow(WINDOW_NAME);
 31     setMouseCallback(WINDOW_NAME, on_MouseHandle, (void*)&srcImage);
 32  
 33     //程序主循环,当进行绘制的标志符为真时,进行绘制
 34     while (1)
 35     {
 36         srcImage.copyTo(tempImage);//复制源图到临时变量
 37         if (g_bDrawingBox)//如果进行绘制的标识符为真时,则进行绘制
 38         {
 39             DrawRectangle(tempImage, g_rectangle);
 40         }
 41         imshow(WINDOW_NAME, tempImage);
 42         if (waitKey(10) == 27)
 43             break;//按下ESC键程序退出
 44     }
 45  
 46     return 0;
 47  
 48 }
 49  
 50 //**********************************************
 51 //on_MouseHandle()函数:
 52 //鼠标回调函数,根据不同的鼠标事件进行不同的操作
 53 //**********************************************
 54  
 55 void on_MouseHandle(int event, int x, int y, int flags, void* param)
 56 {
 57     Mat& image = *(Mat*)param;
 58     switch (event)
 59     {
 60         //鼠标移动消息
 61         case EVENT_MOUSEMOVE:
 62         {
 63             if (g_bDrawingBox)//如果是否进行绘制的标识符为真,则记录下长和框到Rect类型变量中
 64             {
 65                 g_rectangle.width = x - g_rectangle.x;
 66                 g_rectangle.height = y - g_rectangle.y;
 67             }
 68         }            
 69             break;
 70         
 71         //左键按下消息
 72         case EVENT_LBUTTONDOWN:
 73         {
 74             g_bDrawingBox = true;
 75             g_rectangle = Rect(x, y, 0, 0);
 76         }
 77             break;
 78  
 79         //左键抬起消息
 80         case EVENT_LBUTTONUP:
 81         {
 82             g_bDrawingBox = false;//置标识符为false
 83             //对宽和高小于0的处理
 84             if (g_rectangle.width < 0)
 85             {
 86                 g_rectangle.x += g_rectangle.width;
 87                 g_rectangle.width *= -1;
 88             }
 89             if (g_rectangle.height < 0)
 90             {
 91                 g_rectangle.y += g_rectangle.height;
 92                 g_rectangle.height *= -1;
 93             }
 94             //调用函数进行绘制
 95             DrawRectangle(image, g_rectangle);
 96         }
 97         break;
 98  
 99     }
100 }
101  
102 //**********************************************
103 //DrawRectangle()函数:
104  
105 
106 //自定义的矩形绘制函数
107 //**********************************************
108 void DrawRectangle(Mat& img, Rect box)
109 {
110     rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255),
111         g_rng.uniform(0, 255), g_rng.uniform(0, 255)));
112  
113 }
 
 

效果图:

原文地址:https://www.cnblogs.com/Henry-ZHAO/p/12725211.html