Python 点云从入门到进阶

80次阅读
没有评论

本文覆盖点云 (point cloud) 的基础概念、常用格式、环境搭建、可视化、预处理、配准、特征提取、机器学习与深度学习实战、性能优化以及常用数据集与工具链。目标读者为希望用 Python 进行点云处理与研究的工程师与科研人员。


目录

  1. 简介
  2. 环境与安装
  3. 点云基础与常见格式
  4. 使用 Open3D 快速入门(读取/显示/保存)
  5. 可视化技巧
  6. 预处理与滤波
  7. 点云配准(Registration)
  8. 特征提取与聚类
  9. 机器学习与深度学习应用
  10. 大规模点云与性能优化
  11. 常用数据集与资源
  12. 参考资料

1. 简介

点云由大量三维坐标点组成(通常带有颜色、强度或其他属性),广泛用于计算机视觉、机器人、测绘、自动驾驶、AR/VR 等领域。本教程主要基于 Python 生态,推荐使用 Open3D 作为主力库,同时辅以 pyntcloudlaspyPDALpclpy(若需要 PCL 功能)及深度学习框架 PyTorch/TensorFlow。


2. 环境与安装

建议使用 conda 创建隔离环境:

conda create -n pc_env python=3.10 -y
conda activate pc_env

安装常用库(Open3D、numpy、scipy、scikit-learn、matplotlib):

pip install numpy scipy scikit-learn matplotlib
pip install open3d
pip install pyntcloud laspy

对于深度学习:

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118  # 示例(CUDA 11.8),按设备调整
pip install pytorch-lightning

额外工具:

  • PDAL(建议使用 conda 或系统包管理器安装)
  • pclpy 可通过 conda 安装(复杂,按需):conda install -c conda-forge pclpy
  • CloudCompare(可视化与标注 GUI)

常见安装问题:

  • open3d 若在 macOS / Linux 上报错,可尝试先升级 pip:pip install -U pip setuptools,再安装。
  • pclpy 依赖 PCL,安装较繁琐,若不需要 PCL 功能可跳过。

3. 点云基础与常见格式

常见格式:

  • PLY:通用,支持顶点属性(颜色、法线等)。
  • PCD:PCL 原生格式。
  • LAS/LAZ:激光雷达专用格式,支持地理信息与强度。
  • OBJ:网格格式(包含顶点与面)。

点云基本概念:

  • 点的坐标 (x,y,z)
  • 属性:颜色 (r,g,b)、强度、时间戳、法向量等
  • 稠密/稀疏:取决于采样密度

4. 使用 Open3D 快速入门(读取 / 显示 / 保存)

示例:读取 PLY 并显示

import open3d as o3d

pcd = o3d.io.read_point_cloud("example.ply")
print(pcd)

# 显示
o3d.visualization.draw_geometries([pcd])

# 转 numpy
pts = np.asarray(pcd.points)
cols = np.asarray(pcd.colors) if pcd.has_colors() else None

# 保存
o3d.io.write_point_cloud("out.ply", pcd)

快速常用 API:

  • read_point_cloud(path) / write_point_cloud(path, pcd)
  • voxel_down_sample(voxel_size):体素下采样
  • estimate_normals():估计法线
  • paint_uniform_color([r,g,b]):着色

5. 可视化技巧

  • 改变点大小与背景
vis = o3d.visualization.Visualizer()
vis.create_window(window_name='PC', width=800, height=600)
vis.add_geometry(pcd)
opt = vis.get_render_option()
opt.background_color = np.asarray([0,0,0])
opt.point_size = 2.0
vis.run()
vis.destroy_window()
  • 给点上颜色(根据高度或类标签):
import numpy as np
z = np.asarray(pcd.points)[:,2]
norm = (z - z.min()) / (z.max() - z.min())
colors = plt.get_cmap('viridis')(norm)[:,:3]
pcd.colors = o3d.utility.Vector3dVector(colors)
  • 添加坐标轴:o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0)

6. 预处理与滤波

常用步骤:下采样、去噪、法线估计、裁剪。

  • 体素下采样:
down = pcd.voxel_down_sample(voxel_size=0.05)
  • 统计离群点移除(Statistical Outlier Removal):
cl, ind = down.remove_statistical_outlier(nb_neighbors=20, std_ratio=2.0)
pc_filtered = down.select_by_index(ind)
  • 半径离群点移除(Radius Outlier Removal):
cl, ind = down.remove_radius_outlier(nb_points=16, radius=0.05)
pc_filtered = down.select_by_index(ind)
  • 法线估计:
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))
pcd.orient_normals_consistent_tangent_plane(100)

7. 点云配准(Registration)

7.1 RANSAC 平面分割(例如地面或平面提取)

plane_model, inliers = pcd.segment_plane(distance_threshold=0.01, ransac_n=3, num_iterations=1000)
[a, b, c, d] = plane_model
pc_plane = pcd.select_by_index(inliers)
pc_rest = pcd.select_by_index(inliers, invert=True)

7.2 ICP(Iterative Closest Point)

# 假设 source, target 已读取并下采样且估计过法线
threshold = 0.02
trans_init = np.eye(4)
reg_p2p = o3d.pipelines.registration.registration_icp(
    source, target, threshold, trans_init,
    o3d.pipelines.registration.TransformationEstimationPointToPoint())
print(reg_p2p.transformation)

7.3 全局配准(基于特征,如 RANSAC + FPFH)

# 计算 FPFH 特征
source_fpfh = o3d.pipelines.registration.compute_fpfh_feature(
    source, o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=100))
# 使用 RANSAC 全局配准(Open3D 提供接口)

参考 Open3D 文档中 registration 示例获取完整流程。


8. 特征提取与聚类

  • FPFH(Fast Point Feature Histogram):适用于粗配准与特征描述
  • 法线方向直方图

示例:计算 FPFH

fpfh = o3d.pipelines.registration.compute_fpfh_feature(
    pcd, o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=100))

聚类示例(DBSCAN):

labels = np.array(pcd.cluster_dbscan(eps=0.02, min_points=10))
max_label = labels.max()
print(f"point cloud has {max_label+1} clusters")

9. 机器学习与深度学习应用

9.1 传统机器学习流程

  1. 从点云提取全局/局部特征(统计量、直方图、FPFH 等)。
  2. 使用 scikit-learn 的分类器(SVM、RandomForest、XGBoost 等)进行训练与评估。

示例(伪代码):

# features: N x D
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()
clf.fit(train_features, train_labels)
pred = clf.predict(test_features)

9.2 深度学习:PointNet / PointNet++ / DGCNN

常用库/实现:

  • torch_points3d
  • Open3D-ML
  • 论文实现:PointNet、PointNet++、DGCNN、PointConv、KPConv 等

训练要点:

  • 数据预处理(归一化、采样到固定点数如 1024 或 2048)
  • 数据增强(随机旋转、缩放、抖动)
  • 批量化处理与内存管理

示例:使用 PyTorch 加载点云为训练样本(伪代码)

# 假设每个样本已被采样为 N x 3 (无颜色)
class PointDataset(torch.utils.data.Dataset):
    def __init__(self, files):
        self.files = files
    def __len__(self):
        return len(self.files)
    def __getitem__(self, idx):
        pcd = o3d.io.read_point_cloud(self.files[idx])
        pts = np.asarray(pcd.points).astype(np.float32)
        # 随机采样/填充到固定点数
        # 返回 (N,3) 和标签

9.3 实用建议

  • 训练深度网络时尽量使用 GPU;批量大小依显存而定。
  • 使用已有实现(如 torch_points3d)能节省大量时间。

10. 大规模点云与性能优化

  • 使用 PDAL 做格式转换、过滤与分块(tile)处理。
  • 对超大点云,采用分块流式处理:按 tile 或按高度切分并逐块处理。
  • 空间索引:KD-Tree、Octree,用于快速邻域查询。
  • GPU 加速:使用 CUDA 实现最近邻、体素网格等操作的库(如 CUDA 实现的 ICP)。

11. 常用数据集与资源

  • ModelNet40(点云分类)
  • ShapeNet(3D 模型库)
  • Stanford 3D Scanning Repository
  • SemanticKITTI(语义分割)
  • KITTI(LiDAR 数据)

工具与平台:


12. 参考资料

正文完
 0
评论(没有评论)

YanQS's Blog