在 Python 中使用 OpenCV 对点进行不失真时的结果很糟糕



我无法使用OpenCV的Python绑定在校准相机拍摄的图像上不失真点。未失真的点与图像中检测到的原始点具有完全不同的坐标。

这是违规的电话:

undistorted = cv2.undistortPoints(image_points,
                                  camera_matrix,
                                  distortion_coefficients)

其中image_points是由cv2.findChessboardCorners返回并重新调整以匹配cv2.undistortPoints的尺寸要求的检测到的棋盘角的numpy数组,camera_matrixdistortion_coefficientscv2.calibrateCamera返回。

在我看来,camera_matrixdistortion_coefficients都没问题,image_points也是如此。尽管如此,distorted似乎与image_points没有任何关系。以下是这些值的摘要:

>>> image_points
array([[[ 186.95303345,  163.25502014]],
       [[ 209.54478455,  164.62690735]],
       [[ 232.26443481,  166.10734558]],
       ..., 
       [[ 339.03695679,  385.97784424]],
       [[ 339.20108032,  400.38635254]],
       [[ 339.13067627,  415.30780029]]], dtype=float32)
>>> undistorted
array([[[-0.19536583, -0.07900728]],
       [[-0.16608481, -0.0772614 ]],
       [[-0.13660771, -0.07537176]],
       ..., 
       [[ 0.00228534,  0.21044853]],
       [[ 0.00249786,  0.22910291]],
       [[ 0.00240568,  0.24841554]]], dtype=float32)
>>> camera_matrix
array([[ 767.56947802,    0.        ,  337.27849576],
   [   0.        ,  767.56947802,  224.04766824],
   [   0.        ,    0.        ,    1.        ]])
>>> distortion_coefficients
array([[ 0.06993424, -0.32645465,  0.        ,  0.        , -0.04310827]])

我正在使用参考 C 代码,在我做出该调用之前,一切都匹配。出了什么问题?

我想你忘了在调用undistortPoints时指定新的相机矩阵。如果您查看函数的文档,它说签名是:

Python: cv.UndistortPoints(src, dst, cameraMatrix, distCoeffs, R=None, P=None) → None

其中dst是未失真后的点数组,"如果P是恒等或省略,则它包含归一化的点坐标",这意味着在使用校准矩阵在图像中投影之前。

如果您将P设置为cameraMatrix,该函数应执行您的预期操作。

最新更新