我只想使用回调方法" setMouseCallback"在openCV中传递一个指针数组

时间:2014-04-04 01:28:17

标签: c++ opencv

这只是代码的关键部分,我想知道如何通过回调方法传递指针数组:

void mouse_callback(int event, int x, int y, int, void *param)
{   
 Mat *mats = (Mat *)param;
 cout << "address of param: " << (Mat *)param << endl;
 cout << "address of mats: " << mats << endl;
 cout << "address of the first value of mats: " << *mats<< endl;
 ...
}
int main(int argc,const char* argv[])
{
 Mat *mats[2] = {&img,&img2};
 cout << "address of mats : "<< mats << endl;
 cout << "address of the first value of mats: " << *mats << endl;
 cout << "address of the first value of mats: " << mats[0] << endl;
 cout << "address of the second value of mats: " << mats[1] << endl;
 cout << "--------------------------" << endl;
 setMouseCallback("Cropping app", mouse_callback,mats);
}

结果会崩溃,我该怎么办呢?

address of mats : 0x7fffd326f9b0
address of the first value of mats: 0x7fffd326f9d0
address of the first value of mats: 0x7fffd326f9d0
address of the second value of mats: 0x7fffd326fa30
--------------------------
address of param: 0x7fffd326f9b0
address of mats: 0x7fffd326f9b0
OpenCV Error: Assertion failed (m.dims <= 2) in writeMat, file /usr/local    
/lib/opencv-    2.4.8/modules/core/src/out.cpp, line 117
terminate called after throwing an instance of 'cv::Exception'
 what():  /usr/local/lib/opencv-2.4.8/modules/core/src/out.cpp:117: error: (-215)    
 m.dims <= 2 in function writeMat
 address of the first value of mats: [Aborted (core dumped)

1 个答案:

答案 0 :(得分:0)

您正在给回调函数Mat**,因此强制转换是错误的。试试这个:

void mouse_callback(int event, int x, int y, int, void *param)
{
  Mat **mats = param;
  cout << "address of mats : "<< mats << endl;
  cout << "address of the first value of mats: " << mats[0] << endl;
  cout << "address of the second value of mats: " << mats[1] << endl;
}