如何在 C++ 中使用 OpenCV 处理鼠标事件?
鼠标事件是 OpenCV 中最实用的功能之一。在 OpenCV 中,我们可以追踪鼠标指针的位置以及点击(右键、左键和中键)。OpenCV 在机器人学和计算机视觉中有着广泛的应用。在机器人学和计算机视觉中,追踪鼠标指针和点击被广泛使用。
本文将了解如何在图像上追踪鼠标指针的位置和点击。
以下程序展示了如何追踪鼠标指针的位置和点击。
示例
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
void locator(int event, int x, int y, int flags, void* userdata){ //function to track mouse movement and click//
if (event == EVENT_LBUTTONDOWN){ //when left button clicked//
cout << "Left click has been made, Position:(" << x << "," << y << ")" << endl;
} else if (event == EVENT_RBUTTONDOWN){ //when right button clicked//
cout << "Rightclick has been made, Position:(" << x << "," << y << ")" << endl;
} else if (event == EVENT_MBUTTONDOWN){ //when middle button clicked//
cout << "Middleclick has been made, Position:(" << x << "," << y << ")" << endl;
} else if (event == EVENT_MOUSEMOVE){ //when mouse pointer moves//
cout << "Current mouse position:(" << x << "," << y << ")" << endl;
}
}
int main() {
Mat image = imread("bright.jpg");//loading image in the matrix//
namedWindow("Track");//declaring window to show image//
setMouseCallback("Track", locator, NULL);//Mouse callback function on define window//
imshow("Track", image);//showing image on the window//
waitKey(0);//wait for keystroke//
return 0;
}输出


广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP