整个场景可视化:
import numpy as np
import open3d as o3ddef read_kitti_bin_point_cloud(bin_file):# 加载.bin文件point_cloud_np = np.fromfile(bin_file, dtype=np.float32).reshape(-1, 4)# 仅使用X, Y, Z坐标,忽略反射率point_cloud_o3d = o3d.geometry.PointCloud()point_cloud_o3d.points = o3d.utility.Vector3dVector(point_cloud_np[:, :3])# 设置所有点为白色point_cloud_o3d.colors = o3d.utility.Vector3dVector(np.ones((len(point_cloud_np), 3)))return point_cloud_o3ddef visualize_point_cloud(point_cloud_o3d):# 创建一个可视化窗口vis = o3d.visualization.Visualizer()vis.create_window(window_name="KITTI Point Cloud", width=800, height=600)# 设置窗口的背景为黑色opt = vis.get_render_option()opt.point_size = 1.0opt.background_color = np.asarray([0, 0, 0])# 将点云数据加入可视化窗口vis.add_geometry(point_cloud_o3d)# 进行可视化vis.run()vis.destroy_window()if __name__ == "__main__":bin_file = '/root/autodl-data/IA-SSD/data/kitti/training/velodyne/002147.bin' # 更改为你的KITTI .bin文件路径point_cloud_o3d = read_kitti_bin_point_cloud(bin_file)visualize_point_cloud(point_cloud_o3d)
效果:
KITTI范围:point_cloud_range = [0, -40, -3, 70.4, 40, 1]
import numpy as np
import open3d as o3ddef read_kitti_bin_point_cloud(bin_file, point_cloud_range):# 加载.bin文件point_cloud_np = np.fromfile(bin_file, dtype=np.float32).reshape(-1, 4)# 应用点云范围筛选mask = (point_cloud_np[:, 0] >= point_cloud_range[0]) & (point_cloud_np[:, 0] <= point_cloud_range[3]) & \(point_cloud_np[:, 1] >= point_cloud_range[1]) & (point_cloud_np[:, 1] <= point_cloud_range[4]) & \(point_cloud_np[:, 2] >= point_cloud_range[2]) & (point_cloud_np[:, 2] <= point_cloud_range[5])point_cloud_np = point_cloud_np[mask][:, :3]# 使用X, Y, Z坐标创建Open3D点云对象point_cloud_o3d = o3d.geometry.PointCloud()point_cloud_o3d.points = o3d.utility.Vector3dVector(point_cloud_np)# 设置所有点为白色point_cloud_o3d.colors = o3d.utility.Vector3dVector(np.ones((len(point_cloud_np), 3)))return point_cloud_o3ddef visualize_point_cloud(point_cloud_o3d):# 创建一个可视化窗口vis = o3d.visualization.Visualizer()vis.create_window(window_name="KITTI Point Cloud", width=800, height=600)# 设置窗口的背景为黑色opt = vis.get_render_option()opt.point_size = 1.0opt.background_color = np.asarray([0, 0, 0])# 将点云数据加入可视化窗口vis.add_geometry(point_cloud_o3d)# 进行可视化vis.run()vis.destroy_window()if __name__ == "__main__":bin_file = '/root/autodl-data/IA-SSD/data/kitti/training/velodyne/002147.bin' # 更改为你的KITTI .bin文件路径# 定义点云显示范围point_cloud_range = [0, -40, -3, 70.4, 40, 1]point_cloud_o3d = read_kitti_bin_point_cloud(bin_file, point_cloud_range)visualize_point_cloud(point_cloud_o3d)
效果: