我的类名称是 HandMotionRecognition,我在鼠标回调中调用 getColorPixel 方法。这是使用 Visual Studio 2010 的 OpenCV,项目类型是 c++ -> cli。
处理鼠标事件的标准代码(除非我弄错了)是
cvSetMouseCallback( "CameraIn", getColorPixel, (void*) frameHSV);
但是当我编译时它给出了编译时错误
错误 C3867:'HandMotionRecognition::getColorPixel':函数调用缺少参数列表;使用 '&HandMotionRecognition::getColorPixel' 创建指向成员的指针
然后我按照我说的做,并把代码写成这样......
cvSetMouseCallback( "CameraIn", &HandMotionRecognition::getColorPixel, (void*) frameHSV);
但我再次遇到编译错误..
错误 C3374:除非创建委托实例,否则无法获取“HandMotionRecognition::getColorPixel”的地址
所以我创建一个像这样的委托...[创建委托..我不知道这是100%正确的]
I put delegate void MouseCallbackDelegate( int event, int x, int y, int flags, void *param );
在HandMotionRecognition.h中
-
我将此代码放在 HandMotionRecognition.cpp 中而不是cvSetMouseCallback( "CameraIn", &HandMotionRecognition::getColorPixel, (void*) frameHSV);
MouseCallbackDelegate ^StaticDelInst = gcnew MouseCallbackDelegate(this, &HandMotionRecognition::getColorPixel);
cvSetMouseCallback(“CameraIn”,StaticDelInst,(void *)frameHSV);
但它随后给出了编译错误:(这是我得到的唯一错误)
错误 C2664:“cvSetMouseCallback”:无法将参数 2 从“HandMotionRecognition::MouseCallbackDelegate ^”转换为“CvMouseCallback”
(据我所知..这是由于使用了 cli 而不是 win32)
有解决方法吗还是我在这里做错了什么???
请帮忙...
正如您所发现的,回调方法必须是静态的(或非成员函数)。在这种情况下,标准习惯用法是将类实例指针传递给void* param
参数并使用static
函数来调用成员函数。由于您当前正在使用param
储藏frameHSV
您需要以其他方式传输(例如,将其存储在您的类实例中)。
Example:
class HandMotionRecognition {
/* your code */
private:
void getPixelColor(int event, int x, int y, int flags, void* param) {
}
public:
static void mouseCallback(int event, int x, int y, int flags, void* param) {
static_cast<HandMotionRecognition*>(param)->getPixelColor(event, x, y, flags);
}
}
并注册:
HandMotionRecognition* hmr = /* ... */
hmr->setFrameHSV(frameHSV);
cvSetMouseCallback("CameraIn", &HandMotionRecognition::mouseCallback, hmr);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)