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

无人久伴 提交于 2019-12-12 02:57:14

问题


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


来源:https://stackoverflow.com/questions/30478316/convert-stdlist-to-cvmat-in-c-using-opencv

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