Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 472
iFreilicht Avatar asked Feb 02 '17 15:02

iFreilicht


Video Answer


1 Answers

You can use EVENT_MOUSEMOVE to get the position of the mouse:

#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;
}
like image 167
Miki Avatar answered Nov 10 '22 17:11

Miki