未失真与未失真点,用于校准图像的特征匹配

undistort vs. undistortPoints for feature matching of calibrated images

本文关键字:失真 特征 图像 用于      更新时间:2023-10-16

我试图找到两个相机(或者实际上,一个移动相机)之间的欧几里得变换,捕捉同一场景,其中校准数据K(固有参数)和d(失真系数)是已知的。我通过提取特征点、匹配它们并使用最佳匹配作为对应来做到这一点。

在调整大小/功能检测等之前。我undistort两个图像

undistort(img_1, img_1_undist, K, d);
undistort(img_2, img_2_undist, K, d);

其中img_.imread获得的Mat形式的输入。但实际上,我只需要我最终用作对应关系的特征的未失真坐标,而不是所有图像像素的坐标,因此效率更高,而不是undistort整个图像,而只关键点。我以为我可以用undistortPoints做到这一点,但是这两种方法会导致不同的结果。

我调整图像大小

 resize(img_1_undist, img_1_undist, Size(img_1_undist.cols / resize_factor,
            img_1_undist.rows / args.resize_factor));
 resize(img_2_undist, img_2_undist, Size(img_2_undist.cols / resize_factor,
            img_2_undist.rows / args.resize_factor));
 // scale matrix down according to changed resolution
 camera_matrix = camera_matrix / resize_factor;
 camera_matrix.at<double>(2,2) = 1;

在获得matches的最佳匹配后,我为所述匹配的坐标构建std::vector s,

    // Convert correspondences to vectors
    vector<Point2f>imgpts1,imgpts2;
    cout << "Number of matches " << matches.size() << endl;
    for(unsigned int i = 0; i < matches.size(); i++)
    {
       imgpts1.push_back(KeyPoints_1[matches[i].queryIdx].pt);
       imgpts2.push_back(KeyPoints_2[matches[i].trainIdx].pt);
    }

然后我用它来查找基本矩阵。

    Mat mask; // inlier mask
    vector<Point2f> imgpts1_undist, imgpts2_undist;
    imgpts1_undist = imgpts1;
    imgpts2_undist = imgpts2;
    /* undistortPoints(imgpts1, imgpts1_undist, camera_matrix, dist_coefficients,Mat(),camera_matrix); // this doesn't work */
    /* undistortPoints(imgpts2, imgpts2_undist, camera_matrix, dist_coefficients,Mat(),camera_matrix); */
    Mat E = findEssentialMat(imgpts1_undist, imgpts2_undist, 1, Point2d(0,0), RANSAC, 0.999, 8, mask);

当我删除对undistort的调用,而是在关键点上调用undistortPoints时,它不会产生相同的结果(我所期望的)。差异有时很小,但总是存在的。

我阅读了文档

该函数类似于 cv::

undistort 和 cv::initUndistortRectifyMap,但它在一组稀疏的点而不是光栅图像上运行。

这样函数应该做我期望的。我做错了什么?

您看到的差异是因为不扭曲图像和不扭曲一组点的工作方式非常不同。

使用逆映射

使图像不失真,逆映射与通常用于所有几何图像转换(如旋转)的方法相同。首先创建输出图像网格,然后将输出图像中的每个像素转换回输入图像,并通过插值获取值。

由于输出图像包含"正确"的点,因此您必须"扭曲"它们才能将它们转换为原始图像。换句话说,您只需应用失真方程。

另一方面,如果从输入图像中获取点并尝试消除失真,则需要反转失真方程。这很难做到,因为这些方程是4次或6次多项式。所以undistortPoints使用梯度下降来做数字,这将有一些误差。

总而言之:undistort函数使整个图像不失真,这可能是矫枉过正,但它做得相当准确。如果你只对一小部分点感兴趣,undistortPoints可能会更快,但它也可能有更高的误差。