如何在OpenCV的calibrateCamera中固定畸变系数为0?
如何在OpenCV的calibrateCamera中固定畸变系数为0?
我正在学习OpenCV的calibrateCamera函数,目前希望将畸变系数固定为0,暂不考虑畸变因素。
# reprojection error, calibration matrix, distortion, rotation and translation vectors ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera( [3d_points.astype(np.float32)], [image_points.astype(np.float32)], (img_raw.shape[1], img_raw.shape[0]), None, distCoeffs = np.zeros((5, 1),dtype=np.float32))
但输出中仍得到非零的畸变系数:
dist array([[-0.06402103], [ 0.76996236], [ 0.05764124], [ 0.02921579], [-1.12958531]])
请问如何固定畸变系数,使其不被优化?
解决方案
在OpenCV中,calibrateCamera函数默认会对传入的distCoeffs进行优化,仅传入零矩阵无法锁定其值。要固定畸变系数为0,需通过设置标定标志位实现:
- 使用
cv2.CALIB_FIX_K1至cv2.CALIB_FIX_K5标志,分别对应固定第1到第5个畸变系数;若要固定全部5个系数为0,需将这些标志位按位或组合。 - 配合传入初始值为零的
distCoeffs矩阵,即可在标定过程中强制保持畸变系数不被优化。
修改后的代码如下:
# 组合标志位:固定所有5个畸变系数 flags = cv2.CALIB_FIX_K1 | cv2.CALIB_FIX_K2 | cv2.CALIB_FIX_K3 | cv2.CALIB_FIX_K4 | cv2.CALIB_FIX_K5 ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera( [3d_points.astype(np.float32)], [image_points.astype(np.float32)], (img_raw.shape[1], img_raw.shape[0]), None, distCoeffs=np.zeros((5, 1), dtype=np.float32), flags=flags )
执行上述代码后,输出的dist将保持为初始的零矩阵,不会被优化调整。
若仅需忽略径向畸变(前3个系数),可仅设置对应标志位,例如:
flags = cv2.CALIB_FIX_K1 | cv2.CALIB_FIX_K2 | cv2.CALIB_FIX_K3
内容的提问来源于stack exchange,提问作者Mike Azatov
相关产品推荐
相关产品推荐

