【OpenCV_11】利用OpenCV检测鼠标点击及移动 Detect Mouse Clicks and Moves

OpenCV支持检测鼠标事件。鼠标事件包括鼠标点击及鼠标移动在一个特定的OpenCV创建的窗口。

OpenCV 实例


这个其是非常简单。只要利用OpenCV的C++代码定义一个回调函数连接到OpenCV定义的窗口。只要鼠标有事件发生,回调函数就会被调用。回调函数也会给出鼠标事件的坐标。(e.g - (x, y)鼠标的坐标).


#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
     if  ( event == EVENT_LBUTTONDOWN )
     {
          cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
     }
     else if  ( event == EVENT_RBUTTONDOWN )
     {
          cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
     }
     else if  ( event == EVENT_MBUTTONDOWN )
     {
          cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
     }
     else if ( event == EVENT_MOUSEMOVE )
     {
          cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;

     }
}

int main(int argc, char** argv)
{
     // Read image from file 
     Mat img = imread("/Users/iDreamboat/Desktop/OpenCV/RG.JPG"<span class="s1" style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; line-height: 25.48px;"></span><span style="line-height: 25.48px; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;">);</span>

     //if fail to read the image
     if ( img.empty() ) 
     { 
          cout << "Error loading the image" << endl;
          return -1; 
     }

     //Create a window
     namedWindow("My Window", 1);

     //set the callback function for any mouse event
     setMouseCallback("My Window", CallBackFunc, NULL);

     //show the image
     imshow("My Window", img);

     // Wait until user press some key
     waitKey(0);

     return 0;

}


通过OpenCV监测鼠标点击及移动

Summary

在上述OpenCV代码,
 任何的鼠标事件都会被调用 "CallbackFunc"函数,(当鼠标在OpenCV创建的窗口移动也是鼠标事件),通过适当的  if - else语句,打印出左键,右键及中键(仅作用于OpenCV创建的"My Window"窗口)。


  • void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0)
这个函数是在鼠标在指定的窗口发生事件时调用这个回调函数。下面介绍一下这个函数里的每一个参数的意义。

  • winname - 指定的OpenCV窗口。所有的发生在此窗口的事件都会被监测到。
  • onMouse - 回调函数的名称。当鼠标在定义窗口发生事件时,这个函数被调用。这个函数应该有类似下面的特征码。
    • void FunctionName(int event, int x, int y, int flags, void* userdata)
      • event - 鼠标事件类型,下面是鼠标所有的事件
        • EVENT_MOUSEMOVE
        • EVENT_LBUTTONDOWN
        • EVENT_RBUTTONDOWN
        • EVENT_MBUTTONDOWN
        • EVENT_LBUTTONUP
        • EVENT_RBUTTONUP
        • EVENT_MBUTTONUP
        • EVENT_LBUTTONDBLCLK
        • EVENT_RBUTTONDBLCLK
        • EVENT_MBUTTONDBLCLK
      • x - 鼠标事件的 x坐标
      • y - 鼠标事件的 y坐标
      • flags - 定义鼠标事件。参见下面的例子。
        • EVENT_FLAG_LBUTTON
        • EVENT_FLAG_RBUTTON
        • EVENT_FLAG_MBUTTON
        • EVENT_FLAG_CTRLKEY
        • EVENT_FLAG_SHIFTKEY
        • EVENT_FLAG_ALTKEY
      • userdata - 任意的指针传递给 "setMouseCallback"函数作为第3个函数
  • userdata - 这个是指针将传递给回调函数。

OpenCV 当按一个键的时候监测鼠标点击


下面谈一谈按一个键的时候怎么检测鼠标事件。当按鼠标左键及键盘 "CTRL" 键,按鼠标右键及键盘"SHIFT"键 以及按鼠标左键及键盘"ALT"键。

/
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
     if ( flags == (EVENT_FLAG_CTRLKEY + EVENT_FLAG_LBUTTON) )
     {
          cout << "Left mouse button is clicked while pressing CTRL key - position (" << x << ", " << y << ")" << endl;
     }
     else if ( flags == (EVENT_FLAG_RBUTTON + EVENT_FLAG_SHIFTKEY) )
     {
          cout << "Right mouse button is clicked while pressing SHIFT key - position (" << x << ", " << y << ")" << endl;
     }
     else if ( event == EVENT_MOUSEMOVE && flags == EVENT_FLAG_ALTKEY)
     {
          cout << "Mouse is moved over the window while pressing ALT key - position (" << x << ", " << y << ")" << endl;
     }
}

int main(int argc, char** argv)
{
     // Read image from file 
     <span style="line-height: 25.48px; text-align: justify; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;">Mat img = imread("/Users/iDreamboat/Desktop/OpenCV/RG.JPG"</span><span class="s1" style="line-height: 25.48px; text-align: justify; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"></span><span style="line-height: 25.48px; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;">);</span>
     //if fail to read the image
     if ( img.empty() ) 
     { 
          cout << "Error loading the image" << endl;
          return -1; 
     }

     //Create a window
     namedWindow("My Window", 1);

     //set the callback function for any mouse event
     setMouseCallback("My Window", CallBackFunc, NULL);

     //show the image
     imshow("My Window", img);

     // Wait until user press some key
     waitKey(0);

     return 0;
}
/

Note.


从本篇开始换了IDE试验环境为xcode。所以实验结果以及文件路径与windows略有不同。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值