使用 initUndistortRectifyMap 来取消扭曲的图像点

using initUndistortRectifyMap to undistort image points

本文关键字:图像 initUndistortRectifyMap 取消 使用      更新时间:2023-10-16

我有一个应用程序,我必须不断将像素位置不扭曲到它们的"正确"位置。

我首先在 Python 中尝试了它,以下代码产生了良好的结果:

cam_m=np.array([[2590.2742, 0, 997.78192],[0, 2582.9724, 509.76907],[0, 0, 1]])
dist_c=np.array([0.088253155, 0.96952456, 0.0033740622, -0.00064934365, -6.0030732])
map1,map2=cv2.initUndistortRectifyMap(cam_m, dist_c, None, None, (1920,1024), cv2.CV_32FC1)

当我试图将其转换为C++时,它只是返回废话。我打电话给:

cv::initUndistortRectifyMap(this->m_cameraMatrix,this->m_distortionCoeffs, 
         cv::Mat::eye(3, 3, CV_32FC1),cv::Mat::eye(3, 3, CV_32FC1),
         cv::Size(dimX,dimY),CV_32FC1,this->m_undistortMapX,this->m_undistortMapY);

但是在映射中,它返回的值很大,当我输出m_undistortMapX它包含大约 -2.0e+21 的值。 this->m_undistortMapXthis->m_undistortMapY 在类中声明,之前未初始化。其他参数看起来也正确:

std::cout  << this->m_cameraMatrix << std::endl;
std::cout  << this->m_distortionCoeffs << std::endl;
std::cout << dimX <<" / "<<dimY<<std::endl;

输出

[2590.2742, 0, 997.78192;
0, 2582.9724, 509.76907;
0, 0, 1]
[0.088253155, 0.96952456, 0.0033740622, -0.00064934365, -6.0030732]
1920 / 1024

所以就像在Python中一样,我想。任何想法还会出现什么问题?!

试试这个;首先得到最优的新相机矩阵newcam1,然后找到x和y方向map1x和map1y的映射矩阵。最后通过使用不失真功能,您可以获得image_undistorted

Mat newcam1,map1x,map1y;

newcam1 = getOptimalNewCameraMatrix(CM1, Dist1, Size(w, h), 0);
initUndistortRectifyMap(CM1, Dist1, R1, newcam1, Size(w, h), CV_32FC1, map1x, map1y);
undistort(img, image_undistorted, CM1, Dist1, newcam1);

我遇到了类似的问题,偶然发现了你的问题,所以也许这可以帮助某人。另一个答案提供了一个可行的解决方案,但没有解释出了什么问题。

在 CPP 版本中,您为 P 矩阵提供了 None/Identity 矩阵,由于某种原因,OpenCV 允许这样做,尽管它会生成垃圾(生成的映射将使用 1 f.e. 的焦距(。我会尝试看看我是否可以在 OpenCV 中解决这个问题。

我无法评论为什么 python 版本对您有用,特别是由于自 2017 年以来 OpenCV python 绑定中发生的变化。