鱼C论坛

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

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

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

using namespace std;
using namespace cv;
const char* windows_name = "onMouseCallback";

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;
        while (capture.read(frame))
        {
                imshow("video-demo", frame);
                char c = waitKey(66);
                if (c == 27)
                {
                        break;
                }
        }

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

    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;
    }
}
最佳答案
2021-5-16 10:33:02
#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;
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-16 10:33:02 | 显示全部楼层    本楼为最佳答案   
#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;
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 10:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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