1 | initial version |
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), 2);
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;
}
2 | No.2 Revision |
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), 2);
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;
}