在Python中使用OpenCV扭曲点时的不良结果 [英] Bad results when undistorting points using OpenCV in Python
问题描述
我在使用OpenCV的Python绑定来校正由校准相机拍摄的图像上的点时遇到麻烦.未失真点的坐标与图像中检测到的原始点完全不同.
I'm having trouble undistorting points on an image taken with a calibrated camera using the Python bindings for OpenCV. The undistorted points have entirely different coordinates than the original points detected in the image.
这是令人讨厌的电话:
undistorted = cv2.undistortPoints(image_points,
camera_matrix,
distortion_coefficients)
其中,image_points
是由cv2.findChessboardCorners
返回的并检测到的棋盘边角的小数组,并经过调整以符合cv2.undistortPoints
的尺寸要求,而camera_matrix
和distortion_coefficients
由cv2.calibrateCamera
返回.
where image_points
is a numpy array of detected chessboard corners returned by cv2.findChessboardCorners
and reshaped to match the dimensional requirements of cv2.undistortPoints
, and camera_matrix
and distortion_coefficients
were returned by cv2.calibrateCamera
.
camera_matrix
和distortion_coefficients
没问题,image_points
也是如此.但是,distorted
似乎与image_points
没有关系.以下是这些值的摘要:
camera_matrix
and distortion_coefficients
seem to me to be okay, and so do image_points
. Nevertheless, distorted
seems to have no relationship to image_points
. Here's a summary of the values:
>>> 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代码,并且一切都匹配,直到我进行该调用为止.怎么了?
I'm working with reference C code and everything matches up until I make that call. What's going wrong?
推荐答案
我认为您忘记了对undistortPoints
的调用中指定了新相机矩阵.如果您查看函数的文档,它表示签名是:
I think you forgot to specify the new camera matrix in your call to undistortPoints
. If you look at the documentation of the function, it says that the signature is:
Python: cv.UndistortPoints(src, dst, cameraMatrix, distCoeffs, R=None, P=None) → None
其中dst
是未失真后的点的数组,并且如果P
是同一性或省略,则它包含归一化的点坐标",即在使用校准矩阵在图像中投影之前.
where dst
is the array of points after undistortion and "if P
is identity or omitted, then it contains normalized point coordinates", meaning before projection in the image using the calibration matrix.
如果将P
设置为cameraMatrix
,则该功能应该可以实现预期的效果.
The function should do what you expect if you set P
to your cameraMatrix
.
这篇关于在Python中使用OpenCV扭曲点时的不良结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!