OPENCV:标定相机的重投影误差与自定义计算结果不一致

8 投票
1 回答
10366 浏览
提问于 2025-04-18 07:11

我有一个用Python写的脚本,它使用calibratecamera2这个方法来校准相机,方法是通过几张棋盘格的照片来实现。校准成功后,我会拿到所有原始的点,然后做一些图表,并重新计算重投影误差。让我感到惊讶的是,OpenCV计算出来的重投影误差和我自己算的有点不一样。我觉得这很奇怪。我是不是计算错了呢?

obj_points = []# 3d point in real world space. List of arrays
img_points = []# 2d points in image plane. List of arrays

...

ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), camera_matrix, dist_coeffs, rvecs, tvecs, calib_flags +cv2.CALIB_USE_INTRINSIC_GUESS, criteria)
print "Final reprojection error opencv: ", ret   #Compute mean of reprojection error
tot_mean_error=0
mean_error_image = 0
for i in xrange(len(obj_points)):
    reprojected_points, _ = cv2.projectPoints(obj_points[i], rvecs[i], tvecs[i], camera_matrix, dist_coeffs)
    reprojected_points=reprojected_points.reshape(-1,2)
    mean_error_image=np.sum(np.sum(np.abs(img_points[i]-reprojected_points)**2,axis=-1)**(1./2))/np.alen(reprojected_points)
    tot_mean_error +=mean_error_image

mean_error=tot_mean_error/len(obj_points)
print "Mean reprojection error: ", mean_error

OpenCV计算的最终重投影误差:0.571030279037

我计算的平均重投影误差:0.438696960449

1 个回答

8

我之前计算得不对,或者说用的方法不一样。我原本用的是这种公式:

formula

但是 OpenCV 用的是这个公式:

fomrula_opencv

所以,如果有人感兴趣,现在的代码看起来是这样的:

#Compute mean of reprojection error
tot_error=0
total_points=0
for i in xrange(len(obj_points)):
    reprojected_points, _ = cv2.projectPoints(obj_points[i], rvecs[i], tvecs[i], camera_matrix, dist_coeffs)
    reprojected_points=reprojected_points.reshape(-1,2)
    tot_error+=np.sum(np.abs(img_points[i]-reprojected_points)**2)
    total_points+=len(obj_points[i])

mean_error=np.sqrt(tot_error/total_points)
print "Mean reprojection error: ", mean_error

最终的重投影误差(opencv):0.571030279037

平均重投影误差:0.571030718956

撰写回答