Ask Your Question
4

Passing multiple parameters with the setMouseCallback function

asked 2014-05-06 08:31:17 -0600

updated 2020-11-30 03:28:50 -0600

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?

edit retag flag offensive close merge delete

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 ( 2017-08-23 12:45:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
7

answered 2014-05-06 09:12:20 -0600

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);
}
edit flag offensive delete link more

Comments

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

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-06 09:13:47 -0600 )edit
1

array of points should work.

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

Point * mp = (Point*)param;

berak gravatar imageberak ( 2014-05-06 09:22:35 -0600 )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 ( 2015-02-25 02:42:46 -0600 )edit
2

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

berak gravatar imageberak ( 2015-02-25 02:54:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-05-06 08:31:17 -0600

Seen: 16,222 times

Last updated: Aug 23 '17