eta*_*iso 0 c++ opencv callback
我在我的项目中使用C++中的opencv库,我在使用MouseCallback时遇到了问题.
我有一个类BoardCalibration,它有两个数据成员,我需要在回调函数中使用它们.你可以在下面看到这个类:
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);
};
Run Code Online (Sandbox Code Playgroud)
在calibrate()方法中,im调用接收回调my_mouse_callback函数的函数.码:
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;
}
Run Code Online (Sandbox Code Playgroud)
在my_mouse_callback的实现是:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以看到我试图在这里找到_box和_drawingBox成员,但因为它是静态方法,所以它无法识别它们.我怎么解决这个问题??我无法更改my_mouse_callback的原型,否则cvSetMouseCallback不会接受它.我也无法在类之外定义那些数据成员,因为它给了我已经定义的错误.还有什么我可以尝试??? 谢谢.
我对opencv一无所知,但是这样的事情怎么样
struct Helper
{
IplImage * pI;
BoardCalibration * pObj;
};
Rect BoardCalibration::calibrate(Mat& image)
{
.... stuff ....
Helper * p = new Helper;
p->pI = img;
p->pObj = this;
cvSetMouseCallback("Calibration", my_mouse_callback, (void *)p);
.... stuff ...
delete p;
delete img;
return _box;
}
static void BoardCalibration::my_mouse_callback(int event, int x, int y, int flags, void* param)
{
Helper * p = (Helper *)param;
IplImage* image = p->pI;
BoardCalibration * pBC = p->pObj;
switch( event )
{
case CV_EVENT_MOUSEMOVE:
{
if( pBC->_drawingBox ) // use the pBC pointer
... stuff ...
}
... stuff ...
}
Run Code Online (Sandbox Code Playgroud)
我不知道你的代码流程,以确定何时应该删除辅助对象.所以我有delete Helper object代码附近,delete img因为如果该代码是正确的,那么这也可能是deleteHelper对象的正确位置.但你需要检查一下.只有在确定回调将在那时完成为该调用运行时,才需要删除此对象.
| 归档时间: |
|
| 查看次数: |
2368 次 |
| 最近记录: |