ZED 1双目相机OpenCV图像校正失败问题求助
ZED 1双目相机OpenCV校正图像异常问题排查
问题背景
使用StereoLabs ZED 1双目相机拍摄左右图像,通过OpenCV执行立体校正后结果异常:一张图像偏向右方,另一张几乎全黑。
所用代码
import numpy as np import cv2 # Load the calibration parameters left_camera_matrix = np.loadtxt('left_camera_matrix.txt') left_distortion_coefficients = np.loadtxt('left_distortion_coefficients.txt') right_camera_matrix = np.loadtxt('right_camera_matrix.txt') right_distortion_coefficients = np.loadtxt('right_distortion_coefficients.txt') rotation_matrix = np.loadtxt('rotation_matrix.txt') translation_vector = np.loadtxt('translation_matrix.txt') # Set up the rectification parameters image_size = (1280,720) # replace with the size of your images R1, R2, P1, P2, Q,roi1,roi2 = cv2.stereoRectify(left_camera_matrix, left_distortion_coefficients, right_camera_matrix, right_distortion_coefficients, image_size, rotation_matrix, translation_vector,1,(0,0)) # the output of stereoRectify is a tuple of 3 elements (R1, R2, P1, P2, Q) #R1, R2, P1, P2, Q = rectify_parameters[:5] left_map1, left_map2 = cv2.initUndistortRectifyMap(left_camera_matrix, left_distortion_coefficients, R1, P1, image_size, cv2.CV_16SC2) right_map1, right_map2 = cv2.initUndistortRectifyMap(right_camera_matrix, right_distortion_coefficients, R2, P2, image_size, cv2.CV_16SC2) # Load the stereo images left_image = cv2.imread('right2.png') right_image = cv2.imread('left2.png') # Rectify the stereo images left_rectified = cv2.remap(left_image, left_map1, left_map2, cv2.INTER_LANCZOS4) right_rectified = cv2.remap(right_image, right_map1, right_map2, cv2.INTER_LANCZOS4) # Display the rectified images (optional) cv2.imshow('Left Rectified', left_rectified) cv2.imshow('Right Rectified', right_rectified) cv2.imwrite('left_rectified.png', left_rectified) cv2.imwrite('right_rectified.png', right_rectified) cv2.waitKey(0) cv2.destroyAllWindows()
相机参数(从ZED官方工具获取)
内参
- 左相机矩阵:
[[1400.34, 0, 976.99], [0, 1400.34, 534.371], [0, 0, 1]] - 左畸变系数:
[-0.1705, 0.0235, 0, 0, 0](k1,k2,p1,p2,k3格式) - 右相机矩阵:
[[1399.7, 0, 991.78], [0, 1399.7, 524.748], [0, 0, 1]] - 右畸变系数:
[-0.172, 0.0253, 0, 0, 0](k1,k2,p1,p2,k3格式)
外参(计算后保存)
- 欧拉角:Rx=0.0028, Ry=0.0145, Rz=0.0005(弧度)
- 平移向量:Tx=120.0, Ty=0.0, Tz=0.0
- 通过Rodrigues转换为旋转矩阵
R并保存,平移矩阵T保存为[[120],[0],[0]]
异常表现
输入为正常左右双目图像,校正后:
- 左校正图像整体偏向右方
- 右校正图像几乎全黑
问题原因及修复方案
1. 图像加载完全反向
代码中把左图像和右图像的加载路径搞反了:
left_image = cv2.imread('right2.png') # 错误:应该读左图像文件 right_image = cv2.imread('left2.png') # 错误:应该读右图像文件
修复:调换加载路径,让left_image对应左图像文件,right_image对应右图像文件。
2. 平移向量单位与方向错误
ZED给出的基线长度Tx=120单位是毫米,但OpenCV的stereoRectify要求平移向量的单位与相机内参的焦距单位一致。内参的fx是像素,实际平移应转换为米(120mm=0.12m),同时ZED右相机相对于左相机的平移方向为负X方向,因此平移向量应为[-0.12, 0, 0]而非[120,0,0]。
修复:修改平移向量计算代码:
Tx = -0.12 # 120mm转换为米,且方向取反 T = np.array([[Tx], [Ty], [Tz]])
重新保存平移矩阵后再运行校正代码。
3. StereoRectify参数优化
代码中stereoRectify的alpha=0会裁剪掉所有畸变区域,可能导致有效图像被裁;flags=1对应cv2.CALIB_ZERO_DISPARITY,强制主点在同一水平线,若外参误差较大易引发偏移。
优化:将alpha设为1保留所有原始像素(可能带黑边),方便观察校正效果:
R1, R2, P1, P2, Q,roi1,roi2 = cv2.stereoRectify(left_camera_matrix, left_distortion_coefficients, right_camera_matrix, right_distortion_coefficients, image_size, rotation_matrix, translation_vector, cv2.CALIB_ZERO_DISPARITY, alpha=1)
内容的提问来源于stack exchange,提问作者Talha Tahir
相关产品推荐
相关产品推荐

