First time here? Check out the FAQ!

Ask Your Question
4

Passing multiple parameters with the setMouseCallback function

asked May 6 '14

updated Nov 30 '0

Is it possible to do?

As of know I am using the following to pass an image to the callback:

static void onMouse( int event, int x, int y, int, void* param)
{
    // Mount back the parameters
    Mat* ptrImage = (Mat*)param;

    //What to do when left button is pressed
    if( event != EVENT_LBUTTONDOWN ){
        // Draw the point on the image here!
        return;
    }
}

namedWindow("draw mask", WINDOW_AUTOSIZE);
setMouseCallback("draw mask", onMouse, (void*)&work_image);

But I would like to add a second pointer to a vector of points to keep storing them also, just don't know how to add multiple parameters. Anyone has any idea without creating global variables?

Preview: (hide)

Comments

1

6345 i edited this question because i wanted it to be on top of the list and seen again. if you think that this post has some useful information please upvote the question and the answer. by this way you will help others to find useful informations in this forum.

sturkmen gravatar imagesturkmen (Aug 23 '17)edit

1 answer

Sort by » oldest newest most voted
7

answered May 6 '14

berak gravatar image

pack anything required into a struct.

struct MouseParams
{
    Mat img;
    Point pt;
    int lucky;
};


static void onMouse( int event, int x, int y, int, void* param)
{
    // Mount back the parameters
    MouseParams* mp = (MouseParams*)param;
    Mat & img = mp->img;
    mp->pt = Point(x,y);
    // ...    

}


int main() 
{
    MouseParams mp;
    setMouseCallback("draw mask", onMouse, (void*)&mp);

    imshow("hi",mp.img);
}
Preview: (hide)

Comments

Yeah figured that out myself but thanks! No way to actually add multiple points and not an extra struct?

StevenPuttemans gravatar imageStevenPuttemans (May 6 '14)edit
1

array of points should work.

Point pts[4]; setMouseCallback("draw mask", onMouse, (void*)pts);

Point * mp = (Point*)param;

berak gravatar imageberak (May 6 '14)edit
1

how to get 4 points in onMouse function?. When i tried to get x,y coordinates of all four points, the contents of pt[0] to pt[4] are all same. How can i specify my first click is for pt[0], second left click is for pt[2], ans so on.?

jamesnzt gravatar imagejamesnzt (Feb 25 '15)edit
2

^^ sorry, forgot about you, will respond to your other question later

berak gravatar imageberak (Feb 25 '15)edit

Question Tools

1 follower

Stats

Asked: May 6 '14

Seen: 16,404 times

Last updated: Aug 23 '17