Ask Your Question
0

Do I need an OpenCV function to read cursor position?

asked 2017-12-06 12:41:31 -0600

miner_tom gravatar image

Hi to the forum.

35 years ago, when I was working in the Image Processing industry (yes, I am that old) we made our own cursors from lookup tables. Now, I am working with OpenCV and I have seen how I can draw lines and circles (probably more complex shapes as well) using OpenCV functions (or do you call them modules?).

I am going to generate my own cursor using OpenCV on an OpenCV modified image (matrix). One such that I can change the cursor position on the bounded image by using a joystick or mouse. All that I need now is a way to read the cursor X/Y coordinates.

Now, there are a number of MS Windows functions that are used to get cursor coordinates and display them. Two problems that I see with using these MS c++ functions:

  1. I don't necessarily wish to use the MS operating system long term for the final version.
  2. I can see that if I have a camera derived image/matrix from OpenCV, that the XY coordinates received by the MS function might not (probably will not) be aligned with the XY matrix generated by OpenCV.

The above reasons are why I am looking for an OpenCV function to give me cursor coordinates derived from an OpenCV image.

Thank You

Tom

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2017-12-07 06:24:04 -0600

kbarni gravatar image

updated 2017-12-08 05:08:36 -0600

I'm not sure if I understood you correctly, but maybe you want to use the mouse callbacks. The following code will display a crosshair at the mouse position:

#include <opencv2/opencv.hpp>

using namespace cv;

Mat img;

static void onMouse(int event,int x,int y,int,void*)
{
    //this function will be called every time you move your mouse over the image
    // the coordinates will be in x and y variables
    Mat img2;img.copyTo(img2);
    line(img2,Point(x,0),Point(x,img2.cols),Scalar(0,0,255),2);
    line(img2,Point(0,y),Point(img2.rows,y),Scalar(0,0,255),2);
    imshow("Image",img2);
}

void main()
{
    img=imread("lena.jpg");
    namedWindow("Image",WINDOW_NORMAL|WINDOW_KEEPRATIO);
    imshow("Image",img);
    setMouseCallback("Image",onMouse);
    waitKey();
}

x and y are image coordinates, not in screen coordinates (so they follow zoom, etc). The event variable contains events like mouse clicks. For more information check the highgui module (you can add overlays, texts, trackbars, buttons, etc).

You can also get the value of the actual pixel using img.at<Vec3b>(y,x) (replace Vec3b for other image types).

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-12-06 12:41:31 -0600

Seen: 6,339 times

Last updated: Dec 08 '17