简体   繁体   English

如何在OpenCV中将指针指向成员函数传递给setMouseCallback?

[英]How should I pass a pointer-to-member-function to setMouseCallback in OpenCV?

How can call OnMouse defined in a class in setMouseCallback? 如何调用setMouseCallback中的类中定义的OnMouse?

Normally it is like that: 通常是这样的:

cv::setMouseCallback( String, onMouse, 0 );

Here is my program: 这是我的程序:

Camera_Height.h: Camera_Height.h:

class CameraHeight
{

public:
  void onMouse( int, int, int, int, void*);
};

Camera_Height.cpp: Camera_Height.cpp:

void CameraHeight::onMouse( int event, int x, int y, int, void* )
{
   //processing
}

main.cpp: main.cpp中:

How can I use onMouse in setMouseCallback 如何在setMouseCallback中使用onMouse

CameraHeight camh1;
cv::setMouseCallback( String, onMouse, 0 );

setMouseCallback() does not accept a pointer-to-member-function for the callback, it expects a standalone function instead. setMouseCallback()不接受回调的成员函数指针,而是需要一个独立的函数。 As such, if you want to use a class method, you must declare it as static to remove its this pointer. 这样,如果要使用类方法,则必须将其声明为static才能删除其this指针。

To access instance members of your class, the callback needs a pointer to an instance of your class. 要访问您的类的实例成员,回调函数需要一个指向您的类的实例的指针。 The last parameter of setMouseCallback is a user-provided pointer that is passed to the callback: setMouseCallback的最后一个参数是用户提供的指针,该指针传递给回调:

SetMouseCallback SetMouseCallback

Sets mouse handler for the specified window 设置指定窗口的鼠标处理程序

C++: void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata=0 ) C ++: void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata=0 )

C: void cvSetMouseCallback(const char* window_name, CvMouseCallback on_mouse, void* param=NULL ) C: void cvSetMouseCallback(const char* window_name, CvMouseCallback on_mouse, void* param=NULL )

Python: cv.SetMouseCallback(windowName, onMouse, param=None) → None Python: cv.SetMouseCallback(windowName, onMouse, param=None) → None

Parameters: 参数:
- winname – Window name -winname –窗口名称
- onMouse – Mouse callback. -onMouse –鼠标回调。 See OpenCV samples, such as https://github.com/Itseez/opencv/tree/master/samples/cpp/ffilldemo.cpp , on how to specify and use the callback. 有关如何指定和使用回调的信息,请参见OpenCV示例,例如https://github.com/Itseez/opencv/tree/master/samples/cpp/ffilldemo.cpp
- userdata – The optional parameter passed to the callback. -userdata – 传递给回调的可选参数。

You can use that parameter to pass your camh1 object to the callback: 您可以使用该参数将camh1对象传递给回调:

Camera_Height.h: Camera_Height.h:

class CameraHeight
{
public:
  static void onMouse( int evt, int x, int y, int flags, void* param );
};

Camera_Height.cpp: Camera_Height.cpp:

void CameraHeight::onMouse( int evt, int x, int y, int flags, void* param )
{
   CameraHeight *pThis = (CameraHeight*) param; 
   // use pThis as needed...
}

main.cpp: main.cpp中:

CameraHeight camh1;
cv::setMouseCallback( String, &CameraHeight::onMouse, &camh1 );

I would suggest moving setMouseCallback inside the CameraHeight class, where its constructor sets the callback and its destructor clears the callback: 我建议将setMouseCallback移动到CameraHeight类中,在该类的构造函数中设置回调,在其析构函数中清除回调:

Camera_Height.h: Camera_Height.h:

class CameraHeight
{
private:
   string m_winname;
   static void onMouse( int evt, int x, int y, int flags, void* param );
public:
  CameraHeight(const string &winname);
  ~CameraHeight();
};

Camera_Height.cpp: Camera_Height.cpp:

CameraHeight::CameraHeight(const string &winname)
    : m_winname(winname)
{
    cv::setMouseCallback(m_winname, &CameraHeight::onMouse, this);
}

CameraHeight::~CameraHeight()
{
    cv::setMouseCallback(m_winname, NULL, 0);
}

void CameraHeight::onMouse( int evt, int x, int y, int flags, void* param )
{
   CameraHeight *pThis = (CameraHeight*) param; 
   // use pThis as needed...
}

main.cpp: main.cpp中:

CameraHeight camh1( String );

您应该将onMouse静态,然后将其命名为: &CameraHeight::onMouse

@Remy Lebeau Thanks your answer is right, it helps me a lot.. But after that I get an error that a static member function can't call a member variable of the same class! @Remy Lebeau谢谢您的回答是正确的,它对我有很大帮助。但是之后,我得到一个错误,即静态成员函数无法调用同一类的成员变量! But I solve it in this way: 但是我可以这样解决:

void CameraHeight::onMouse( int event, int x, int y, int flags, void* param ) { 
    CameraHeight *anInstance = static_cast<CameraHeight *>(param); 
  }

and to access to CameraHeight instance member inside CameraHeight::onMouse (ex int aa; ): 并访问CameraHeight::onMouse (例如int aa; )中的CameraHeight实例成员:

anInstance->aa;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM