【Lidar】基于Python的Open3D库、Laspy库保存点云文件/点云格式转换

发布时间 2023-12-04 16:53:54作者: RS迷途小书童

         因为最近在做点云相关的项目,过程中用到了Python中的Open3D库和Laspy库,所以今天给大家分享一下如何使用Open3D和Laspy这两个库对点云数据进行保存和格式的转换。

1 Open3D库介绍

        Laspy库我到时候会单独介绍,所以这里就不多说了!!!

        Open3D是一个开源的3D数据处理库,发布于2015年,目前已经更新到0.17.0版本。它基于MIT协议开源许可,使用C++11实现,并经过高度优化,还通过Python Pybinding提供了前端Python API。 Open3D为开发者提供了一组精心选择的数据结构和算法,内部实现高度优化并设置为并行化。它处理3D数据的各种应用,包括点云、网格、体积计算、可视化、深度学习、测量和场景图等。Open3D的目标是成为一个高效,可扩展和易用的3D数据处理库。

2 点云数据保存代码

2.1 laspy库保存

        laspy中可以通过列表索引选择对应的点云数据。需要注意laspy只支持.las和.laz文件。

def save_point(path, point_type):
    # ---------------------------laspy库保存----------------------------
    las = laspy.read(r"Z:\Personal\彭俊喜\Lidar_try/2.las")  # read a las file
    points = las.points
    out_file = laspy.LasData(las.header)
    ground = [2, 5, 6]  # 索引
    out_file.points = points[np.array(ground)]  # extract ground points, and save it to a las file.
    out_file.write(r"Z:\Personal\彭俊喜\Lidar_try/out1.las")

2.2 Open3D库保存/格式转换

        这里包含了一个自己通过数组创建点云数据并保存的方法。标题中所说的点云格式的转换也是在这里,我们只要使用open3d库读取点云后,导出时加入相应的后缀即可进行格式转换。

def save_point(path, point_type):

    # ---------------------------open3d库保存---------------------------
    pcd = o3d.io.read_point_cloud(path, format=point_type, remove_nan_points=True, remove_infinite_points=True, print_progress=True)
    # 路径、输入格式、删除包含NAN的所有点、删除包含无限值的所有点、可视化进度条
    print(pcd)  # 输出点云点的个数
    print(np.asarray(pcd.points))  # 输出点的三维坐标
    o3d.io.write_point_cloud(r'1.xyz', pcd, write_ascii=False, compressed=False, print_progress=True)
    # 路径、文件、以ascii格式输出否则使用二进制输出、以压缩格式写入、可视化进度条

    # ---------------------------open3d库保存---------------------------
    numpy = [[1, 2, 3], [1, 2, 3], [3, 4, 5]]
    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(numpy)  # 数组转点云
    o3d.io.write_point_cloud(r'Z:\Personal\彭俊喜\Lidar_try/1.xyz', pcd, write_ascii=False, compressed=False,
                             print_progress=True)

3 总结

        目前我就使用了Open3D和Laspy两个库去处理点云数据,个人觉得这两个库还是挺不错的,后面也会持续更新更多的Python处理点云数据的教程,如点云聚类、单木分割等。如果感兴趣可以关注我,目前没有设为收费专栏的想法,大家可以放心学习。