简体   繁体   English

我可以在没有鼠标事件的情况下获得OpenCV中的鼠标位置吗?

[英]Can I get the mouse position in OpenCV without a mouse event?

All the tutorials I find use setMouseCallback() to set a callback function to which the mouse position is passed. 我发现的所有教程都使用setMouseCallback()设置将鼠标位置传递到的回调函数。 Unfortunately, this function is only called when an actual mouse event is occurring, but I'd like to get the mouse position while no keys on my mouse are pressed. 不幸的是,仅在发生实际鼠标事件时才调用此函数,但是我想在没有按下鼠标键的同时获取鼠标位置。

Is that possible in OpenCV? 在OpenCV中有可能吗?

You can use EVENT_MOUSEMOVE to get the position of the mouse: 您可以使用EVENT_MOUSEMOVE获取鼠标的位置:

#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

void mouse_callback(int  event, int  x, int  y, int  flag, void *param)
{
    if (event == EVENT_MOUSEMOVE) {
        cout << "(" << x << ", " << y << ")" << endl;
    }
}

int main()
{
    cv::Mat3b img(200, 200, Vec3b(0, 255, 0));

    namedWindow("example");
    setMouseCallback("example", mouse_callback);

    imshow("example", img);
    waitKey();

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM