You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV鱼眼相机标定失败及畸变校正效果不佳求助

鱼眼相机标定启用CALIB_RECOMPUTE_EXTRINSIC失败,校正效果不佳求优化

我正在用OpenCV基于棋盘格图像做鱼眼相机标定,启用cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC标志时标定直接失败,只能暂时跳过这个标志。但得到的标定参数做畸变校正时效果很差——校正后的图像边缘仍有明显畸变,棋盘格直线无法完全校正为直线。

我也尝试过普通相机标定,结果同样不理想,校正后的图像还是存在明显的畸变残留。

因为后续需要用这些畸变参数做恒星和星型图案检测,所以希望能尽可能优化标定效果。

以下是我的标定代码:

chessboardsize = (13, 9)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1)
flags = ( cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_NORMALIZE_IMAGE + cv2.CALIB_CB_FILTER_QUADS)
# calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC + cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW
calibration_flags = cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW

objp = np.zeros((1, chessboardsize[0]*chessboardsize[1], 3), np.float32)
objp[0, :, :2] = np.mgrid[0:chessboardsize[0], 0:chessboardsize[1]].T.reshape(-1, 2)
_img_shape = None

objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('calibration_frames/*.jpg')  # Images

if len(images) == 0:
    raise ValueError("No images found in the specified directory.")

for fname in images:
    
    img = cv2.imread(fname)
    if img is None:
        print(f"Image {fname} cannot be read.")
        continue  # Skip if the image is not read properly
    if _img_shape == None:
        _img_shape = img.shape[:2]
    else:
        assert _img_shape == img.shape[:2], "All images must share the same size."

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, chessboardsize, flags)
    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)
        cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        imgpoints.append(corners)
        
        cv2.drawChessboardCorners(img, chessboardsize, corners, ret)
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        plt.figure(figsize=(10, 7))
        plt.imshow(img_rgb)
        plt.title(f'Chessboard Corners: {fname}')
        plt.axis('off')
        plt.show()
    else:
        print(f"Chessboard corners not found in {fname}")

if len(objpoints) == 0 or len(imgpoints) == 0:
    raise ValueError("No valid images found for calibration.")

N_OK = len(objpoints)
K = np.zeros((3, 3))
D = np.zeros((4, 1))
rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]
tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]

 # Perform fisheye calibration
try:
    rms, _, _, _, _ = cv2.fisheye.calibrate(
        objpoints,
        imgpoints,
        _img_shape[::-1],
        K,
        D,
        rvecs,
        tvecs,
        calibration_flags,  # cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC throws error
        (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
    )

    print("Found " + str(N_OK) + " valid images for calibration")
    print("DIM=" + str(_img_shape[::-1]))
    print("K=np.array(" + str(K.tolist()) + ")")
    print("D=np.array(" + str(D.tolist()) + ")")

except cv2.error as e:
    print("Calibration failed:", e)

内容的提问来源于stack exchange,提问作者B.S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 06:42:14