Convert std::list to cv::Mat in C++ using OpenCV

2019-09-15 04:21发布

问题:

I'm trying to solve an equation system using SVD: cv::SVD::solveZ(A, x);, but A needs to be a Matrix. OpenCV doesn't offer any convertion of a std::list to cv::Mat. So my question is, whether there is a smart way to convert it without having to convert the std::list to a std::vector before.

The Matrix A is a 3xN matrix. My list contains N cv::Point3d elements.

My code looks something like this:

std::list<cv::Point3d> points; // length: N
cv::Mat A = cv::Mat(points).reshape(1); // that's how I do it with a std::vector<cv::Point3d>
cv::Mat x;
cv::SVD::solveZ(A, x); // homogeneous linear equation system Ax = 0

If anybody has an idea about it, then please tell me.

回答1:

cv::Mat can handle only continously stored data, so there are no suitable conversion from std::list. But you can implement it by yourself, as follows:

std::list<cv::Point3d> points;
cv::Mat matPoints(points.size(), 1, CV_64FC3);
int i = 0;
for (auto &p : points) {
    matPoints.at<cv::Vec3d>(i++) = p;
}
matPoints = matPoints.reshape(1);