如何将3D矢量转换为cv :: Mat

时间:2015-06-10 13:57:02

标签: c++ opencv

我试图从浮动的3D矢量(vGraph1)填充OpenCV Mat(浮点)。但是,得到的OpenCV Mat(测试)未正确填充,即只有第一列填充了正确的值,而其余的是垃圾值(-4.32e + 08)。我通过将Mat写入文本文件来确认错误。难道我做错了什么? 好心劝告。 感谢。

int nStates = 9; const int rows = 10; const int cols = 10;
vector < vector < vector<float> > > vGraph1;
 for(int iii = 0; iii < rows; iii++){
     vGraph1.push_back(vector<vector<float> >());
     for(int jjj = 0; jjj < cols; jjj++){
         vGraph1[iii].push_back(vector<float>());
         for(int kkk = 0; kkk < nStates; kkk++){ 
             vGraph1[iii][jjj].push_back(rand());
             cout <<  vGraph1[iii][jjj][kkk] << " ";
         }
         cout << endl;
      }
  }
 cout << "OpenCV Mat \n" << endl;
 Mat test(rows, cols, CV_MAKE_TYPE(CV_32F, nStates));
 for(int iii = 0; iii < rows; iii++){
     float *ptest = test.ptr<float>(iii);
     for( int jjj = 0; jjj < cols; jjj++){
         for (int kkk = 0; kkk < nStates; kkk++){
             ptest[kkk] = vGraph1[iii][jjj][kkk];
             cout << ptest[kkk] << " ";
         }
         cout << endl;
     }

 }
 FileStorage Save("Mat.txt", FileStorage::WRITE);
 Save << "Node" << test;
 Save.release();

1 个答案:

答案 0 :(得分:0)

我设法获得以下工作解决方案;我希望这是对的吗?

int nStates = 9; const int rows = 10; const int cols = 10;
vector < vector < vector<float> > > vGraph1;
 for(int iii = 0; iii < rows; iii++){
     vGraph1.push_back(vector<vector<float> >());
     for(int jjj = 0; jjj < cols; jjj++){
         vGraph1[iii].push_back(vector<float>());
         for(int kkk = 0; kkk < nStates; kkk++){ 
             vGraph1[iii][jjj].push_back(rand());
             cout <<  vGraph1[iii][jjj][kkk] << " ";
         }
         cout << endl;
      }
  }
 cout << "OpenCV Mat \n" << endl;
 Mat test(rows, cols, CV_MAKE_TYPE(CV_32F, nStates));
 for(int iii = 0; iii < rows; iii++){
     float *ptest = test.ptr<float>(iii);
     for( int jjj = 0; jjj < cols; jjj++){
         for (int kkk = 0; kkk < nStates; kkk++){
             ptest[nStates*jjj + kkk] = vGraph1[iii][jjj][kkk];
             cout << ptest[nStates*jjj+kkk] << " ";
         }
         cout << endl;
     }

 }

 FileStorage Save("Mat.txt", FileStorage::WRITE);
 Save << "Node" << test;
 Save.release();