1. 程式人生 > >opencv 檢測滑鼠的點選和移動

opencv 檢測滑鼠的點選和移動

只需要定義一個callback的函式, 然後將這個函式附屬到我們定義的一個opencv的window上即可, 這樣每當滑鼠在視窗處發生動作的時候, 就會呼叫callback函式。  點選滑鼠的時候, callback也會記錄下點選滑鼠的位置。 程式如下:
#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("D:\\psb.JPG");

      //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;

}

函式解釋如下:

void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0)
This function sets a callback function to be called every time any mouse events occurs in the specified window. Here is the detailed explanation of the each parameters of the above OpenCV function.

winname - Name of the OpenCV window. All mouse events related to this window will be registered
onMouse - Name of the callback function. Whenever mouse events related to the above window occur, this callback function will be called. This function should have the signature like the following
void FunctionName(int event, int x, int y, int flags, void* userdata)
event - Type of the mouse event. These are the entire list of mouse events
EVENT_MOUSEMOVE
EVENT_LBUTTONDOWN
EVENT_RBUTTONDOWN
EVENT_MBUTTONDOWN
EVENT_LBUTTONUP
EVENT_RBUTTONUP
EVENT_MBUTTONUP
EVENT_LBUTTONDBLCLK
EVENT_RBUTTONDBLCLK
EVENT_MBUTTONDBLCLK
x - x coordinate of the mouse event
y - y coordinate of the mouse event
flags - Specific condition whenever a mouse event occurs. See the next OpenCV example code for the usage of this parameter. Here is the entire list of enum values which will be possesed by "flags"
EVENT_FLAG_LBUTTON
EVENT_FLAG_RBUTTON
EVENT_FLAG_MBUTTON
EVENT_FLAG_CTRLKEY
EVENT_FLAG_SHIFTKEY
EVENT_FLAG_ALTKEY
userdata - Any pointer passes to the "setMouseCallback" function as the 3rd parameter (see below)
userdata - This pointer will be passed to the callback function
下面在給一個函式, 用於檢測 一個滑鼠事件配合鍵盤上的某個鍵時候, 會響應, 如下:
#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
     Mat img = imread("D:\\psb.JPG");

      //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;
}