【问题标题】:using initUndistortRectifyMap to undistort image points使用 initUndistortRectifyMap 去扭曲图像点
【发布时间】:2017-04-13 13:11:49
【问题描述】:

我有一个应用程序,我经常需要将像素位置不失真到它们的“正确”位置。

我首先在 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 中一样,我想。有什么想法还会出错吗?!

【问题讨论】:

    标签: python c++ opencv computer-vision camera-calibration


    【解决方案1】:

    试试这个;首先得到最优的新相机矩阵newcam1,然后找到x和y方向的映射矩阵ma​​p1xma​​p1y。最后使用 undistort 函数,可以得到 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);
    

    【讨论】:

    • 好吧,undistort 与我的原始相机矩阵配合得很好(我不得不承认也来自 Python)。我想生成一个查找表来查找我使用视觉/检测算法找到的坐标的未失真位置。不扭曲/重新映射整个图像不是这里的选项,因为它是实时的,这将花费太长时间。但是我看不到 getOptimalNewCameraMatrix 会如何改变 map1x 和 map1y 中只有废话的情况?
    • 是否只需要对特定像素坐标进行畸变校正并获取它们的新坐标?
    • 是的,没错。但在 15fps 时,每帧大约有 30 到 40 个坐标。
    【解决方案2】:

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

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

    我无法评论为什么 python 版本对你有用,尤其是因为自 2017 年以来 OpenCV python 绑定发生了变化。

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      相关资源
      最近更新 更多