c++ - How to convert a 3D vector to a cv::Mat -
i trying fill opencv mat (float) 3d vector of floats (vgraph1). however, resulting opencv mat (test) not correctly filled, i.e. first column filled correct values while rest junk values (-4.32e+08). confirmed error writting mat text file. doing wrong? kindly advice. thanks.
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++){ (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();
i manage following working solution; hope 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++){ (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();
Comments
Post a Comment