oar*_*ish 1 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);
Run Code Online (Sandbox Code Playgroud)
表格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;
Run Code Online (Sandbox Code Playgroud)
在获得最佳匹配后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);
}
Run Code Online (Sandbox Code Playgroud)
然后我用它来寻找基本矩阵.
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);
Run Code Online (Sandbox Code Playgroud)
当我删除调用undistort并调用undistortPoints关键点时,它不会产生相同的结果(我期望).差异有时很小,但始终存在.
我看了文档
该函数类似于cv :: undistort和cv :: initUndistortRectifyMap,但它在稀疏的点集上操作而不是光栅图像.
这样功能应该达到我的预期.我究竟做错了什么?
你看到了这种差异,因为不对图像进行不失真并且不失真的一组点的工作方式非常不同.
使用逆映射不会失真图像,这与通常用于所有几何图像变换(例如旋转)的方法相同.首先创建输出图像网格,然后将输出图像中的每个像素转换回输入图像,并通过插值获取值.
由于输出图像包含"正确"点,因此必须"扭曲"它们以将它们转换为原始图像.换句话说,您只需应用失真方程.
另一方面,如果从输入图像中取点并尝试去除失真,则需要反转失真方程.这很难做到,因为这些方程是4阶或6阶多项式.因此,undistortPoints这是否数字,使用梯度下降,这将有一定的误差.
总结一下:该undistort函数不会影响整个图像,这可能是一种矫枉过正,但它可以相当准确地完成.如果你只对一小组点感兴趣,undistortPoints可能会更快,但也可能有更高的误差.