ZED相机标定

1. 对左右相机分别进行校正

import cv2
import pyzed.sl as sl
import glob
import numpy as np

# 类二:相机标定执行函数(单目校正)
class Cam_calibrate():  # 执行校正
    def __init__(self):
        # 终止条件
        self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
        # 准备对象点,棋盘方块交界点排列:6行8列 如 (0,0,0), (1,0,0), (2,0,0) ....,(6,8,0)
        self.row,self.col = 8,11
        self.objpoints = np.zeros((self.row * self.col, 3), np.float32)
        self.objpoints[:, :2] = np.mgrid[0:self.row, 0:self.col].T.reshape(-1, 2)

    def exe(self,dir):
        objectpoints = [] # 真实世界中的3d点
        imgpoints = []
        # 标定所用图像
        images = glob.glob('%s/*'%dir)
        for fname in images:
            img = cv2.imread(fname)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # 寻找到棋盘角点
            ret, corners = cv2.findChessboardCorners(img, (self.row, self.col), None)
            # 如果找到,添加对象点,图像点(细化之后)
            if ret == True:
                # 添加每幅图的对应3D-2D坐标
                objectpoints.append(self.objpoints)
                corners = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1),self.criteria)
                imgpoints.append(corners)
                # 绘制并显示拐角
                cv2.drawChessboardCorners(img, (self.row, self.col), corners, ret)
                cv2.namedWindow('View')
                cv2.imshow("View", img)
                cv2.waitKey(0)
        cv2.destroyAllWindows()
        # 输出:相机内参、畸变系数、旋转矩阵(每个点都会得到一个)、平移矩阵(同左)
        ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objectpoints, imgpoints, gray.shape[::-1], None, None)
        print(ret,'\n相机内参:%s\n畸变系数:%s\n旋转矩阵:%s\n%s\n平移向量::%s\n%s '%(mtx, dist, rvecs[0].shape,rvecs, tvecs[0].shape,tvecs))

if __name__ == "__main__":
    # 单目校正
    path_R = r'./imgR/'  # 棋盘格图片路径
    cal_R = Cam_calibrate()
	path_L = r'./imgL/'  # 棋盘格图片路径
    cal_L = Cam_calibrate()
    cal.exe(path)

你可能感兴趣的:(opencv,人工智能,计算机视觉)