KITTI自动驾驶数据集使用与点云可视化实例
KITTI数据集
KITTI数据集是自动驾驶领域最著名的公开数据集之一,由德国卡尔斯鲁厄理工学院和丰田美国技术研究院联合创建。它包含:
- 超过6小时的交通场景数据
- 高分辨率RGB图像(左/右相机)
- 3D激光雷达点云数据(64线Velodyne激光扫描仪)
- 3D物体标注(汽车、行人、自行车等)
- 立体视觉、光流、视觉里程计等多种任务数据
数据集下载与结构
KITTI数据集可从官网下载:http://www.cvlibs.net/datasets/kitti/
主要目录结构:
kitti/
├── raw_data/ # 原始数据
│ ├── 2011_09_26/ # 按日期组织的采集数据
│ └── ...
├── object/ # 3D物体检测任务数据
│ ├── training/
│ │ ├── image_2/ # 左相机图像
│ │ ├── velodyne/ # 点云数据(.bin)
│ │ └── label_2/ # 标注文件
│ └── testing/
└── tracking/ # 多目标跟踪数据
点云数据可视化实例
1. 使用Python进行点云可视化
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def read_velodyne_bin(bin_path):
"""读取KITTI的.bin点云文件"""
point_cloud = np.fromfile(bin_path, dtype=np.float32)
return point_cloud.reshape((-1, 4)) # x,y,z,反射率
def visualize_point_cloud(points):
"""3D可视化点云"""
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# 只取前3列(x,y,z),忽略反射率
ax.scatter(points[:, 0], points[:, 1], points[:, 2],
s=1, c=points[:, 2], cmap='viridis')
ax.set_xlabel('X (m)')
ax.set_ylabel('Y (m)')
ax.set_zlabel('Z (m)')
ax.set_title('KITTI点云可视化')
plt.show()
# 示例使用
bin_file = "kitti/object/training/velodyne/000000.bin"
points = read_velodyne_bin(bin_file)
visualize_point_cloud(points)
2. 使用Open3D进行更高级的可视化
import open3d as o3d
def visualize_with_open3d(points):
"""使用Open3D可视化点云"""
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points[:, :3]) # 只取x,y,z
# 设置颜色基于Z轴高度
colors = plt.get_cmap("viridis")((points[:, 2] - points[:, 2].min()) /
(points[:, 2].max() - points[:, 2].min()))
pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])
# 可视化
o3d.visualization.draw_geometries([pcd],
window_name="KITTI点云",
width=800,
height=600)
# 使用示例
visualize_with_open3d(points)
3. 结合图像和点云的可视化
import cv2
from PIL import Image
def project_points_to_image(points, calib_path, image_path):
"""将点云投影到图像平面"""
# 读取标定数据
with open(calib_path, 'r') as f:
lines = f.readlines()
P2 = np.array(lines[2].strip().split(' ')[1:], dtype=np.float32).reshape(3, 4)
R0_rect = np.array(lines[4].strip().split(' ')[1:], dtype=np.float32).reshape(3, 3)
Tr_velo_to_cam = np.array(lines[5].strip().split(' ')[1:], dtype=np.float32).reshape(3, 4)
# 转换点云到相机坐标系
points_3d = points[:, :3]
points_3d_hom = np.hstack([points_3d, np.ones((points_3d.shape[0], 1))])
points_cam = R0_rect @ (Tr_velo_to_cam @ points_3d_hom.T)
points_cam = points_cam.T
# 投影到图像平面
points_img = P2 @ np.hstack([points_cam[:, :3], np.ones((points_cam.shape[0], 1))]).T
points_img = points_img.T
points_img[:, :2] /= points_img[:, 2][:, np.newaxis]
# 过滤在图像外的点
img = Image.open(image_path)
width, height = img.size
mask = (points_img[:, 0] >= 0) & (points_img[:, 0] < width) & \
(points_img[:, 1] >= 0) & (points_img[:, 1] < height) & \
(points_img[:, 2] > 0)
points_img = points_img[mask]
points_3d = points_3d[mask]
# 可视化
img = np.array(img)
fig, ax = plt.subplots(1, 1, figsize=(12, 5))
ax.imshow(img)
# 根据深度着色
depths = points_img[:, 2]
colors = plt.cm.viridis((depths - depths.min()) / (depths.max() - depths.min()))
ax.scatter(points_img[:, 0], points_img[:, 1],
c=colors, s=5, alpha=0.7)
plt.title("点云投影到图像")
plt.show()
# 使用示例
calib_file = "kitti/object/training/calib/000000.txt"
image_file = "kitti/object/training/image_2/000000.png"
project_points_to_image(points, calib_file, image_file)
实际应用建议
-
数据预处理:
- 过滤地面点(使用RANSAC平面拟合)
- 体素网格下采样减少点数
- 移除过远或无效的点
-
性能优化:
- 对于大规模点云,考虑使用八叉树或KD树加速处理
- 使用GPU加速的点云处理库如PyTorch3D
-
高级可视化:
- 添加交互功能(旋转、缩放、选择)
- 显示3D边界框标注
- 多帧点云累积显示
KITTI数据集为自动驾驶研究提供了丰富的真实场景数据,通过点云可视化可以直观理解传感器数据,为后续的物体检测、语义分割等任务奠定基础。