正在做一个鼠标绘图程序,在一个class中需要调用鼠标响应函数setMouseCallback,
由于setMouseCallback中的mousecallback不支持调用non-static function,遇到了很多问题,现总结解决方法。
myclass.h中
class MyClass
{
private:
<span style="white-space:pre"> </span>void on_Mouse(int event, int x, int y);
static void onMouse(int event, int x, int y, int, void* userdata);
}
myclass.cpp中
void MyClass::onMouse(int event, int x, int y, int, void* userdata)
{
// Check for null pointer in userdata and handle the error
MyClass* temp = reinterpret_cast<MyClass*>(userdata);
temp->on_Mouse(event, x, y);
}
void MyClass::on_Mouse(int event, int x, int y)
{
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
//your code here
break;
case CV_EVENT_MOUSEMOVE:
//your code here
break;
case CV_EVENT_LBUTTONUP:
//your code here
break;
}
}在cpp函数中调用setMouseCallback时采用:

在创建鼠标绘图程序时,遇到在类中调用OpenCV的setMouseCallback函数的问题,由于setMouseCallback不支持非静态成员函数,通过在类中巧妙使用this关键字解决了这个问题。
最低0.47元/天 解锁文章
4137





