OpenCV 2.1 托管 C++ 中的 cvSetMouseCallback (CLI/C++)

发布于 2024-11-17 07:31:50 字数 1535 浏览 2 评论 0原文

我的类名称是 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% 正确]

  1. 我输入 delegate void MouseCallbackDelegate( int event, int x, int y, int flags, void *param ); in HandMotionRecognition.h

  2. 我将此代码放入 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]

  1. I put delegate void MouseCallbackDelegate( int event, int x, int y, int flags, void *param ); in HandMotionRecognition.h

  2. I 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2024-11-24 07:31:50

正如您所发现的,回调方法必须是静态的(或非成员函数)。这种情况下的标准习惯用法是在 void* param 参数中传递类实例指针,并使用 static 函数来调用成员函数。由于您当前使用 param 来存储 frameHSV,因此您需要以其他方式传输(例如,将其存储在类实例中)。

示例:

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);

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 a static function to call the member function. Since you're currently using param to store frameHSV you need to transfer that some other way (e.g. by storing it in your class instance).

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);       
  }
}

And to register:

HandMotionRecognition* hmr = /* ... */
hmr->setFrameHSV(frameHSV);
cvSetMouseCallback("CameraIn", &HandMotionRecognition::mouseCallback, hmr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文