双目矫正
双目矫正的方法有很多,最常见的就是python+opencv和matlab,我这里用的比较省事的matlab矫正
1.拍摄左右目的图片
我使用的是单usb的双目相机,可以利用python+opencv拍摄图片,下面是拍摄图片的python源代码
import cv2
import os
# Create VideoCapture object for the camera
cap = cv2.VideoCapture(0) # Replace 0 with the appropriate camera index if necessary
# Define the output directory path for left and right images
output_directory = 'images/'
left_image_prefix = 'left_'
right_image_prefix = 'right_'
image_format = '.png'
counter = 0
capture_image = False
# Create the output directory if it doesn't exist
if not os.path.exists(output_directory):
os.makedirs(output_directory)
while True:
# Read frames from the camera
ret, frame = cap.read()
# Split the frame into left and right images
height, width, _ = frame.shape
half_width = width // 2
left_image = frame[:, :half_width]
right_image = frame[:, half_width:]
# Display the left and right images
cv2.imshow('Left Image', left_image)
cv2.imshow('Right Image', right_image)
# Check if 'C' key is pressed to capture images
if cv2.waitKey(1) & 0xFF == ord('c'):
capture_image = True
# Save the left and right images with unique file names if 'C' key is pressed
if capture_image:
left_image_path = os.path.join(output_directory, left_image_prefix + str(counter) + image_format)
right_image_path = os.path.join(output_directory, right_image_prefix + str(counter) + image_format)
cv2.imwrite(left_image_path, left_image)
cv2.imwrite(right_image_path, right_image)
counter += 1
capture_image = False
# Exit the loop if 'Q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the VideoCapture object and close windows
cap.release()
cv2.destroyAllWindows()
将左右目图像保存在当前文件夹下的一个名为"images"的子文件夹中
2.matlab标定
在matlab标定前需要将左右目的图片分别保存到不同的文件夹中
然后打开matlab,这个教程很多,可以看这位博主的教程
matlab标定
3 导出标定数据
标定好后将标定数据导入到工作空间,点击Export Camera Parameters即可。此时我们已经拿到标定数据了,为了避免手工获取数据时出错,运行下面脚本可以直接获取标定数据,并保存到表格文件中,之后直接复制粘贴即可。
rowName = cell(1,10);
rowName{1,1} = '平移矩阵';
rowName{1,2} = '旋转矩阵';
rowName{1,3} = '相机1内参矩阵';
rowName{1,4} = '相机1径向畸变';
rowName{1,5} = '相机1切向畸变';
rowName{1,6} = '相机2内参矩阵';
rowName{1,7} = '相机2径向畸变';
rowName{1,8} = '相机2切向畸变';
rowName{1,9} = '相机1畸变向量';
rowName{1,10} = '相机2畸变向量';
xlswrite('out.xlsx',rowName(1,1),1,'A1');
xlswrite('out.xlsx',rowName(1,2),1,'A2');
xlswrite('out.xlsx',rowName(1,3),1,'A5');
xlswrite('out.xlsx',rowName(1,4),1,'A8');
xlswrite('out.xlsx',rowName(1,5),1,'A9');
xlswrite('out.xlsx',rowName(1,6),1,'A10');
xlswrite('out.xlsx',rowName(1,7),1,'A13');
xlswrite('out.xlsx',rowName(1,8),1,'A14');
xlswrite('out.xlsx',rowName(1,9),1,'A15');
xlswrite('out.xlsx',rowName(1,10),1,'A16');
xlswrite('out.xlsx',stereoParams.TranslationOfCamera2,1,'B1'); % 平移矩阵
xlswrite('out.xlsx',stereoParams.RotationOfCamera2.',1,'B2'); % 旋转矩阵
xlswrite('out.xlsx',stereoParams.CameraParameters1.IntrinsicMatrix.',1,'B5'); % 相机1内参矩阵
xlswrite('out.xlsx',stereoParams.CameraParameters1.RadialDistortion,1,'B8'); % 相机1径向畸变(1,2,5)
xlswrite('out.xlsx',stereoParams.CameraParameters1.TangentialDistortion,1,'B9'); % 相机1切向畸变(3,4)
xlswrite('out.xlsx',stereoParams.CameraParameters2.IntrinsicMatrix.',1,'B10'); % 相机2内参矩阵
xlswrite('out.xlsx',stereoParams.CameraParameters2.RadialDistortion,1,'B13'); % 相机2径向畸变(1,2,5)
xlswrite('out.xlsx',stereoParams.CameraParameters2.TangentialDistortion,1,'B14'); % 相机2切向畸变(3,4)
xlswrite('out.xlsx',[stereoParams.CameraParameters1.RadialDistortion(1:2), stereoParams.CameraParameters1.TangentialDistortion,...
stereoParams.CameraParameters1.RadialDistortion(3)],1,'B15'); % 相机1畸变向量
xlswrite('out.xlsx',[stereoParams.CameraParameters2.RadialDistortion(1:2), stereoParams.CameraParameters2.TangentialDistortion,...
stereoParams.CameraParameters2.RadialDistortion(3)],1,'B16'); % 相机2畸变向量
这里主要是为了拿到两个相机的焦距和基线,从而可以实现视差图转深度图
焦距(Focal Length):
相机1焦距:约为 1102.6
相机2焦距:约为 1100.4
基线(Baseline):
基线值:约为 39.8641