OpenCV支持检测鼠标事件。鼠标事件包括鼠标点击及鼠标移动在一个特定的OpenCV创建的窗口。
这个其是非常简单。只要利用OpenCV的C++代码定义一个回调函数连接到OpenCV定义的窗口。只要鼠标有事件发生,回调函数就会被调用。回调函数也会给出鼠标事件的坐标。(e.g - (x, y)鼠标的坐标).
OpenCV 实例
#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"&