Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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:

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);
}

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);
}

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

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

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