Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble creating a class based implementation of OpenCV's mouseCallback function

As the title suggest, I'm having some trouble implementing OpenCV's mouseCallback function in a class based C++ structure. Allow me to explain. I have defined a class called BriskMatching in which I have created a member function named mouseCallback with the correct parameters that OpenCV requires (please see the code snippet below).

**Briskmatching.h**

class BriskMatching
{
public:
    BriskMatching();
    ~BriskMatching();

public:
    void mouseCallback(int event, int x, int y, int flags, void *param);
};

This is all fine, however my problem arises when I try to set this function as the designated mouse callback function through OpenCV's cv::setMouseCallback function.

In my main function, I create an instance of the BriskMatching class called briskMatcher then when it comes time to set the mouse callback I attempt to do it like so...

cv::setMouseCallback("Matches", BriskMatching::mouseCallback, &matchesImg);

Unfortunately, this throws an error.

Error   3   error C3867: 'BriskMatching::mouseCallback': function call missing argument list; use '&BriskMatching::mouseCallback' to create a pointer to member c:\users\mobilef\documents\visual studio 2010\projects\opencv_objtracking\opencv_briskmatching\main.cpp 54  1   OpenCV_BriskMatching

I could get rid of this error by declaring the mouseCallback function as static in BriskMatching.h but then I get a whole whack of other errors since I refer to many non-static member variables in the mouseCallback function.

So my question to you guys is this. How should I modify my code to be able to properly pass my mouseCallback function declared within the BriskMatching class to the cv::setMouseCallback function?

Thanks for all your help in advanced!

like image 906
szakeri Avatar asked Jul 11 '13 09:07

szakeri


1 Answers

Since a member function takes a this pointer, you will need a static wrapper function. Typically, you use the param parameter to be the address of the object that the member function belongs to, so you end up with something like this:

...
static void mouseCallback(int event, int x, int y, int flags, void *param);

void doMouseCallback(int event, int x, int y, int flags);

And then inside the mouseCallback:

void BriskMatching::mouseCallback(int event, int x, int y, int flags, void *param)
{
    BriskMatching *self = static_cast<BriskMatching*>(param);
    self->doMouseCallback(event, x, y, flags);
}
like image 182
Mats Petersson Avatar answered Oct 25 '22 16:10

Mats Petersson