1.注意:使用matlab矫正时,camera1一定对应左,camera2一定对应右!!!!
stereo_camera_config
import cv2
import numpy as np
#stereoParams.CameraParameters1.IntrinsicMatrix
#ans'
right_camera_matrix = np.array([[372.9499 , 0, 339.9497],
[0 ,371.2997, 289.0286],
[0 , 0 , 1.0000]])
#stereoParams.CameraParameters1
#顺序:RadialDistortion[0],RadialDistortion[1],TangentialDistortion[0],TangentialDistortion[1],RadialDistortion[2]
right_distortion = np.array([[-0.0303 ,0.0886,0,0 -0.0646]])
#使用时,需要注意参数的排放顺序,即K1,K2,P1,P2,K3。切记不可弄错,否则后续的立体匹配会出现很大的偏差。
#stereoParams.CameraParameters2.IntrinsicMatrix
#ans'
left_camera_matrix = np.array([[368.3098 , 0 ,357.8760],
[0 ,367.6755 ,248.2351],
[0 , 0 ,1.0000]])
#stereoParams.CameraParameters2
left_distortion = np.array([[0.0163, -0.0502,0,0 ,0.0247 ]])
#stereoParams
R = np.matrix([
[ 0.997114422566844,-0.013467397037999,0.074709152894473],
[ 0.012913244142177,0.999885447384289,0.007895583249125],
[ -0.074806927720054,-0.006908062401299,0.997174108287488],
])
# print(R)
T = np.array([3.151885008307438e+02,0.262068133059456,8.907585308658991]) # 平移关系向量
size = (640, 480) # 图像尺寸
# 进行立体更正
R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify(left_camera_matrix, left_distortion,
right_camera_matrix, right_distortion, size, R,
T)
# 计算更正map
left_map1, left_map2 = cv2.initUndistortRectifyMap(left_camera_matrix, left_distortion, R1, P1, size, cv2.CV_16SC2)
right_map1, right_map2 = cv2.initUndistortRectifyMap(right_camera_matrix, right_distortion, R2, P2, size, cv2.CV_16SC2)
# print(Q)
根据标定好的参数,进行矫正
import cv2
# 根据标定数据对图片进行重构消除图片的畸变
img1_rectified = cv2.remap(frame1, camera_configs.left_map1, camera_configs.left_map2, cv2.INTER_LINEAR)
img2_rectified = cv2.remap(frame2, camera_configs.right_map1, camera_configs.right_map2, cv2.INTER_LINEAR)