OpenCV 2.1 托管 C++ 中的 cvSetMouseCallback (CLI/C++)
我的类名称是 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% 正确]
我输入
delegate void MouseCallbackDelegate( int event, int x, int y, int flags, void *param );
in 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)
是否有解决方法或我在这里做错了什么吗???
请帮忙...
My class name is HandMotionRecognition and I'm calling getColorPixel method in mouse callback. This is in OpenCV using Visual Studio 2010 and project type is c++ -> cli.
The standard code (unless I'm mistaken) to handle mouse events is
cvSetMouseCallback( "CameraIn", getColorPixel, (void*) frameHSV);
But when I compile it gives a compile time error
error C3867: 'HandMotionRecognition::getColorPixel': function call missing argument list; use '&HandMotionRecognition::getColorPixel' to create a pointer to member
Then I do as I told and put the code like this...
cvSetMouseCallback( "CameraIn", &HandMotionRecognition::getColorPixel, (void*) frameHSV);
But again I get a compile error..
error C3374: can't take address of 'HandMotionRecognition::getColorPixel' unless creating delegate instance
So i create a delegate like this...[creating delegate..I dont know this is 100% correct]
I put
delegate void MouseCallbackDelegate( int event, int x, int y, int flags, void *param );
in HandMotionRecognition.hI put this code in HandMotionRecognition.cpp instead of
cvSetMouseCallback( "CameraIn", &HandMotionRecognition::getColorPixel, (void*) frameHSV);
MouseCallbackDelegate ^StaticDelInst = gcnew MouseCallbackDelegate(this, &HandMotionRecognition::getColorPixel);
cvSetMouseCallback( "CameraIn", StaticDelInst, (void*) frameHSV);
But it then gives the compile error: (this is the only error I get)
error C2664: 'cvSetMouseCallback' : cannot convert parameter 2 from 'HandMotionRecognition::MouseCallbackDelegate ^' to 'CvMouseCallback'
(As for as I can see..this is due to using cli instead of win32)
Is there a work-around this or am i doing something wrong here???
Please help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所发现的,回调方法必须是静态的(或非成员函数)。这种情况下的标准习惯用法是在
void* param
参数中传递类实例指针,并使用static
函数来调用成员函数。由于您当前使用param
来存储frameHSV
,因此您需要以其他方式传输(例如,将其存储在类实例中)。示例:
并注册:
The callback method has to be static (or a non-member function) as you've discovered. The standard idiom in this case is to pass the class instance pointer in the
void* param
parameter and use astatic
function to call the member function. Since you're currently usingparam
to storeframeHSV
you need to transfer that some other way (e.g. by storing it in your class instance).Example:
And to register: