cvSetMouseCallback in OpenCV 2.1 Managed C++ (CLI/C++)

北城余情 提交于 2019-12-23 20:05:12

问题


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...


回答1:


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


来源:https://stackoverflow.com/questions/6489457/cvsetmousecallback-in-opencv-2-1-managed-c-cli-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!