def calibrateTwo(leftCameraParam, rightCameraParam, lmap, rmap, leftImg, rightImg): img1 = cv2.imread(leftImg) img2 = cv2.imread(rightImg) F, H_left, H_right = rectify_pair(img1, img2, True) R1 = np.linalg.inv( leftCameraParam['mtx']) * H_left * leftCameraParam['mtx'] R2 = np.linalg.inv( rightCameraParam['mtx']) * H_right * rightCameraParam['mtx'] rms, C1, dist1, C2, dist2, R, T, E, F = cv2.stereoCalibrate( leftCameraParam['obj_points'], leftCameraParam['img_points'], rightCameraParam['img_points'], leftCameraParam['mtx'], leftCameraParam['dist'], rightCameraParam['mtx'], rightCameraParam['dist'], leftCameraParam['size'], flags=cv2.CALIB_USE_INTRINSIC_GUESS) Q = cv2.stereoRectify(C1, dist1, C2, dist2, leftCameraParam['size'], R, T, alpha=1) #Computes rectification transforms for each head of a calibrated stereo camera. left_map1, left_map2 = cv2.initUndistortRectifyMap( C1, dist1, R1, leftCameraParam['newmtx'], leftCameraParam['size'], cv2.CV_16SC2) #计算矫正参数 right_map1, right_map2 = cv2.initUndistortRectifyMap( C2, dist2, R2, rightCameraParam['newmtx'], leftCameraParam['size'], cv2.CV_16SC2) print('calibrate two camera success') lmap['lm1'] = left_map1 lmap['lm2'] = left_map2 rmap['rm1'] = right_map1 rmap['rm2'] = right_map2 lmap['Q'] = Q lmap['R1'] = R1 rmap['R2'] = R2 cv2.waitKey(-1)
def get_rect_maps(cameras, rect, proj): """Computes rectification maps.""" # empty_mat = np.empty(tuple(frame_size)) # map1 = [empty_mat for __ in range(len(cam_mats))] # map2 = [empty_mat for __ in range(len(cam_mats))] map1 = [np.empty(cameras[0].size) for __ in range(len(cameras))] map2 = [np.empty(cameras[0].size) for __ in range(len(cameras))] for i, (camera, r, p) in enumerate(zip(cameras, rect, proj)): map1[i], map2[i] = cv2.initUndistortRectifyMap(camera.cam_mat, camera.dist_coeff, r, p, camera.size, cv2.CV_32FC1) return map1, map2
print("mtx:\n",mtx) # 内参数矩阵 print("dist畸变值:\n",dist ) # 畸变系数 distortion cofficients = (k_1,k_2,p_1,p_2,k_3) print("rvecs旋转(向量)外参:\n",rvecs) # 旋转向量 # 外参数 print("tvecs平移(向量)外参:\n",tvecs ) # 平移向量 # 外参数 newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (u, v), 0, (u, v)) print('newcameramtx外参',newcameramtx) #打开摄像机 camera=cv2.VideoCapture(0) while True: (grabbed,frame)=camera.read() h1, w1 = frame.shape[:2] newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (u, v), 0, (u, v)) # 纠正畸变 dst1 = cv2.undistort(frame, mtx, dist, None, newcameramtx) #dst2 = cv2.undistort(frame, mtx, dist, None, newcameramtx) mapx,mapy=cv2.initUndistortRectifyMap(mtx,dist,None,newcameramtx,(w1,h1),5) dst2=cv2.remap(frame,mapx,mapy,cv2.INTER_LINEAR) # 裁剪图像,输出纠正畸变以后的图片 x, y, w1, h1 = roi dst1 = dst1[y:y + h1, x:x + w1] #cv2.imshow('frame',dst2) #cv2.imshow('dst1',dst1) cv2.imshow('dst2', dst2) if cv2.waitKey(1) & 0xFF == ord('q'): # 按q保存一张图片 cv2.imwrite("../u4/frame.jpg", dst1) break camera.release() cv2.destroyAllWindows()
right_distortion = np.array([[-0.4983, 0.5503, -0.0020, 0.0026, -0.5252]]) R = np.array([[1.0000, -0.0024, 0.0073], [0.0024, 1.0000, -0.0061], [-0.0073, 0.0062, 1.0000]]) # 旋转关系向量 #R = cv2.Rodrigues(om)[0] # 使用Rodrigues变换将om变换为R T = np.array([-114.6016, -1.9783, 4.3376]) # 平移关系向量 size = (640, 480) # 图像尺寸 # 进行立体更正 R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify( left_camera_matrix, left_distortion, right_camera_matrix, right_distortion, size, R, T) # 计算更正map left_map1, left_map2 = cv2.initUndistortRectifyMap(left_camera_matrix, left_distortion, R1, P1, size, cv2.CV_16SC2) right_map1, right_map2 = cv2.initUndistortRectifyMap(right_camera_matrix, right_distortion, R2, P2, size, cv2.CV_16SC2) cv2.namedWindow("left") cv2.namedWindow("right") cv2.namedWindow("leftt") cv2.namedWindow("rightt") cv2.namedWindow("depth") cv2.moveWindow("left", 0, 0) cv2.moveWindow("right", 640, 0) cv2.moveWindow("leftt", 0, 480) cv2.moveWindow("rightt", 640, 480) #cv2.createTrackbar("num", "depth", 0, 10, lambda x: None)
# If found, add object points, image points (after refining them) if ret == True: objpoints.append(objp) corners2 = cv.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria) imgpoints.append(corners) # Draw and display the corners cv.drawChessboardCorners(img, (11, 8), corners2, ret) ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera( objpoints, imgpoints, gray.shape[::-1], None, None) h, w = img.shape[:2] newcameramtx, roi = cv.getOptimalNewCameraMatrix( mtx, dist, (w, h), 1, (w, h)) # print(roi) mapx, mapy = cv.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w, h), 5) dst = cv.remap(img, mapx, mapy, cv.INTER_LINEAR) x, y, w, h = roi dst = dst[y:y + h, x:x + w] npz = np.load('./output.npz') print(npz.files) cv.imwrite('calibresult.png', dst) plt_img = img[:, :, ::-1] plt.figure(fname) plt.imshow(plt_img) plt.show()