点云:Python版本的点云数据处理库

Python之王

共 10908字,需浏览 22分钟

 · 2022-03-04

最近在搞点云DL,顺便看了看python版本的点云数据处理库,记录一下。

python我用得少,不喜勿喷,欢迎探讨,为文明和谐的社会主义事业增砖添瓦。

测试数据是这样的。

一、Open3D

A Modern Library for 3D Data Processing,Intel出品,MIT协议。

Open3D是一个支持3D数据处理软件快速开发的开源库。Open3D使用C++和Python公开了一组精心选择的数据结构和算法。后端经过高度优化,并设置为并行化。Open3D的依赖项较少,可在不同的平台上编译与布置。

Open3D侧重于三维数据的可视化与整体处理算法。想学习的同学可百度“Open3D学习计划”。

官网:Open3D – A Modern Library for 3D Data Processing

GitHub:https://github.com/intel-isl/Open3D

安装:pip install open3d 或者 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple open3d

分享给有需要的人,代码质量勿喷。

  1. import open3d as o3d

  2. import numpy as np

  3. from matplotlib import pyplot as plt


  4. # read PC

  5. pcd = o3d.io.read_point_cloud("F:/test.pcd")


  6. # # write PC

  7. # o3d.io.write_point_cloud("F:/newFile.pcd",pcd)


  8. # DBSCAN

  9. with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:

  10. labels = np.array(pcd.cluster_dbscan(eps=0.1, min_points=10, print_progress=True))

  11. max_label = labels.max()

  12. print(f"point cloud has {max_label + 1} clusters")

  13. colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1))

  14. colors[labels < 0] = 0

  15. pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])


  16. # 可视化

  17. o3d.visualization.draw_geometries([pcd],width=910,height=540)

 二、PyVista

3D plotting and mesh analysis through a streamlined interface for the Visualization Toolkit (VTK),MIT协议。

PyVista具有可视化工具包(VTK)的高级API,空间数据集的网格数据结构和过滤方法,使3D绘图变得简单,可用于大型/复杂数据几何.

PyVista(以前称为vtki)是可视化工具包(VTK)的帮助程序模块,它通过NumPy和直接数组访问采用了与VTK接口不同的方法。该软件包提供了Pythonic的,文档齐全的界面,该界面公开了VTK强大的可视化后端,以促进对空间参考数据集的快速原型制作,分析和可视化集成。该模块可用于演示文稿和研究论文的科学绘图,以及其他与网格相关的Python模块的支持模块。

PyVista侧重于可视化。

官网:The PyVista Project

介绍:PyVista — PyVista 0.32.0 documentation

GitHub:https://github.com/pyvista/pyvista

安装:pip install pyvista 或者 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyvista

分享给有需要的人,代码质量勿喷。

  1. import pyvista as pv


  2. mesh = pv.read('F:/test.vtk')

  3. mesh.plot(screenshot='F:/test.vtk.png')


/* ************************************************** 挺好的 *************************************************************** */

三、PCL

PCL(Point Cloud Library)是主要用于点云(二三维图像也可)的独立、强大的开源项目,BSD协议,可免费用于商业和研究用途。

PCL是点云数据处理的王者库,近乎全能,可视化、读写、算法(!!!)。

官网:Point Cloud Library | The Point Cloud Library (PCL) is a standalone, large scale, open project for 2D/3D image and point cloud processing.

GitHub:https://github.com/PointCloudLibrary

但是,python-pcl安装较为麻烦!!!建议谷歌或百度。

四、pclpy

pclpy是python-pcl的姊妹库吧,安装很方便,算法接口啥的也挺全的,而且,支持las。同时存在某些限制,比如正在开发中,API和功能测试也许会不稳定,只支持Windows和python 3.6 x64。

介绍:pclpy · PyPI

GitHub:https://github.com/davidcaron/pclpy

安装:pip install pclpy 或者 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pclpy

分享给有需要的人,代码质量勿喷。

  1. import pclpy

  2. from pclpy import pcl


  3. # 读

  4. pc=pclpy.pcl.PointCloud.PointXYZRGBA()

  5. pcl.io.loadPCDFile('F:/test.pcd',pc)


  6. # 显示

  7. viewer=pcl.visualization.PCLVisualizer('Point Cloud viewer')

  8. viewer.addPointCloud(pc)

  9. while(not viewer.wasStopped()):

  10. viewer.spinOnce(100)

五、pyntcloud

pyntcloud是一个Python 3.x库,利用Python科学堆栈的强大功能处理3D点云。

pyntcloud侧重于点云数据处理,例如读写(支持las)、属性、滤波、数据结构组织、构建体素、抽稀、RANSAC等。与Open3D、PyVista等库衔接较好。

介绍:pyntcloud | Read the Docs

GitHub:https://github.com/daavoo/pyntcloud

安装:pip install pyntcloud 或者 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyntcloud

分享给有需要的人,代码质量勿喷。

  1. from pyntcloud import PyntCloud

  2. import open3d as o3d


  3. # io

  4. cloud = PyntCloud.from_file("F:/test.ply")

  5. # structures

  6. kdtree_id = cloud.add_structure("kdtree")

  7. # neighbors

  8. k_neighbors = cloud.get_neighbors(k=5, kdtree=kdtree_id)

  9. # scalar_fields

  10. ev = cloud.add_scalar_field("eigen_values", k_neighbors=k_neighbors)

  11. # filters

  12. f = cloud.get_filter("BBOX", min_x=0.1, max_x=0.8)


  13. # FROM Open3D

  14. original_triangle_mesh = o3d.io.read_triangle_mesh("F:/test.ply")

  15. cloud = PyntCloud.from_instance("open3d", original_triangle_mesh)


  16. # TO Open3D

  17. cloud = PyntCloud.from_file("F:/test.ply")

  18. converted_triangle_mesh = cloud.to_instance("open3d", mesh=True) # mesh=True by default

/* *************************************************  liblas系列  *************************************************** */

六、libLAS

libLAS是一个C/C++/Python库(接触的第一个点云处理库),用于读写LAS格式的点云。libLAS支持ASPRS LAS格式规范版本:1.0、1.1、1.2和1.3(基本支持)。虽然libLAS已经被 PDAL / Laspy 取代,但不可否认,它是一个很nice的库。

libLAS库侧重于点云的读写、修改编辑处理。

介绍:libLAS - LAS 1.0/1.1/1.2 ASPRS LiDAR data translation toolset — liblas.org

API:Python Class Documentation — liblas.org

GitHub:https://github.com/libLAS

安装:pip install liblas  或者  pip install -i https://pypi.tuna.tsinghua.edu.cn/simple liblas

分享给有需要的人,代码质量勿喷。

  1. import liblas

  2. from liblas import file

  3. from liblas import header


  4. # 读

  5. f=file.File('F:/test.las',mode='r')


  6. # 头文件

  7. lasHeader = f.header

  8. print('主版本号:' + str(lasHeader.major_version))

  9. print('副版本号:' + str(lasHeader.minor_version))

  10. print('最小值:%f,%f,%f' % (lasHeader.min[0],lasHeader.min[1],lasHeader.min[2]))

  11. print('最大值:%f,%f,%f' % (lasHeader.max[0],lasHeader.max[1],lasHeader.max[2]))

  12. print('比例:%f,%f,%f' % (lasHeader.scale[0],lasHeader.scale[1],lasHeader.scale[2]))

  13. print('偏移量:%f,%f,%f' % (lasHeader.offset[0],lasHeader.offset[1],lasHeader.offset[2]))

  14. print('点云数量:%d' % (lasHeader.point_records_count))


  15. # 遍历点

  16. for point in f:

  17. # point = f[0]

  18. print('x=%f, y=%f, z=%f, intensity=%d, PointsourceID=%d, GPStime=%f,

  19. Red=%d, Green=%d, Blue=%d, Classification=%d, UserData=%d'

  20. % (point.x, point.y, point.z,

  21. point.intensity, point.point_source_id, point.raw_time,

  22. point.color.red, point.color.green, point.color.blue,

  23. point.classification, point.user_data))


  24. # 写

  25. las_header = header.Header()

  26. las_header.dataformat_id = 1

  27. las_header.minor_version = 2

  28. fw = file.File('F:/new.las', mode='w', header=las_header)

  29. pt = liblas.point.Point()

  30. for i in range(10):

  31. pt.x = 118.0+i

  32. pt.y = 532.0+i

  33. pt.z = 112.0+i

  34. fw.write(pt)

  35. fw.close()


  36. print('ok666')

七、PDAL

libLAS的升级版。PDAL(Point Data Abstraction Library)是一个C/C ++开源库,用于转换和处理点云数据。尽管库中许多重点工具源于LiDAR,但它不限于LiDAR数据。

介绍:PDAL - Point Data Abstraction Library — pdal.io

API:Python Class Documentation — liblas.org

GitHub:https://github.com/PDAL

安装:pip install PDAL(没成功,郁闷)   

八、Laspy

兼容 libLAS 的点云处理python库,与 libLAS算是一家吧。Laspy是一个用于读取、修改和创建LAS LiDAR文件的python库。对LAZ的支持仅限于1.0-1.3版本。Laspy与Python 2.6+和3.5+兼容。Laspy包含一组命令行工具,可用于执行基本文件操作,例如格式转换和验证以及比较LAS文件。

API:Laspy: Documentation — laspy 1.2.5 documentation

API:laspy 2.0

GitHub:https://github.com/grantbrown/laspy

安装:pip install laspy 或者 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple laspy

分享给有需要的人,代码质量勿喷。

  1. import math

  2. import numpy as np

  3. import laspy



  4. # 读

  5. las = laspy.read('E:/data/test.las')


  6. # 头

  7. lasHeader = las.header

  8. print('主版本号:' + str(lasHeader.major_version))

  9. print('副版本号:' + str(lasHeader.minor_version))

  10. print('最小值:%f,%f,%f' % (lasHeader.min[0], lasHeader.min[1], lasHeader.min[2]))

  11. print('最大值:%f,%f,%f' % (lasHeader.max[0], lasHeader.max[1], lasHeader.max[2]))

  12. print('比例:%f,%f,%f' % (lasHeader.scale[0], lasHeader.scale[1], lasHeader.scale[2]))

  13. print('偏移量:%f,%f,%f' % (lasHeader.offset[0], lasHeader.offset[1], lasHeader.offset[2]))

  14. print('点云数量:%d' % (lasHeader.point_records_count))


  15. # 遍历点

  16. points = las.points


  17. def scaled_x_dimension(las_file):

  18. x_dimension = las_file.X

  19. scale = las_file.header.scale[0]

  20. offset = las_file.header.offset[0]

  21. return (x_dimension * scale + offset)


  22. ##################### 遍历点,给GPStime赋新值

  23. gpstime= []

  24. for i in range(lasHeader.point_records_count):

  25. print('x=%f, y=%f, z=%f, intensity=%d, GPStime=%f, PointSourceID=%f, '

  26. 'Classification=%d, UserData=%d, Red=%d, Green=%d, Blue=%d'

  27. % (scaled_x_dimension(las)[i], las.y[i],las.z[i],

  28. las.intensity[i], las.gps_time[i],las.point_source_id[i],

  29. las.classification[i],las.user_data[i],

  30. las.red[i],las.green[i], las.blue[i]))

  31. gpstime.append(i)


  32. ############################writer:原有XYZ不变

  33. new_las = laspy.LasData(las.header)

  34. new_las.x = las.x

  35. new_las.y = las.y

  36. new_las.z = las.z

  37. new_las.intensity = las.intensity

  38. new_las.gps_time = gpstime

  39. new_las.red = las.red

  40. new_las.green = las.green

  41. new_las.blue = las.blue

  42. new_las.write("E:/new_las.las")



  43. ####################################按类别筛选

  44. new_file_c0 = laspy.create(point_format=las.header.point_format,

  45. file_version=las.header.version)

  46. new_file_c0.points = las.points[las.classification == 0]

  47. new_file_c0.write('E:/new_file_c0.las')




  48. #region #################旋转点云,XY变化,头 变化,添加新的属性

  49. #旋转参数

  50. basePx = (lasHeader.min[0]+lasHeader.max[0]) / 2

  51. basePy = (lasHeader.min[1]+lasHeader.max[1]) / 2

  52. cosa = 0.5

  53. sina = math.sqrt(1-cosa*cosa)


  54. #遍历点

  55. xo = []

  56. yo = []

  57. xj = []

  58. for i in range(lasHeader.point_records_count):

  59. x = las.x[i] - basePx

  60. y = las.y[i] - basePy


  61. xnew = x * cosa - y * sina + basePx

  62. ynew = x * sina + y * cosa + basePy

  63. xo.append(xnew)

  64. yo.append(ynew)

  65. xj.append(i)


  66. #保存新的点云

  67. header = laspy.LasHeader(point_format=3, version="1.2")

  68. header.offsets = [np.min(xo), np.min(yo), las.header.offsets[2]]

  69. header.scales = np.array([0.0001, 0.0001, 0.0001])

  70. header.add_extra_dim(laspy.ExtraBytesParams(name="xj", type=np.int32))


  71. new_las = laspy.LasData(header)

  72. new_las.x = xo

  73. new_las.y = yo

  74. new_las.z = las.z

  75. new_las.intensity = las.intensity

  76. new_las.gps_time = las.gps_time

  77. new_las.point_source_id = las.point_source_id

  78. new_las.classification = las.classification

  79. new_las.red = las.red

  80. new_las.green = las.green

  81. new_las.blue = las.blue

  82. new_las.xj = xj


  83. new_las.write('E:/new_file_rotation.las')

  84. #endregion

/* ************************************************************** 其它 *************************************************** */

九、plyfile

plyfile用于读写ply文件。

GitHub:https://github.com/dranjan/python-plyfile

安装:pip install plyfile 或者 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple plyfile

十、point_cloud_utils

Point Cloud Utils (pcu) - A Python library for common tasks on 3D point clouds

挺实用。

GitHub:https://github.com/fwilliams/point-cloud-utils

安装:pip install git+git://github.com/fwilliams/point-cloud-utils

十一、pptk

pptk(Point Processing Toolkit)是用于可视化和处理二三维点云的python包。目前,其具有以下功能:

(1)点云查看,可接受任何3列numpy数组作为输入;

(2)基于Octree的LOD点云渲染可视化;

(3)支持点选,用于检查和注释点数据;

(4)并行化的点KD-tree;

(5)基于点云邻域PCA的法线估计。

介绍:Contents — pptk 0.1.1 documentation

GitHub:https://github.com/heremaps/pptk

安装:pip install pptk 或者 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pptk

分享给有需要的人,代码质量勿喷。

  1. import pptk


  2. # Create 100 random points

  3. xyz = pptk.rand(200, 3)


  4. # Visualize points shaded by height

  5. v = pptk.viewer(xyz, xyz[:, 2])

  6. v.set(point_size=0.005)

十二、PyLidar

PyLidar用于从LiDAR设备中获取数据。其分为PyLidar2和PyLidar3,分别对应python2和python3版本。

介绍:PyLidar — Pylidar 0.4.4 documentation

GitHub:https://github.com/lakshmanmallidi/PyLidar3

安装:pip install PyLidar3

/* ************************************** 以下的已经不维护或者很久没更新了 ********************************************** */

十三、pylas

This code is no longer maintained.

Originally designed as a proof-of-concept for reading Light Detection and Ranging (LIDAR) data in binary LAS format and converting to GIS point data formats (xyz or shapefile). Today, there are much better tools for using LIDAR in python code - this repo is for archival purposes only.

GitHub:https://github.com/perrygeo/pylas

十四、las

The las module implements a reader for LAS (Log ASCII Standard) well log files (LAS 2.0). For more information about this format, see the Canadian Well Logging Society web page (http://www.cwls.org/las/).

GitHub:https://github.com/WarrenWeckesser/las


浏览 396
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报