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

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

我找到的所有教程都使用setMouseCallback()设置鼠标位置传递到的回调函数。不幸的是,此函数仅在实际鼠标事件发生时调用,但我想在鼠标上没有按下任何键时获取鼠标位置。

这在 OpenCV 中可行吗?

您可以使用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;
}