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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With