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)
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 = []
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:
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)