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

  • 本文关键字:cv 转换 Mat 3D c++ opencv
  • 更新时间 :
  • 英文 :


我试图从浮点数的3D向量(vGraph1)填充一个OpenCV Mat (float)。然而,产生的OpenCV Mat (test)没有正确填充,即只有第一列填充正确的值,而其余的是垃圾值(-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();

我设法得到以下工作解决方案;我希望它是正确的。

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

相关内容

  • 没有找到相关文章

最新更新