import open3d as o3d import numpy as np import cv2 import matplotlib.pyplot as plt # 创建一些点云数据 pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(np.random.rand(100, 3)) # 设置相机参数 fx = 525.0 # 焦距 fy = 525.0 cx = 319.5 # 主点 cy = 239.5 depth_scale = 1000.0 # 深度缩放因子 intrinsic = np.identity(3) intrinsic[0, 0] = fx intrinsic[0, 2] = cx intrinsic[1, 1] = fy intrinsic[1, 2] = cy # 将点云投影到图像平面 pcd.transform(intrinsic) # 转换为OpenCV的图像格式 points = np.asarray(pcd.points) depth = points[:, 2] depth[depth < 0] = 0 # 设置负的深度值为0 depth /= depth_scale # 归一化深度 h, w = int(cy + 1), int(cx + 1) image = np.zeros((h, w, 3), dtype=np.uint8) for i in range(points.shape[0]): x, y, z = points[i] if 0 <= int(y) < h and 0 <= int(x) < w: image[int(y), int(x)] = (int(255 * (1 - z / depth[i])), int(255 * (1 - z / depth[i])), int(255 * (1 - z / depth[i]))) # 显示结果 plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.show()