4

In OpenCV I want to return the point position like Point(x,y) to the main() function that I click on the image in the mouse callback function . Is there anyway other than setting a global variable?

I don't want to write all the codes inside the on_mouse() function.

Thanks

3 Answers 3

6

to expand Safirs idea there, apart from a class or such, you could just pass in the point itself:

void on_mouse( int e, int x, int y, int d, void *ptr )
{
    Point*p = (Point*)ptr;
    p->x = x;
    p->y = y;
}

Point p;
namedWindow("win");
setMouseCallback("win",on_mouse, (void*)(&p) );

// changed value of p will be accessible here 
Sign up to request clarification or add additional context in comments.

2 Comments

I hate to raise the dead here, but can you tell me what the 4th parameter (int d) is? I can't seem to find a good explanation of the params for the mouse callbacks.
mouse state (e.g, when you move the mouse while one is pressed)
3

You can avoid using global variables by passing a pointer to your data as a parameter to setMouseCallback(). Agree with @berek, just wanted to show a full example below to avoid confusion about global variables.

using namespace cv; 

void on_mouse( int e, int x, int y, int d, void *ptr )
{
    Point*p = (Point*)ptr;
    p->x = x;
    p->y = y;
}

in main() {
    Point p;
    namedWindow("window");
    Mat image = imread("someimage.jpg");
    imshow(image);


    //pass a pointer to `p` as parameter
    setMouseCallback("window",on_mouse, &p ); 

    // p will update with new mouse-click image coordinates 
    // whenever user clicks on the image window 
}

Comments

-2

No, this isn't possible, since the on_mouse() is a callback function. Here is the opencv documentation of it.

So, "global" variables are the only way to solve this problem. Alternatively, if you're looking for a nicer solution, you can create a wrapper class in which you have the namedWindow and the MouseCallback and a private member variable, which is manipulated when mouse callback function is called.

8 Comments

Thanks. Then the only way is to set a global variable? Why OpenCV has this setting?
Yes. Or the wrapper class I suggested. opencv is mainly for image processing it doesn't have a reach GUI. It only supports simple user interface: opencv documentation for user interface.
I see. But wrapping a class for just a point position... Thanks anyway
This is not correct. You can return a value from on_mouse(). Just pass a pointer your data (cv::Point in this case) as the parameter to on_mouse(). No need for the wrapper class. See the answer by @berak.
Yes. However the Point still must be "global", in the scope of main function. For simple testing programmes, I'd go with @berak solution. However, if your application gets slightly bigger, I think it's better to create a wrapper class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.