Ask Your Question

Mouse callback - I wan't to know what I'm doing

asked 2013-03-02 17:03:18 -0500

Jedi gravatar image

updated 2015-07-19 14:24:05 -0500

I took function from book to create callback function and it's work. Fuction specifies the location of the mouse on the image and write to console cordinates.

Function looks like:

      void mouse_ev (int event, int x, int y, int flags, void* param)

      Mat *img = ((Mat *)param);

      switch (event) {

      case EVENT_MOUSEMOVE: cout << "x: " << x << "y: " << y << endl;



and im main function I use it by this:

     setMouseCallback ( window_name, mouse_ev, NULL);

How exactly x and y are sent to mouse_ev function? Is setMouseCallback send all this information to second param of function?

For what is that: Mat *img = ((Mat *)param); line?

My question may seem silly but I want to know 100 percent what I'm doing.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-03-02 20:02:47 -0500

awknaust gravatar image

setmouseCallback only registers a handler for the window. It doesn't actually call mouse_ev itself. Instead when you click on a point, opencv will call 'mouse_ev' with the event type and the coordinates of the point that was clicked on.

The last argument void* allows you to pass additional arguments to the mouse_ev function when it is called, but it isn't clear from your code how that is happening.

edit flag offensive delete link more

answered 2013-03-03 02:33:26 -0500

berak gravatar image

you want to pass your image into the function(the last parameter there), like this:

Mat image = imload("blah");
setMouseCallback ( window_name, mouse_ev, &image);

then, later, if you click into the window, the mousehandler gets called, and you can access your image inside it, like:

Mat img = *((Mat *)param);
Vec3b pixel =<Vec3b>(y,x);
edit flag offensive delete link more



sturkmen gravatar imagesturkmen ( 2015-07-19 14:22:24 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-03-02 17:03:18 -0500

Seen: 1,014 times

Last updated: Mar 03 '13