鱼C论坛

 找回密码
 立即注册
查看: 1825|回复: 1

[已解决]opencv打开摄像头后左击无法显示像素坐标

[复制链接]
发表于 2021-5-16 10:33:01 | 显示全部楼层 |阅读模式
10鱼币
如标题所示,我是根据kinect之前的代码去改的
  1. #include <iostream>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <opencv2/highgui/highgui.hpp>

  4. using namespace std;
  5. using namespace cv;
  6. const char* windows_name = "onMouseCallback";

  7. void on_Mouse(int event, int x, int y, int flags, void* param);

  8. int main(int argc, char* argv[])
  9. {
  10.     //打开一个默认的相机
  11.         VideoCapture capture(0);
  12.         //检查是否成功打开
  13.         if (!capture.isOpened())
  14.         {
  15.                 cout << "摄像头无法打开,请检查是否连接正常" << endl;
  16.                 return -1;
  17.         }
  18.         Mat frame;
  19.         while (capture.read(frame))
  20.         {
  21.                 imshow("video-demo", frame);
  22.                 char c = waitKey(66);
  23.                 if (c == 27)
  24.                 {
  25.                         break;
  26.                 }
  27.         }

  28.     namedWindow(windows_name);
  29.     setMouseCallback(windows_name, on_Mouse, (void*)&capture); //类型转换

  30.     waitKey();
  31.     return 0;
  32. }
  33. void on_Mouse(int event, int x, int y, int flags, void* param) {
  34.     Mat depth_img = *(Mat*)param; // 先转换类型,再取数据
  35.     Vec3b depth;    // 初始化不可放在case内

  36.     // 一般包含如下3个事件
  37.     switch (event)
  38.     {
  39.     case EVENT_MOUSEMOVE:
  40.         //cout << "Please select one point:" << endl;
  41.         break;
  42.     case EVENT_LBUTTONDOWN: // 鼠标左键按下
  43.         depth = depth_img.at<Vec3b>(x, y);
  44.         cout << "Position: (" << x << "," << y
  45.             << ")—— depth = " << depth << endl;
  46.         break;
  47.     case EVENT_LBUTTONUP: // 鼠标左键释放
  48.         cout << "buttonup" << endl;
  49.         break;
  50.     }
  51. }
复制代码
最佳答案
2021-5-16 10:33:02
  1. #include <iostream>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <opencv2/highgui/highgui.hpp>

  4. using namespace std;
  5. using namespace cv;
  6. const char* window_name = "video-demo";

  7. void on_Mouse(int event, int x, int y, int flags, void* param);

  8. int main(int argc, char* argv[])
  9. {
  10.     //打开一个默认的相机
  11.     VideoCapture capture(0);
  12.     //检查是否成功打开
  13.     if (!capture.isOpened())
  14.     {
  15.         cout << "摄像头无法打开,请检查是否连接正常" << endl;
  16.         return -1;
  17.     }

  18.     Mat frame;
  19.     namedWindow(window_name);
  20.     setMouseCallback(window_name, on_Mouse, &frame); //类型转换
  21.     while (capture.read(frame))
  22.     {
  23.         imshow(window_name, frame);
  24.         char c = waitKey(66);
  25.         if (c == 'q')
  26.         {
  27.             break;
  28.         }
  29.     }

  30.     destroyAllWindows();
  31.     waitKey();
  32.     return 0;
  33. }
  34. void on_Mouse(int event, int x, int y, int flags, void *param) {
  35.     Mat *depth_img = (Mat *)param;
  36.     Vec3b depth;    // 初始化不可放在case内

  37.     // 一般包含如下3个事件
  38.     switch (event)
  39.     {
  40.         case EVENT_MOUSEMOVE:
  41.             //cout << "Please select one point:" << endl;
  42.             break;
  43.         case EVENT_LBUTTONDOWN: // 鼠标左键按下
  44.             depth = depth_img->at<Vec3b>(x, y);
  45.             cout << "Position: (" << x << "," << y
  46.                 << ")—— depth = " << depth << endl;
  47.             break;
  48.         case EVENT_LBUTTONUP: // 鼠标左键释放
  49.             cout << "buttonup" << endl;
  50.             break;
  51.     }
  52. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-16 10:33:02 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <opencv2/highgui/highgui.hpp>

  4. using namespace std;
  5. using namespace cv;
  6. const char* window_name = "video-demo";

  7. void on_Mouse(int event, int x, int y, int flags, void* param);

  8. int main(int argc, char* argv[])
  9. {
  10.     //打开一个默认的相机
  11.     VideoCapture capture(0);
  12.     //检查是否成功打开
  13.     if (!capture.isOpened())
  14.     {
  15.         cout << "摄像头无法打开,请检查是否连接正常" << endl;
  16.         return -1;
  17.     }

  18.     Mat frame;
  19.     namedWindow(window_name);
  20.     setMouseCallback(window_name, on_Mouse, &frame); //类型转换
  21.     while (capture.read(frame))
  22.     {
  23.         imshow(window_name, frame);
  24.         char c = waitKey(66);
  25.         if (c == 'q')
  26.         {
  27.             break;
  28.         }
  29.     }

  30.     destroyAllWindows();
  31.     waitKey();
  32.     return 0;
  33. }
  34. void on_Mouse(int event, int x, int y, int flags, void *param) {
  35.     Mat *depth_img = (Mat *)param;
  36.     Vec3b depth;    // 初始化不可放在case内

  37.     // 一般包含如下3个事件
  38.     switch (event)
  39.     {
  40.         case EVENT_MOUSEMOVE:
  41.             //cout << "Please select one point:" << endl;
  42.             break;
  43.         case EVENT_LBUTTONDOWN: // 鼠标左键按下
  44.             depth = depth_img->at<Vec3b>(x, y);
  45.             cout << "Position: (" << x << "," << y
  46.                 << ")—— depth = " << depth << endl;
  47.             break;
  48.         case EVENT_LBUTTONUP: // 鼠标左键释放
  49.             cout << "buttonup" << endl;
  50.             break;
  51.     }
  52. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 03:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表