Quantcast

Member function callback in cvCreateTrackbar ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Member function callback in cvCreateTrackbar ?

Michah Lerner
Can we use a member function as the callback to cvCreateTrackbar (see
page 508 of the opencv.pdf)?


/*** main.cpp ****/
#include "stdafx.h"
#include "Tclass.h"

int _tmain(int argc, _TCHAR* argv[]) {
   void (*func)(int i) = NULL;
   func = &Tclass::process_image;
   int val, Tclass::* x = &Tclass::slider_pos;
   cvCreateTrackbar("Size","window",&val,255,func);
   cvCreateTrackbar("Size","window",&val,255,&Tclass::process_image);
}

error C2440: '='
: cannot convert from 'void (__thiscall Tclass::* )(int)' to 'void
(__cdecl *)(int)'
error C2664: 'cvCreateTrackbar'
: cannot convert parameter 5 from 'void (__thiscall Tclass::* )(int)' to
'CvTrackbarCallback'



/*** Tclass.h ***/
#pragma once
#define CV_NO_BACKWARD_COMPATIBILITY
#include "cv.h"
#include "highgui.h"
using namespace cv;

class Tclass {
     public:
         int slider_pos;
         Ptr<IplImage> img;
         void handler(int);

         Tclass(int size);
         virtual ~Tclass(void);
         virtual void process_image(int h);
     };




[Non-text portions of this message have been removed]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Resolved (was [Re: Member function callback in cvCreateTrackbar ?])

Michah Lerner
Issue  resolved by static singletons, which seems good enough given the
C/C++ interactions are complicated enough already.

#pragma once
#include "cv.h"
#include "highgui.h"
using namespace cv;

class Tclass {
     IplImage* img;
     static int *static_slider_pos;
     static IplImage *static_img;
     static int priorSliderPos;

public:
     Tclass(int size);
     virtual ~Tclass(void);
     static int *getSliderPos() { return static_slider_pos; }
     static void static_process_image(int h);
     int slider_pos;
     };


--- In [hidden email], "michahlerner" <lerner@...> wrote:

>
> Can we use a member function as the callback to cvCreateTrackbar (see
> page 508 of the opencv.pdf)?
>
>
> /*** main.cpp ****/
> #include "stdafx.h"
> #include "Tclass.h"
>
> int _tmain(int argc, _TCHAR* argv[]) {
>    void (*func)(int i) = NULL;
>    func = &Tclass::process_image;
>    int val, Tclass::* x = &Tclass::slider_pos;
>    cvCreateTrackbar("Size","window",&val,255,func);
>    cvCreateTrackbar("Size","window",&val,255,&Tclass::process_image);
> }
>
> error C2440: '='
> : cannot convert from 'void (__thiscall Tclass::* )(int)' to 'void
> (__cdecl *)(int)'
> error C2664: 'cvCreateTrackbar'
> : cannot convert parameter 5 from 'void (__thiscall Tclass::* )(int)'
to

> 'CvTrackbarCallback'
>
>
>
> /*** Tclass.h ***/
> #pragma once
> #define CV_NO_BACKWARD_COMPATIBILITY
> #include "cv.h"
> #include "highgui.h"
> using namespace cv;
>
> class Tclass {
>      public:
>          int slider_pos;
>          Ptr<IplImage> img;
>          void handler(int);
>
>          Tclass(int size);
>          virtual ~Tclass(void);
>          virtual void process_image(int h);
>      };
>
>
>
>
> [Non-text portions of this message have been removed]
>



[Non-text portions of this message have been removed]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Resolved (was [Re: Member function callback in cvCreateTrackbar ?])

elektrischerpinguin

Hey, 
I'm digging out this old posting as I've been battling around with that
problem for a day now and want to point out a nice solution.



Didn't want to mess my class by making functions and accessed member
data static.
Statics became more and more and I really wanted to get rid of it.


Played around with boost, mem_fun, bind... etc.
Didn't get it to work.



Finally I became aware that there's a

cvCreateTrackbar2() function (!!!), nowhere documented and like it seems
by noone ever used.

It let's you pass userdata with it's last parameter.
So you can simply pass a 'this' pointer to the Callback function...





static void onChange(int para, void* obj)
{
  MyClass* myObj = (MyClass*) obj; //recast

  myObj->mData = ... * para; // do something with the slider's value
}



void createMyWindows()
{
 ...
 cvCreateTrackbar2("slider_name", "window_name", 0, 1,
&MyClass::onChange, this);
 ...
}





... will do the trick and only the callback function itself needs to be
static... but that's okay.


Loading...