import cv2
import numpy as np
假设已经获取了相机矩阵和畸变系数
camera_matrix = np.array(...)
dist_coeffs = np.array(...)
读取校正前的图像
img1 = cv2.imread('left01.jpg')
校正图像
map1, map2 = cv2.fisheye.initUndistortRectifyMap(camera_matrix, dist_coeffs, np.eye(3), camera_matrix, img1.shape[0:2][::-1], cv2.CV_16SC2)
undistorted_img1 = cv2.remap(img1, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
计算投影矩阵
proj_matrix = cv2.fisheye.getOptimizedProjectionMatrix(camera_matrix, dist_coeffs, undistorted_img1.shape[1], undistorted_img1.shape[0], alpha=0.0)
将校正后的图像投影到三维空间
img1_3d = cv2.fisheye.projectImageTo3D(undistorted_img1, proj_matrix)
显示三维点云
points = np.reshape(img1_3d, (3, -1)).T
points = np.hstack((points, np.ones((1, points.shape[1]))))
print(points)