Converting vector<Point3d> to a Mat of size (n x 3) and vice versa

馋奶兔 提交于 2021-01-27 19:32:51

问题


I have point clouds in the form of a vector of Point3d (vector). If I use the conversion provided by OpenCV, like

cv::Mat tmpMat = cv::Mat(pts) //Here pts is vector<cv::Point3d>

It gets converted into a Matrix with 3 channels. I want a single channel matrix with the dimensions nx3 (n - the number of elements in the vector). Is there any direct method to convert a vector of Point3d into an OpenCV Mat of size nx3?

Now I'm doing

cv::Mat1f tmpMat = cv::Mat::zeros(pts.size(), 3, cv::CV_32FC1);
for(int i = 0; i< pts.size(); ++i)
{
    tmpMat(i, 0) = pts[i].x;
    tmpMat(i, 1) = pts[i].y;
    tmpMat(i, 2) = pts[i].z;
}

from Mat to vector of Point3d

vector<cv::Point3d> pts;
for (int i = 0; i < tmpMat.rows; ++i)
{
    pts.push_back(cv::Point3d(tmpMat(i, 0), tmpMat(i, 1), tmpMat(i, 2));
}

I will be doing this repeatedly. Is there any faster method?


回答1:


Found the method to convert a 3 Channel Mat to a single Channel Mat of size (nx3) at

http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat-reshape

cv::Mat tmpMat = cv::Mat(pts).reshape(1);

to convert from a Mat of size nx3 to vector

vector<cv::Point3d> pts;
tmpMat.reshape(3, tmpMat.rows*tmpMat.cols).copyTo(pts);

the size of the vector will be the equal to the number of rows of the Mat



来源:https://stackoverflow.com/questions/45860306/converting-vectorpoint3d-to-a-mat-of-size-n-x-3-and-vice-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!