Ask Your Question
0

Trackbar callback as a member function

asked 2015-08-03 06:32:49 -0600

betacentauri gravatar image

updated 2015-08-03 07:49:33 -0600

I am modeling my whole app with one class, let's call it X. As I intend to have many X objects, I need to use a Videocapture object as a member of each X object and I need to have a member function thereof to act as a callback for GUI trackbars. Plus, my callback needs to access X.cap status. However, C++ pointers to functions as required by createTrackbar() have to be either global or static, thus defeating the idea of having many X objects. Has somebody worked around this problem? StackOverflow and other communities present non-working or non-feasible solutions. Thank you in advance!

EDIT: In all fairness, StackOverflow user Danieloffers a good, similar response I was not able to understand in http://stackoverflow.com/questions/86...

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-08-03 06:52:58 -0600

berak gravatar image

updated 2015-08-03 06:55:55 -0600

since createTrackbar() wants the address of a static function, you need some redirection, pass the 'this' pointer as arg, and later resolve it to your class instance, so you can call a class-member function:

class Capture
{
    String name;
    VideoCapture cap;
    int trackValue;

    static void onChange(int v, void *ptr)
    {
        // resolve 'this':
        Capture *that = (Capture*)ptr;
        that->realTrack(v);
    }

    void realTrack(int v)
    {
        // do something with class members or v;
    }
public:
    Capture(const String &name, int id)
        : name(name)
        , cap(id)
        , trackValue(19)
    {
        namedWindow(name);
        createTrackbar("track1",name,&trackValue,100,onChange,this); // add 'this' as arg here.
    }

    bool nextFrame()
    {
        Mat frame;
        if ( ! cap.read(frame) )
            return false;
        // do something with frame
        putText(frame,format("%d",trackValue),Point(90,90),FONT_HERSHEY_PLAIN,2,Scalar(0,200,0));
        imshow(name, frame);
        if ( waitKey(10)==27 )
            return false;
        return true;
    }
};

int main()
{
    Capture cap("cap1",0);
    while( 1 )
    {
        if (! cap.nextFrame())
            break;
    }
    return 0;
}
edit flag offensive delete link more

Comments

1

===> W O N D E R F U L <===

Sorry to clutter the forum like this but I'm too excited - I've searched so hard for this. THANK YOU.

betacentauri gravatar imagebetacentauri ( 2015-08-03 07:17:49 -0600 )edit
1

I do like the solution given, but you should keep in mind that OpenCV visualisation and trackbars is mainly there for debugging and not for plain GUI building. I always suggest people to use a specific UI based interface like Qt to avoid problems like this :P

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-03 10:38:22 -0600 )edit
1

Thank you Steven, I'm aware of this. The fact is, much of the operation in my app is determined by the kind of control trackbars provide. So I'm doing initial testing with the native GUI elements and then I am confident to deal with the whole under GTK+.

betacentauri gravatar imagebetacentauri ( 2015-08-03 11:55:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-03 06:32:49 -0600

Seen: 4,310 times

Last updated: Aug 03 '15