使用OpenCV在C ++中将std :: list转换为cv :: Mat [英] Convert std::list to cv::Mat in C++ using OpenCV

查看:353
本文介绍了使用OpenCV在C ++中将std :: list转换为cv :: Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SVD解决方程组:cv::SVD::solveZ(A, x);,但是A必须是一个矩阵. OpenCV不提供std::listcv::Mat的任何转换.所以我的问题是,是否存在一种无需先将std::list转换为std::vector的聪明方法.

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.

矩阵A是3xN矩阵.我的列表包含N cv::Point3d元素.

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

我的代码如下:

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.

推荐答案

cv::Mat仅能处理连续存储的数据,因此没有来自std::list的适当转换.但是您可以自己实现它,如下所示:

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

这篇关于使用OpenCV在C ++中将std :: list转换为cv :: Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆