Search code examples
c++opencvcallbacktrackbar

OpenCV trackbar callback in C++ class


I have a question about how to define the callback for trackbars in OpenCV when working with classes in C++.

When I define my trackbar let's say in the constructor method of my .cpp class how can I define the callback?

I have been trying to work with function pointers but it doesn't work out. I guess I must be doing something very wrong :-)

This is my header file:

class SliderwithImage {

public:
    SliderwithImage(void);
    ~SliderwithImage(void); 

    void sliderCallBack(int pos);
};

This is the implementation file:

#include "SliderwithImage.h"

void SliderwithImage::sliderCallBack(int pos) {


}

SliderwithImage::SliderwithImage(void)  {

    const char* windowName = "window";
    int lowvalue  =1;

    namedWindow(windowName,  CV_GUI_EXPANDED);

    createTrackbar("mytrackbar", windowName, &lowvalue, 255, sliderCallBack);

}

SliderwithImage::~SliderwithImage(void) {

}

Obviously the createTrackbar method does not recognize sliderCallBack... I guess it's a problem of scope. But I am not sure how to solve this?

Any help would be appreciated.

Thank you very much.


Solution

  • You have to implement the callback function either as a global function or a static member function. To make it more OOP look, you might prefer to implement it as a static member function:)