【问题标题】:Detect touch on OpenCV C++ window检测 OpenCV C++ 窗口上的触摸
【发布时间】:2020-01-17 07:18:35
【问题描述】:

我正在尝试打开网络摄像头视频,然后在第一次点击时将其暂停,然后在第二次点击触摸屏显示器时将其关闭。我正在使用 OpenCV 3.4.0 版。

目前我可以通过按q 键或关闭窗口 来完成此操作,但我无法通过点击屏幕来完成此操作。这是我的代码示例:

bool exit_flag = false;
do
{
    cv::imshow("window", draw_frame);
    int key = cv::waitKey(3);
    if (key == 'q'|| cv::getWindowProperty("window", cv::WND_PROP_ASPECT_RATIO) < 0) 
    { 
        //do_something
        exit_flag = true;
    }
} while (!exit_flag);

cv::waitKey(0);
cv::destroyWindow("window"); 

我尝试使用cv::EVENT_LBUTTONDOWN,但无法正确使用它来带来任何积极的结果。

如果代码不正确,请原谅,我创建了一个示例用于演示,但我不太擅长 C++。

【问题讨论】:

    标签: c++ opencv computer-vision touchscreen


    【解决方案1】:

    如果你想用鼠标关闭你的imshow窗口,你可以简单地使用setMouseCallback。这是我的方法:您可以通过“q”关键字或简单地单击窗口来关闭窗口:

    #include "opencv2/highgui/highgui.hpp"
    #include <iostream>
    
    using namespace std;
    using namespace cv;
    
    static bool exit_flag = false;
    
    
    static void mouseHandler(int event,int x,int y, int flags,void* param){
        if(event==1)
        exit_flag = true;
    }
    
    
    int main(int argc, char **argv) {
    
        Mat draw_frame = imread("/ur/image/directory/photo.jpg");
        do {
        cv::imshow("window", draw_frame);
        int key = cv::waitKey(3);
    
        setMouseCallback("window",mouseHandler);
    
        if (key == 'q'|| cv::getWindowProperty("window", cv::WND_PROP_ASPECT_RATIO) < 0)
            {
            //do_something
    
            exit_flag = true;
            }
        } while (!exit_flag);
    
        cv::destroyWindow("window");
    }
    

    【讨论】:

    • 感谢您花时间回答我的问题。您提供的代码将适用于我提供的示例。但是我有 detection.exit_flag 而不是 exit_flag ,其中检测是一个结构。你能建议我如何在 mouseHandler 中使用它吗?结构已在主函数中声明。
    • @ShwetaChandel 也许最好通过打开一个新帖子来询问它,因为我无法理解这一点,而且似乎也是一个不同的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多