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

My class name is HandMotionRecognition, and I call the getColorPixel method in the mouse callback. This is in OpenCV using Visual Studio 2010, and the project type is C ++ β†’ cli.

Standard code (if I'm not mistaken) for handling mouse events

cvSetMouseCallback( "CameraIn", getColorPixel, (void*) frameHSV); 

But when I compile, it gives a compile time error

error C3867: "HandMotionRecognition :: getColorPixel": list of missing function arguments; use '& HandMotionRecognition :: getColorPixel' to create a pointer to an element

Then I will do as I said, and put the code like this:

 cvSetMouseCallback( "CameraIn", &HandMotionRecognition::getColorPixel, (void*) frameHSV); 

But again, I get a compilation error.

error C3374: Unable to get address "HandMotionRecognition :: getColorPixel" if delegate instance is not created

So, I create such a delegate ... [creating a delegate..I don’t know that this is 100% correct]

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

  • 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 then it gives a compilation error: (this is the only error I get)

error C2664: 'cvSetMouseCallback': cannot convert parameter 2 from 'HandMotionRecognition :: MouseCallbackDelegate ^' to 'CvMouseCallback'

(As you can see, this is due to using cli instead of win32)

Is there any work around this or am I doing something wrong here?

Please, help...

+3
c ++ opencv delegates mouseevent
source share
1 answer

The callback method must be static (or a non-member function) as you discovered. The standard idiom in this case is to pass the pointer to the class instance in the void* param parameter and use the static function to call the member function. Since you are currently using param to store frameHSV , you need to transfer it in some other way (for example, by storing it in an instance of the class).

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 register:

 HandMotionRecognition* hmr = /* ... */ hmr->setFrameHSV(frameHSV); cvSetMouseCallback("CameraIn", &HandMotionRecognition::mouseCallback, hmr); 
+4
source share

All Articles