#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
const char* window_name = "video-demo";
void on_Mouse(int event, int x, int y, int flags, void* param);
int main(int argc, char* argv[])
{
//打开一个默认的相机
VideoCapture capture(0);
//检查是否成功打开
if (!capture.isOpened())
{
cout << "摄像头无法打开,请检查是否连接正常" << endl;
return -1;
}
Mat frame;
namedWindow(window_name);
setMouseCallback(window_name, on_Mouse, &frame); //类型转换
while (capture.read(frame))
{
imshow(window_name, frame);
char c = waitKey(66);
if (c == 'q')
{
break;
}
}
destroyAllWindows();
waitKey();
return 0;
}
void on_Mouse(int event, int x, int y, int flags, void *param) {
Mat *depth_img = (Mat *)param;
Vec3b depth; // 初始化不可放在case内
// 一般包含如下3个事件
switch (event)
{
case EVENT_MOUSEMOVE:
//cout << "Please select one point:" << endl;
break;
case EVENT_LBUTTONDOWN: // 鼠标左键按下
depth = depth_img->at<Vec3b>(x, y);
cout << "Position: (" << x << "," << y
<< ")—— depth = " << depth << endl;
break;
case EVENT_LBUTTONUP: // 鼠标左键释放
cout << "buttonup" << endl;
break;
}
}
|