相机标定原理
文章目录
- 相机标定原理
- 前言
- 一、采集图像
- 二、基于Matlab单双目标定流程
- 采集棋盘图
- 三、基于OpenCV-Python双目标定流程
- 检测棋盘格角点
- 对角点进行亚像素精细化
- 单目标定
- 双目标定
- 双目校正
- 保存标定参数
- 读取标定参数
- 代码示例
- 参考
前言
相机标定可以说是计算机视觉/机器视觉的基础,也是面试过程中经常出现的问题。相机标定涉及的知识面很广,成像几何、镜头畸变、单应矩阵、非线性优化等。在双目测距系统中,相机标定能消除畸变,进行立体校正,从而提高视差计算的准确性,这样才能得到精确的深度图。
首先需要准备一张棋盘,如下图所示。对于标定不同测距范围相机所用的棋盘方格宽度会有所不同。对于短焦双目相机(测距范围在20m以内),棋盘中方格的宽度达到20mm即可;对于长焦双目相机(测距范围在40m左右),棋盘中方格的宽度需要尽量大,否则会影响标定的精度,一般至少达到60mm。
尽量拍摄多组照片,这样可以提高标定效果,标定效果的好坏直接影响到测距的精度。对于短焦相机通常拍摄40组照片即可;长焦相机通常会需要更多组照片,标定长焦相机时拍摄了60组。
一、采集图像
如果使用的是左右图像拼接1个USB接口输出的双目摄像头,在采集图像时需要注意其图像拼接的顺序,例如我使用的双目摄像头拼接顺序从左至右为右目——左目(不知道是不是都是这么拼接的,还是厂家搞错了),这个问题处理不好可能会导致后面的处理结果都不对,所以这里特别说明一下。
import os
import argparse
import cv2
class StereoCamera(object):
"""采集双目标定图片,按键盘【c】或【s】保存图片"""
def __init__(self, chess_width, chess_height, detect=False):
"""
:param chess_width: chessboard width size,即棋盘格宽方向黑白格子相交点个数,
:param chess_height: chessboard height size,即棋盘格长方向黑白格子相交点个数
:param detect: 是否实时检测棋盘格,方便采集数据
"""
self.chess_width = chess_width
self.chess_height = chess_height
self.detect = detect
self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
def detect_chessboard(self, image):
"""检测棋盘格并显示"""
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (self.chess_width, self.chess_height), None)
if ret:
# 角点精检测
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), self.criteria)
# Draw and display the corners
image = cv2.drawChessboardCorners(image, (self.chess_width, self.chess_height), corners2, ret)
return image
def capture2(self, left_video, right_video, save_dir):
"""
用于采集双USB连接线的双目摄像头
:param left_video:int or str,左路视频路径或者摄像头ID
:param right_video:int or str,右视频路径或者摄像头ID
:param save_dir: str,保存左右图片的路径
:return:
"""
self.create_file(save_dir)
capL = cv2.VideoCapture(left_video)
capR = cv2.VideoCapture(right_video)
widthL, heightL, numFramesL, fpsL = self.get_video_info(capL)
widthR, heightR, numFramesR, fpsR = self.get_video_info(capR)
print("capL:\n", widthL, heightL, numFramesL, fpsL)
print("capR:\n", widthR, heightR, numFramesR, fpsR)
save_videoL = self.create_file(save_dir, "video", "left_video.avi")
save_videoR = self.create_file(save_dir, "video", "right_video.avi")
writerL = self.get_video_writer(save_videoL, widthL, heightL, fpsL)
writerR = self.get_video_writer(save_videoR, widthR, heightR, fpsR)
i = 0
while True:
isuccessL, frameL = capL.read()
isuccessR, frameR = capR.read()
if not (isuccessL and isuccessR):
print("No more frames")
break
if self.detect:
l = self.detect_chessboard(frameL.copy())
r = self.detect_chessboard(frameR.copy())
else:
l = frameL.copy()
r = frameR.copy()
cv2.imshow('left', l)
cv2.imshow('right', r)
key = cv2.waitKey(10)
if key == ord('q'):
break
elif key == ord('c') or key == ord('s'):
print("save image:{:0=3d}".format(i))
cv2.imwrite(os.path.join(save_dir+"/left", "left_{:0=3d}.png".format(i)), frameL)
cv2.imwrite(os.path.join(save_dir+"/right", "right_{:0=3d}.png".format(i)), frameR)
i += 1
writerL.write(frameL)
writerR.write(frameR)
capL.release()
capR.release()
cv2.destroyAllWindows()
def capture1(self, video, save_dir):
"""
用于采集单USB连接线的双目摄像头(左右摄像头被拼接在同一个视频中显示)
:param video:int or str,视频路径或者摄像头ID
:param save_dir: str,保存左右图片的路径
"""
self.create_file(save_dir)
cap = cv2.VideoCapture(video)
width, height, numFrames, fps = self.get_video_info(cap)
print("capL:\n", width, height, numFrames, fps)
save_videoL = self.create_file(save_dir, "video", "left_video.avi")
save_videoR = self.create_file(save_dir, "video", "right_video.avi")
writerL = self.get_video_writer(save_videoL, int(width / 2), height, fps)
writerR = self.get_video_writer(save_videoR, int(width / 2), height, fps)
i = 0
while True:
isuccess, frame = cap.read()
if not isuccess:
print("No more frames")
break
# 分离左右摄像头
# 注意:需要根据摄像头画面分割左右图像
frameL = frame[:, int(width / 2):, :]
frameR = frame[:, :int(width / 2), :]
if self.detect:
l = self.detect_chessboard(frameL.copy())
r = self.detect_chessboard(frameR.copy())
else:
l = frameL.copy()
r = frameR.copy()
cv2.imshow('left', l)
cv2.imshow('right', r)
key = cv2.waitKey(10)
if key == ord('q'):
break
elif key == ord('c') or key == ord('s'):
print("save image:{:0=3d}".format(i))
cv2.imwrite(os.path.join(save_dir+"/left", "left_{:0=3d}.png".format(i)), frameL)
cv2.imwrite(os.path.join(save_dir+"/right", "right_{:0=3d}.png".format(i)), frameR)
i += 1
writerL.write(frameL)
writerR.write(frameR)
cap.release()
cv2.destroyAllWindows()
@staticmethod
def get_video_info(video_cap):
width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
numFrames = int(video_cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = int(video_cap.get(cv2.CAP_PROP_FPS))
return width, height, numFrames, fps
@staticmethod
def get_video_writer(save_path, width, height, fps):
if not os.path.exists(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
frameSize = (int(width), int(height))
video_writer = cv2.VideoWriter(save_path, fourcc, fps, frameSize)
print("video:width:{},height:{},fps:{}".format(width, height, fps))
return video_writer
@staticmethod
def create_file(parent_dir, dir1=None, filename=None):
out_path = parent_dir
if dir1:
out_path = os.path.join(parent_dir, dir1)
if not os.path.exists(out_path):
os.makedirs(out_path)
if filename:
out_path = os.path.join(out_path, filename)
return out_path
def get_parser():
width = 12
height = 8
left_video = 0
right_video = -1
save_dir = "data/"
parser = argparse.ArgumentParser(description='Camera calibration')
parser.add_argument('--detect', type=bool, default=True, help='detect corner')
parser.add_argument('--width', type=int, default=width, help='chessboard width size')
parser.add_argument('--height', type=int, default=height, help='chessboard height size')
parser.add_argument('--left_video', type=int, default=left_video, help='left video file or camera ID')
parser.add_argument('--right_video', type=int, default=right_video, help='right video file or camera ID')
parser.add_argument('--save_dir', type=str, default=save_dir, help='YML file to save calibrate matrices')
return parser
if __name__ == '__main__':
args = get_parser().parse_args()
stereo = StereoCamera(args.width, args.height, detect=args.detect)
if args.left_video > -1 and args.right_video > -1:
# 双USB连接线的双目摄像头
stereo.capture2(left_video=args.left_video, right_video=args.right_video, save_dir=args.save_dir)
elif args.left_video > -1:
# 单USB连接线的双目摄像头(左右摄像头被拼接在同一个视频中显示)
stereo.capture1(video=args.left_video, save_dir=args.save_dir)
else:
raise Exception("Error: Check your camera{}".format(args.left_video, args.right_video))
二、基于Matlab单双目标定流程
采集棋盘图
尽量让棋盘占据照片中最多的画面(不小于1\3)
尽量让棋盘格出现在图像中的各个位置(尤其图像的四个角)
拍摄的棋盘图是具有多个角度的(前倾,后倾,左倾,右倾,斜倾等)
拍摄的棋盘图要清晰可辩,最好是在最清晰的范围内拍摄
拍摄照片数量要多一点,推荐20张以上
https://blog.csdn.net/leonardohaig/article/details/81254179
三、基于OpenCV-Python双目标定流程
双目标定的目的是获取左右目相机的内参矩阵、畸变向量、旋转矩阵和平移矩阵。除了Matlab的标定工具箱之外,OpenCV同样也实现了张友正标定法,而我们只需要调用相关的函数即可对相机进行标定。
双目相机标定步骤:
检测棋盘格角点
retL, cornersL = cv2.findChessboardCorners(ChessImaL,(self.width, self.height), cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS) # 提取左图每一张图片的角点
retR, cornersR = cv2.findChessboardCorners(ChessImaR,(self.width, self.height), cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS) # 提取右图每一张图片的角点
对角点进行亚像素精细化
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
cv2.cornerSubPix(ChessImaL, cornersL, (11, 11), (-1, -1), criteria) # 亚像素精确化,对粗提取的角点进行精确化
cv2.cornerSubPix(ChessImaR, cornersR, (11, 11), (-1, -1), criteria) # 亚像素精确化,对粗提取的角点进行精确化
单目标定
# 左侧相机单独标定
retL, K1, D1, rvecsL, tvecsL = cv2.calibrateCamera(objpoints,imgpointsL,ChessImaL.shape[::-1], None, None)
# 右侧相机单独标定
retR, K2, D2, rvecsR, tvecsR = cv2.calibrateCamera(objpoints,imgpointsR,ChessImaR.shape[::-1], None, None)
双目标定
criteria_stereo = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-5)
flags = 0
flags |= cv2.CALIB_FIX_INTRINSIC
# 内参、畸变系数、平移向量、旋转矩阵
retS, K1, D1, K2, D2, R, T, E, F = cv2.stereoCalibrate(objpoints,imgpointsL,imgpointsR,K1,D1,K2,D2,ChessImaR.shape[::-1], criteria_stereo,flags)
我们要注意函数中的flags
CV_CALIB_FIX_INTRINSIC:固定K和D矩阵。这是默认标志。如果你校准好你的相机,那就只求解𝑅,𝑇,𝐸,𝐹。
CV_CALIB_USE_INTRINSIC_GUESS: K和D个矩阵将被优化。对于这个计算,你应该给出经过良好校准的矩阵,以便(可能)得到更好的结果。
CV_CALIB_FIX_PRINCIPAL_POINT: 修复K矩阵中的参考点。
CV_CALIB_FIX_FOCAL_LENGTH: 在K矩阵中固定焦距。
CV_CALIB_FIX_ASPECT_RATIO: 固定长宽比。
CV_CALIB_SAME_FOCAL_LENGTH: 校准焦距,并设置Fx和Fy相同的校准结果。
CV_CALIB_ZERO_TANGENT_DIST: 去掉畸变。
CV_CALIB_FIX_K1, …, CV_CALIB_FIX_K6: 移除K1到K6的畸变。
双目校正
双目校正的目的是得到立体校正所需的映射矩阵,然后对图像进行畸变校正和立体校正(极线校正)。立体校正最常见的校正方法就是Bouguet极线校正方法。
Bouguet极线校正方法:左右相机成像平面各旋转一半,使得左右图像重投影造成的误差最小,左右视图的共同面积最大。
# 左校正变换矩阵、右校正变换矩阵、左投影矩阵、右投影矩阵、深度差异映射矩阵
R_l,R_r,P_l,P_r,Q, roi_left, roi_right = cv2.stereoRectify(K1, D1, K2, D2,(width, height),R, T,flags=cv2.CALIB_ZERO_DISPARITY, alpha=0.9)
在这个函数中,只有一个标志CALIB_ZERO_DISPARITY,它用于匹配图像之间的y轴。alpha值用于转换后的黑色部分,因为图像会旋转,而显示的图像大小不会改变,所以一些图像边缘部分会是黑色的,而原始图像会小得多:
alpha= -1: 让 OpenCV 优化黑色部分。
alpha= 0 : 旋转和裁切图像,使没有黑色的部分。这个选项在大多数情况下会严重削减图像,你不会得到一个像样的高质量的图像,但可以一试。
alpha= 1 : 进行变换,但不要裁切任何部分。
alpha= experimental: 尝试设置不同的值,在某个特定的alpha值,可能有一些黑色的区域,但图像质量整体高。
# 计算畸变矫正和立体校正的映射变换。
map_lx, map_ly = cv2.initUndistortRectifyMap(K1, D1, R_l, P_l,(width,height),cv2.CV_32FC1)
map_rx, map_ry = cv2.initUndistortRectifyMap(K2, D2, R_r, P_r, (width,height),cv2.CV_32FC1)
# 得到畸变校正和立体校正后的图像
rec_img_L = cv2.remap(imgL,map_lx, map_ly, cv2.INTER_LINEAR, cv2.BORDER_CONSTANT) # 使用remap函数完成映射
rec_img_R = cv2.remap(imgR,map_rx, map_ry, cv2.INTER_LINEAR, cv2.BORDER_CONSTANT)
initUndistortRectifyMap函数可以同时实现图像的畸变校正和校准。对于左相机,我们使用K1(相机矩阵)和D1(失真矩阵)进行畸变校正,使用R1(从左到右旋转)和P1(从左到右投影矩阵)进行校正。在对remap进行变换后,我们将得到修正后的图像。对于右相机,我们会用相同的步骤做一遍。至此双目校正部分就完成了。
标定效果如下所示。可以看到原图中左右图像存在畸变,并且棋盘格位置明显未对齐;校正后左右图像畸变被消除,且极线对齐效果明显。
保存标定参数
为方便查看和读取,首先将需要保存的参数放到一个空字典中,然后保存为json文件。如下所示:
params_dict = {}
params_dict['size'] = [width, height]
params_dict['K1'] = left_K.tolist()
params_dict['D1'] = left_D.tolist()
params_dict['K2'] = right_K.tolist()
params_dict['D2'] = right_D.tolist()
params_dict['left_map_x'] = map_lx.tolist()
params_dict['left_map_y'] = map_ly.tolist()
params_dict['right_map_x'] = map_rx.tolist()
params_dict['right_map_y'] = map_ry.tolist()
params_dict['R'] = R.tolist()
params_dict['T'] = T.tolist()
params_dict['Q'] = Q.tolist()
# 保存为.json文件
file_path = args.save_dir + args.file_name + ".json"
with open(file_path,"w") as f:
json.dump(params_dict, f, indent=1)
读取标定参数
# 读取.json文件
with open(params_file, "r") as f:
dict = json.load(f)
for d in dict:
dict[d] = np.asarray(dict[d], "f")
代码示例
#-*- coding:utf-8 -*-
import os
import numpy as np
import cv2
import glob
import argparse
import json
import pickle
class Stereo_Camera_Calibration(object):
def __init__(self, width, height, lattice):
self.width = width # 棋盘格宽方向黑白格子相交点个数
self.height = height # 棋盘格长方向黑白格子相交点个数
self.lattice = lattice
# 设置迭代终止条件
self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
self.criteria_stereo = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-5)
# =========================== 双目标定 =============================== #
def stereo_calibration(self, file_L, file_R):
# 设置 object points, 形式为 (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((self.width * self.height, 3), np.float32) #我用的是6×7的棋盘格,可根据自己棋盘格自行修改相关参数
objp[:, :2] = np.mgrid[0:self.width, 0:self.height].T.reshape(-1, 2)
objp *= self.lattice
# 用arrays存储所有图片的object points 和 image points
objpoints = [] # 3d points in real world space
imgpointsR = [] # 2d points in image plane
imgpointsL = []
for i in range(len(file_L)):
ChessImaL = cv2.imread(file_L[i],0) # 左视图
ChessImaR = cv2.imread(file_R[i],0) # 右视图
retL, cornersL = cv2.findChessboardCorners(ChessImaL,(self.width, self.height), cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS) # 提取左图每一张图片的角点
retR, cornersR = cv2.findChessboardCorners(ChessImaR,(self.width, self.height), cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS) # 提取右图每一张图片的角点
if (True == retR) & (True == retL):
objpoints.append(objp)
cv2.cornerSubPix(ChessImaL, cornersL, (11, 11), (-1, -1), self.criteria) # 亚像素精确化,对粗提取的角点进行精确化
cv2.cornerSubPix(ChessImaR, cornersR, (11, 11), (-1, -1), self.criteria) # 亚像素精确化,对粗提取的角点进行精确化
imgpointsL.append(cornersL)
imgpointsR.append(cornersR)
# ret_l = cv2.drawChessboardCorners(ChessImaL, (self.width, self.height), cornersL, retL)
# cv2.imshow(file_L[i], ChessImaL)
# cv2.waitKey()
# ret_r = cv2.drawChessboardCorners(ChessImaR, (self.width, self.height), cornersR, retR)
# cv2.imshow(file_R[i], ChessImaR)
# cv2.waitKey(500)
# 相机的单双目标定、及校正
# 左侧相机单独标定
retL, K1, D1, rvecsL, tvecsL = cv2.calibrateCamera(objpoints,imgpointsL,ChessImaL.shape[::-1], None, None)
# 右侧相机单独标定
retR, K2, D2, rvecsR, tvecsR = cv2.calibrateCamera(objpoints,imgpointsR,ChessImaR.shape[::-1], None, None)
# --------- 双目相机的标定 ----------#
flags = 0
flags |= cv2.CALIB_FIX_INTRINSIC # K和D个矩阵是固定的。这是默认标志。如果你校准好你的相机,只求解𝑅,𝑇,𝐸,𝐹。
#flags |= cv2.CALIB_FIX_PRINCIPAL_POINT # 修复K矩阵中的参考点。
# flags |= cv2.CALIB_USE_INTRINSIC_GUESS # K和D个矩阵将被优化。对于这个计算,你应该给出经过良好校准的矩阵,以便(可能)得到更好的结果。
#flags |= cv2.CALIB_FIX_FOCAL_LENGTH # 在K矩阵中固定焦距。
# flags |= cv2.CALIB_FIX_ASPECT_RATIO # 固定长宽比。
#flags |= cv2.CALIB_ZERO_TANGENT_DIST # 去掉畸变。
# 内参、畸变系数、平移向量、旋转矩阵
retS, K1, D1, K2, D2, R, T, E, F = cv2.stereoCalibrate(objpoints,imgpointsL,imgpointsR,K1,D1,K2,D2,
ChessImaR.shape[::-1], self.criteria_stereo,flags)
# 左内参矩阵、左畸变向量、右内参矩阵、右畸变向量、旋转矩阵、平移矩阵
return K1, D1, K2, D2, R, T
# ==================================================================== #
# =========================== 双目校正 =============================== #
# 获取畸变校正、立体校正、重投影矩阵
def getRectifyTransform(self, width,height,K1 ,D1 ,K2 ,D2 , R, T):
#得出进行立体矫正所需要的映射矩阵
# 左校正变换矩阵、右校正变换矩阵、左投影矩阵、右投影矩阵、深度差异映射矩阵
R_l,R_r,P_l,P_r,Q, roi_left, roi_right = cv2.stereoRectify(K1, D1, K2, D2,
(width, height),R, T,
flags=cv2.CALIB_ZERO_DISPARITY, alpha=0)
# # 标志CALIB_ZERO_DISPARITY,它用于匹配图像之间的y轴
# 计算畸变矫正和立体校正的映射变换。
map_lx, map_ly = cv2.initUndistortRectifyMap(K1, D1, R_l, P_l, (width,height),cv2.CV_32FC1)
map_rx, map_ry = cv2.initUndistortRectifyMap(K2, D2, R_r, P_r, (width,height),cv2.CV_32FC1)
return map_lx, map_ly,map_rx, map_ry, Q
# 得到畸变校正和立体校正后的图像
def get_rectify_img(self, imgL, imgR,map_lx, map_ly,map_rx, map_ry):
rec_img_L = cv2.remap(imgL,map_lx, map_ly, cv2.INTER_LINEAR, cv2.BORDER_CONSTANT) # 使用remap函数完成映射
rec_img_R = cv2.remap(imgR,map_rx, map_ry, cv2.INTER_LINEAR, cv2.BORDER_CONSTANT)
return rec_img_L, rec_img_R
# 立体校正检验——极线对齐
def draw_line(self, rec_img_L,rec_img_R):
#建立输出图像
width = max(rec_img_L.shape[1],rec_img_R.shape[1])
height = max(rec_img_L.shape[0],rec_img_R.shape[0])
output = np.zeros((height,width*2,3),dtype=np.uint8)
output[0:rec_img_L.shape[0],0:rec_img_L.shape[1]] = rec_img_L
output[0:rec_img_R.shape[0],rec_img_L.shape[1]:] = rec_img_R
# 绘制等间距平行线
line_interval = 50 # 直线间隔:50
for k in range(height // line_interval):
cv2.line(output, (0, line_interval * (k + 1)), (2 * width, line_interval * (k + 1)), (0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
return output # 可显示的图像
# ===================================================================== #
def get_parser():
parser = argparse.ArgumentParser(description='Camera calibration')
parser.add_argument('--width', type=int, default=12, help='chessboard width size')
parser.add_argument('--height', type=int, default=8, help='chessboard height size')
parser.add_argument('--lattice', type=float, default=12.5, help='lattice length')
parser.add_argument('--image_dir', type=str, default="data/", help='images path')
parser.add_argument('--save_dir', type=str, default="config/", help='path to save file')
parser.add_argument('--file_name', type=str, default="camera_params", help='camera params save file')
return parser
def get_file(path): #获取文件路径
img_path = []
for root, dirs, files in os.walk(path):
for file in files:
img_path.append(os.path.join(root,file))
return img_path
if __name__ == "__main__":
args = get_parser().parse_args()
params_dict = {}
file_L = get_file(args.image_dir + 'left')
file_R = get_file(args.image_dir + 'right')
imgL = cv2.imread(file_L[2])
imgR = cv2.imread(file_R[2])
height, width = imgL.shape[0:2]
calibration = Stereo_Camera_Calibration(args.width, args.height, args.lattice)
left_K,left_D, right_K, right_D, R, T = calibration.stereo_calibration(file_L, file_R)
map_lx, map_ly,map_rx, map_ry, Q = calibration.getRectifyTransform(width,height,left_K,left_D,
right_K, right_D, R, T)
# 查看校正效果
img_ = calibration.draw_line(imgL,imgR)
cv2.imshow("img",img_)
rec_img_L, rec_img_R = calibration.get_rectify_img(imgL,imgR,map_lx, map_ly,map_rx, map_ry)
img_show = calibration.draw_line(rec_img_L,rec_img_R)
cv2.imshow("output",img_show)
cv2.waitKey(0)
params_dict['size'] = [width, height]
params_dict['K1'] = left_K.tolist()
params_dict['D1'] = left_D.tolist()
params_dict['K2'] = right_K.tolist()
params_dict['D2'] = right_D.tolist()
params_dict['map_lx'] = map_lx.tolist()
params_dict['map_ly'] = map_ly.tolist()
params_dict['map_rx'] = map_rx.tolist()
params_dict['map_ry'] = map_ry.tolist()
params_dict['R'] = R.tolist()
params_dict['T'] = T.tolist()
params_dict['Q'] = Q.tolist()
# =========== 保存相机参数 =========== #
# 保存为.json文件
file_path = args.save_dir + args.file_name + ".json"
with open(file_path,"w") as f:
json.dump(params_dict, f, indent=1)
print("ALL Make Done!")
参考
https://ww2.mathworks.cn/help/vision/ug/stereo-camera-calibrator-app.html
https://blog.csdn.net/baidu_39231810/article/details/128628651