Ask Your Question
1

OpenCV setMouseCallback

asked 2013-03-01 02:21:44 -0500

boFFeL gravatar image

Hi,

i am trying to set a mouseCallback function using openCV within an ros application.

So i defined a function prototype within my class defintion object_tracker looking like this:

class object_tracker
{    
public:
    ...
private:
    void onMouse( int event, int x, int y, int flags, void* param );

Function itself looks like this:

void object_tracker::onMouse( int event, int x, int y, int flags, void* param )
{
...
}

When i now set the mouseCallback

setMouseCallback( "CamShift Demo", object_tracker::onMouse, 0 );

it says:

/home/namco/ros/ object_tracker/src/object_tracker.cpp:323: Fehler:argument of type ‘void ( object_tracker::)(int, int, int, int, void)’ does not match ‘cv::MouseCallback {aka void ( *)(int, int, int, int, void)}’

But when i use this with normal cpp, everything works fine. What's wrong?

edit retag flag offensive close merge delete

Comments

Hi boFFeL, did you solve this problem? I met the same issue as you... Best regards K

Kanzhi Wu gravatar imageKanzhi Wu ( 2014-04-06 22:50:08 -0500 )edit

2 Answers

Sort by » oldest newest most voted
-1

answered 2014-11-24 02:35:13 -0500

Wolf gravatar image

OpenCV's setMouseCallback does not support calling into non-static class member functions ( http://docs.opencv.org/modules/highgu... ).

A work around for this could be declaring a static method that calls into the non-static method:

Declaration:

class object_tracker
{    
public:
    ...
private:
    static void onMouseStatic( int event, int x, int y, int flags, void* that );
    void onMouse( int event, int x, int y, int flags );

Definition:

void object_tracker::onMouseStatic( int event, int x, int y, int flags, void* that )
{
    that->onMouse( event,  x, y, flags );
}

void object_tracker::onMouse( int event, int x, int y, int flags )
{
...
}

And add this rather than 0 if you want to the callback:

setMouseCallback( "CamShift Demo", object_tracker::onMouseStatic, this );
edit flag offensive delete link more

Comments

I now got: ‘void*’ is not a pointer-to-object type that->onMouse( event, x, y, flags );

Markus gravatar imageMarkus ( 2018-05-23 04:17:33 -0500 )edit

In theory, this should not work. When using a void pointer, you can't just dereference. You first need to type-cast it. At least, that is how I know it works. The other answer is actually type-casting it, that is why it is correct.

Metalzero2 gravatar imageMetalzero2 ( 2019-06-27 10:09:23 -0500 )edit
1

answered 2018-05-23 07:44:35 -0500

Markus gravatar image

updated 2018-05-23 07:45:56 -0500

So I have a quite similar solution that worked for me:

in the main.cpp file:

 void BallTrackingNode::mouseHandleStatic(int event, int x, int y, int flags, void* param )
 {
     BallTrackingNode* thiz = static_cast<BallTrackingNode*>(param);
     thiz->onMouse(event, x, y, flags);
 }


void BallTrackingNode::onMouse( int event, int x, int y, int flags)
{
    if (rect_done_)
      return;
    switch (event)
    {
    case cv::EVENT_MOUSEMOVE:
    {
      region_rect_.width = x - region_rect_.x;
      region_rect_.height = y - region_rect_.y;
    }
    break;
    case cv::EVENT_LBUTTONDOWN:
    {
      region_rect_ = cv::Rect(x, y, 0, 0);
      draw_box_ = true;
    }
    break;
    case cv::EVENT_LBUTTONUP:
    {
      draw_box_ = false;
      if (region_rect_.width < 0)
      {
        region_rect_.x += region_rect_.width;
        region_rect_.width *= -1;
      }
      if (region_rect_.height < 0)
      {
        region_rect_.y += region_rect_.height;
        region_rect_.height *= -1;
      }
      rect_done_ = true;
    }
    }
}

call it with

cv::setMouseCallback(select_cloud_area_window_name_, BallTrackingNode::mouseHandleStatic, (void*)this);

in the header:

public:
  static void mouseHandleStatic( int event, int x, int y, int flags, void* that );
  void onMouse( int event, int x, int y, int flags);
edit flag offensive delete link more

Comments

Works beautifully.

Metalzero2 gravatar imageMetalzero2 ( 2019-06-27 10:09:41 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-03-01 02:21:44 -0500

Seen: 1,504 times

Last updated: May 23 '18