【问题标题】:Convert Keypoints to cv::Mat in OpenCV with C++ for TriangulatePoints在 OpenCV 中使用 C++ 为 TriangulatePoints 将关键点转换为 cv::Mat
【发布时间】:2021-05-07 19:06:44
【问题描述】:

我是 CV 领域的新手,我试图在三张图片之间对点进行三角剖分,但一开始,我想在两张图片之间进行三角剖分。 为此,我做了以下步骤:

  1. 使用 AKAZE 进行特征检测
  2. NORM_HAMMING 的特征匹配
  3. 过滤匹配项
  4. 将过滤后的描述符与关键点坐标匹配

之后,对于每个相同大小的图像,我都有一个关键点向量。 知道我想将此向量转换为“cv::Mat”,以便我可以使用 TriangulatPoints 函数:

std::vector<cv::KeyPoint>::iterator it_1;
std::vector<cv::Point2f> points_1;

for(it_1 = keypoints_matched_1.begin() ; it_1!=keypoints_matched_1.end() ; it_1++)
{
    points_1.push_back(it_1->pt);
}
cv::Mat pointmatrix1(points_1);



std::vector<cv::KeyPoint>::iterator it_2;
std::vector<cv::Point2f> points_2;

for(it_2 = keypoints_matched_2.begin() ; it_2!=keypoints_matched_2.end() ; it_2++)
{
    points_2.push_back(it_2->pt);
}
cv::Mat pointmatrix2(points_2);

std::vector<cv::Mat> points3D;
std::vector<cv::Mat> points_all={pointmatrix1, pointmatrix2};
cv::sfm::triangulatePoints(points_all,Proj_matrices,points3D);

我一直遇到这个错误:

错误:(-215:Assertion failed) points2d_tmp[i].rows == 2 && points2d_tmp[i].cols == n_points in function 'triangulatePoints'

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    pointmatrix1 pointmatrix2 应该是包含浮点数的 2xN 矩阵。
    现在它们是包含 cv::Point2f 元素的 1xN 矩阵;

    试试这样的方法:

        cv::Mat pointmatrix1(2, keypoints_matched_1.size(), CV_32F);
        int column = 0;
        for (auto& kp: keypoints_matched_1) {
            pointmatrix1.at<float>(0,column) = kp.pt.x;
            pointmatrix1.at<float>(1,column) = kp.pt.y;
            column++;
        }
    

    【讨论】:

    • 错误改为:error: (-215:Assertion failed) d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0] *sizes[1] == 0) 在函数'create'中
    • 检查doc。您的输入有很多条件。 keypoints_matched_1.size() 是否等于 keypoints_matched_2.size() 并且都严格为正?另外,points3D 不应该是一个 cv::Mat,triangulatePoints() 将调整为 3xN?
    • 非常感谢!我将:std::vector&lt;cv::Mat&gt; points3D; 更改为 cv::Mat points3D(3,keypoints_matched_1.size() ,CV_32F); 现在看起来可以正常工作了
    猜你喜欢
    • 1970-01-01
    • 2022-06-25
    • 2017-06-03
    • 1970-01-01
    • 2017-12-24
    • 2018-04-16
    • 2011-06-28
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多