为什么opencv-python中的相机标定需要30多分钟?

Why does the camera calibration in opencv-python takes more than 30 minutes?

我正在尝试复制 this camera calibration code

当我 运行 我的脚本时,校准过程不会停止大约 30 分钟(我预计输出的固有相机矩阵)。我的代码有什么问题?为什么校准过程需要这么长时间?

这是我的代码

import os
import cv2
import numpy as np 

# DUMMY TEST
FilepathCalib = './data'

# Declaring global variable
img_calib_set = []

points_3d_sample = np.zeros((6*7,3), np.float32)
points_3d_sample[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

points_2d = []
points_3d = []

# Loading calibration image dataset
for img_calib in os.listdir(FilepathCalib):
    img_calib = cv2.imread(FilepathCalib + '/' + img_calib, 0)
    img_calib_set.append(img_calib)

# Finding checkerboard corners pattern in image dataset
for img in img_calib_set:
    ret_val, corner = cv2.findChessboardCorners(img, (7,9), None)

    if ret_val == True:
        points_3d.append(points_3d_sample)
        points_2d.append(corner)

# Extracting camera parameters 
ret_val, intrinsic_mat, dist_coef, rot_vector, tran_vector =         
cv2.calibrateCamera(points_3d, points_2d, img_calib_set[0].shape[::-1], None, None)
print(intrinsic_mat)

这是我的样本校准图像

cv2.findChessboardCorners() 函数的处理时间在很大程度上取决于输入图像。

1K-4K 30 分钟没有异常。 您是否尝试将处理时间写入控制台输出或调试日志?

这样您就可以了解某些图像是否比其他图像花费的时间长很多。我有一个案例,其中大多数图像需要 10-20 秒,但少数需要 3-5 分钟。花费大量时间处理的图片通常具有模糊的棋盘图案(运动模糊),或具有其他矩形背景图案(壁橱、百叶窗、window 框架等...)。

从你的输入图像来看,我可以肯定地看到纸板由于握住它而略微弯曲。我也很难将校准图案粘贴到真正平坦的表面上。最后我用液晶显示器来显示图像,并移动相机来校准图像(确保你没有在显示器上缩放图像;图像上的 1 个像素应该是显示器上的 1 个像素,并且它不一定是全屏)。 这给了我最低的重新投影误差。

您的样本图片质量是否低于实际镜头?您可以清楚地看到 JPEG 压缩算法在图像的边缘创建了大量压缩伪影。这并不理想。如果这是您的实际素材,我建议将压缩质量设置为最大,或使用无损格式,如 PNG。

通过用更好的候选图像替换一些校准图像,我能够将校准时间缩短一半。