c++ - Convert a std::vector<std::vector <double> > representing a 2D array to cv::Mat -
what elegant , efficient way convert nested std::vector of std::vectors cv::mat? nested structure holding array, i.e. inner std::vectors have same size , represent matrix rows. don't mind data being copied 1 another.
i know single, non-nested std::vector easy, there constructor:
std::vector <double> myvec; cv::mat mymat; // fill myvec bool copy = true; mymat = cv::mat(myvec, copy); what nested vector?
this way it:
std::vector<double> row1; row1.push_back(1.0); row1.push_back(2.0); row1.push_back(3.0); std::vector<double> row2; row2.push_back(4.0); row2.push_back(5.0); row2.push_back(6.0); std::vector<std::vector<double> > vector_of_rows; vector_of_rows.push_back(row1); vector_of_rows.push_back(row2); // make sure size of of row1 , row2 same, else you'll have serious problems! cv::mat my_mat(vector_of_rows.size(), row1.size(), cv_64f); (size_t = 0; < vector_of_rows.size(); i++) { (size_t j = 0; j < row1.size(); j++) { my_mat.at<double>(i,j) = vector_of_rows[i][j]; } } std::cout << my_mat << std::endl; outputs:
[1, 2, 3; 4, 5, 6] i tried approach using constructor of mat , push_back() method without success, maybe can figure out. luck!
Comments
Post a Comment