【问题标题】:Converting vector<Point3d> to a Mat of size (n x 3) and vice versa将 vector<Point3d> 转换为大小为 (n x 3) 的 Mat,反之亦然
【发布时间】:2017-08-24 11:21:04
【问题描述】:

我有Point3d(向量)向量形式的点云。如果我使用 OpenCV 提供的转换,比如

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

它被转换为具有 3 个通道的矩阵。我想要一个尺寸为 nx3 的单通道矩阵(n - 向量中的元素数)。有没有直接的方法将 Point3d 的向量转换成大小为 nx3 的 OpenCV Mat?

现在我在做

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

从 Mat 到 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));
}

我会反复这样做。有没有更快的方法?

【问题讨论】:

    标签: c++ opencv point-clouds


    【解决方案1】:

    找到将 3 Channel Mat 转换为大小为 (nx3) 的单个 Channel Mat 的方法

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

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

    将大小为 nx3 的 Mat 转换为矢量

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

    向量的大小将等于 Mat 的行数

    【讨论】:

      猜你喜欢
      • 2017-01-26
      • 2015-06-28
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多