undistort vs. undistortPoints用于校准图像的特征匹配

时间:2015-06-18 15:42:17

标签: c++ opencv computer-vision feature-detection camera-calibration

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

在调整大小/特征检测/等之前。我undistort两张图片

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

其中img_.是由Mat获得的imread形式的输入。但实际上我只需要最终用作对应的特征的未失真坐标,而不是所有图像像素的坐标,因此它更有效,不是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

    // 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,但它在稀疏的点集上操作而不是光栅图像。

这样功能应该达到我的预期。 我做错了什么?

1 个答案:

答案 0 :(得分:6)

您会看到这种差异,因为对图像进行无失真并且不会失真一组点的工作方式会有很大不同。

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

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

另一方面,如果从输入图像中取点并尝试去除失真,则需要反转失真方程。这很难做到,因为这些方程是4阶或6阶多项式。所以undistortPoints使用渐变下降来进行数字化,这会产生一些误差。

总结一下:undistort函数不会影响整个图像,这可能是一种矫枉过正,但它可以相当准确地完成。如果您只对一小组积分感兴趣,undistortPoints可能会更快,但也可能会出现更高的错误。

相关问题