I’m using opencv library in C++ in my project and i’m having problems using the MouseCallback.
I have a class BoardCalibration which has two data members, which I need to use them inside the callback function. You can see this class below:
class BoardCalibration{
private:
Rect _box; <-- data members i need to upadte inside the callback function
bool _drawingBox; <--
public:
BoardCalibration();
static void my_mouse_callback(int event, int x, int y, int flags, void* param);
Rect calibrate(Mat& image);
void drawBox(IplImage* img);
};
At the calibrate() method im calling the function which recieves the callback my_mouse_callback function. Code:
Rect BoardCalibration::calibrate(Mat& image){
IplImage * img = new IplImage(image);
namedWindow("Calibration");
IplImage *temp = cvCloneImage(img);
cvSetMouseCallback("Calibration", my_mouse_callback, (void *)img);
while (1){
imshow("Calibration", Mat(img));
cvCopyImage(img,temp);
if( _drawingBox ){
drawBox(temp);
}
imshow("Calibration", Mat(temp));
if (waitKey(1)>=0)
break;
}
cout << "calibrated\n";
delete img;
return _box;
}
And at implementation of my_mouse_callback is:
static void my_mouse_callback(int event, int x, int y, int flags, void* param){
IplImage* image = (IplImage*) param;
switch( event ) {
case CV_EVENT_MOUSEMOVE: {
if( _drawingBox ) {
_box.width = x-_box.x;
_box.height = y-_box.y;
}
}
break;
case CV_EVENT_LBUTTONDOWN: {
_drawingBox = true;
_box = Rect( x, y, 0, 0 );
}
break;
case CV_EVENT_LBUTTONUP: {
_drawingBox = false;
if( _box.width<0 ) {
_box.x+=_box.width;
_box.width *=-1;
}
if( _box.height<0 ) {
_box.y+=_box.height;
_box.height*=-1;
}
//drawBox(image, box); // keep draw on screen
// display rectangle coordinates
cout << "TopLeft: (" << _box.x << "," << _box.y << "), BottomRight: ("
<< _box.x+_box.width << "," << _box.y+_box.height << ")" << endl;
}
break;
}
}
As you can see i’m trying to reach the _box and _drawingBox members here but because it’s static method it does not recognize them.
How can I solve this problem?? I can’t change the prototype of my_mouse_callback otherwise it won’t accepted by cvSetMouseCallback.
I also can’t define those data member outside of the class because its giving me error that they already defined.
Is there anything else I can try???
Thanks.
I don’t know anything about opencv but how about something like this
I don’t know the flow of your code to figure out when exactly the helper object should be deleted. So I have the
delete Helper objectcode near thedelete imgbecause if that code was correct, then this would probably be the right place todeletethe Helper object also. But you need to check that. You need to delete this objects only when you are sure that the callback would have finished running for that call by then.