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

时间:2021-03-10 04:26:05

I am trying to fill up an OpenCV Mat (float) from a 3D vector of floats (vGraph1). However, the resulting OpenCV Mat (test) is not correctly filled, i.e. only the first column is filled with correct values while the rest are junk values (-4.32e+08). I confirmed the error by writting the Mat to a text file. Am I doing something wrong? kindly advice. Thanks.

我试图从浮动的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 个解决方案

#1


0  

I manage to get the following working solution; I hope it is correct?

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

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();

#1


0  

I manage to get the following working solution; I hope it is correct?

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

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();