OpenCV下的单目标定,双目标定与立体校正(calibrateCamera, stereoCalibrate and stereoRectify)
文章目录
1. 杂话
大伙儿应该都用过OpenCV和相机吧,所以今天咱们就来说说怎么使用两个相机拍摄的照片和OpenCV来进行标定和立体校正。相机标定的理论解释其实有很多啦,我就随便找两个写得不错的帖子给大家参考一下哈:
总而言之,我就不那个班门弄斧关公面前耍大刀了,我就简单说说代码层面的实现。
其中,标定部分的代码部分参考了:Temuge Batpurev’s Blog
绘制极线部分的代码参考了:逆光525的帖子-绘制极线
对了,我使用的数据和完整的代码在这里:Repo : Calibrate-and-Rectify
2. 单目标定
2.1 先看代码
chessboard_size = (9, 6)
frame_size = (640, 480)
# 设置棋盘格点的世界坐标
objp = np.zeros((chessboard_size[0]*chessboard_size[1], 3), np.float32)
objp[:,:2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2)
square_size = 1
objp *= square_size
# 用于存储世界坐标和图像坐标
objpoints = [] # 3d points in real world space
imgpoints_main = [] # 2d points in image plane
images = glob.glob('demo/left*.jpg')
images = sorted(images)
print(f"Found {
len(images)} images for calibration")
for idx, image_file in enumerate(images):
img = cv2.imread(image_file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 寻找棋盘格角点
ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None)
if ret == True:
objpoints.append(objp)
# 亚像素级角点精确化
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11