赞
踩
想对于c++版本的PCL,python处理点云的库还是比较多的,下面对此进行简单的总结:
一、Open3D
Open3D是一个支持3D数据处理软件快速开发的开源库。Open3D使用C++和Python公开了一组精心选择的数据结构和算法。后端经过高度优化,并设置为并行化。Open3D的依赖项较少,可在不同的平台上编译与布置。Open3D侧重于三维数据的可视化与整体处理算法。
GitHub:https://github.com/intel-isl/Open3D
安装:pip install open3d 或者 pip3 install open3d -i https://pypi.tsinghua.edu.cn/simple
- import open3d as o3d
- import numpy as np
- fromm atplotlib import pyplot as plt
- # read PC
- pcd = o3d.io.read_point_cloud("F:/test.pcd")
- # # write PC
- # o3d.io.write_point_cloud("F:/newFile.pcd",pcd)
- # DBSCAN
- with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
- labels = np.array(pcd.cluster_dbscan(eps=0.1, min_points=10, print_progress=True))
- max_label = labels.max()
- print(f"point cloud has{max_label +1}clusters")
- colors = plt.get_cmap("tab20")(labels / (max_labelifmax_label >0else1))
- colors[labels <0] =0
- pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])
- # 可视化
- o3d.visualization.draw_geometries([pcd],width=910,height=540)
PyVista具有可视化工具包(VTK)的高级API,空间数据集的网格数据结构和过滤方法,使3D绘图变得简单,可用于大型/复杂数据几何。PyVista(以前称为vtki)是可视化工具包(VTK)的帮助程序模块,它通过NumPy和直接数组访问采用了与VTK接口不同的方法。该软件包提供了Pythonic的,文档齐全的界面,该界面公开了VTK强大的可视化后端,以促进对空间参考数据集的快速原型制作,分析和可视化集成。该模块可用于演示文稿和研究论文的科学绘图,以及其他与网格相关的Python模块的支持模块。PyVista侧重于可视化。GitHub:https://github.com/pyvista/pyvista
安装:pip install pyvista
- import pyvista as pv
- mesh = pv.read('F:/test.vtk')
- mesh.plot(screenshot='F:/test.vtk.png')
PCL(Point Cloud Library)是主要用于点云(二三维图像也可)的独立、强大的开源项目,BSD协议,可免费用于商业和研究用途。PCL是点云数据处理的王者库,不过更多使用的是c++版进行点云处理,GitHub:https://github.com/PointCloudLibrary
pclpy功能与python-pcl类似,安装简单,接口齐全,而且支持las。但是由于一些接口和功能还在开发中,可能会出现不稳定情况,目前只支持Windows和python 3.6 x64。
GitHub:https://github.com/davidcaron/pclpy
安装:pip install pclpy
- import pclpy
- from pclpy import pcl
- # 读
- pc=pclpy.pcl.PointCloud.PointXYZRGBA()
- pcl.io.loadPCDFile('F:/test.pcd',pc)
- # 显示
- viewer=pcl.visualization.PCLVisualizer('Point Cloud viewer')
- viewer.addPointCloud(pc)
- while(notviewer.wasStopped()):
- viewer.spinOnce(100)
pyntcloud是一个Python 3.x库,利用Python科学堆栈的强大功能处理3D点云。pyntcloud侧重于点云数据处理,例如读写(支持las)、属性、滤波、数据结构组织、构建体素、抽稀、RANSAC等。与Open3D、PyVista等库衔接较好。GitHub:https://github.com/daavoo/pyntcloud
安装:pip install pyntcloud
- from pyntcloud import PyntCloud
- import open3d as o3d
- # io
- cloud = PyntCloud.from_file("F:/test.ply")
- # structures
- kdtree_id = cloud.add_structure("kdtree")
- # neighbors
- k_neighbors = cloud.get_neighbors(k=5, kdtree=kdtree_id)
- # scalar_fields
- ev = cloud.add_scalar_field("eigen_values", k_neighbors=k_neighbors)
- # filters
- f = cloud.get_filter("BBOX", min_x=0.1, max_x=0.8)
- # FROM Open3D
- original_triangle_mesh = o3d.io.read_triangle_mesh("F:/test.ply")
- cloud = PyntCloud.from_instance("open3d", original_triangle_mesh)
- # TO Open3D
- cloud = PyntCloud.from_file("F:/test.ply")
- converted_triangle_mesh = cloud.to_instance("open3d", mesh=True)# mesh=True by default
libLAS是一个C/C++/Python库(接触的第一个点云处理库),用于读写LAS格式的点云。libLAS支持ASPRS LAS格式规范版本:1.0、1.1、1.2和1.3(基本支持)。虽然libLAS已经被 PDAL / Laspy 取代,但不可否认,它是一个很nice的库。libLAS库侧重于点云的读写、修改编辑处理。
GitHub:https://github.com/libLAS
安装:pip install liblas
- import liblas
- from liblas import file
- from liblas import header
- # 读
- f=file.File('F:/test.las',mode='r')
- # 头文件
- lasHeader = f.header
- print('主版本号:'+ str(lasHeader.major_version))
- print('副版本号:'+ str(lasHeader.minor_version))
- print('最小值:%f,%f,%f'% (lasHeader.min[0],lasHeader.min[1],lasHeader.min[2]))
- print('最大值:%f,%f,%f'% (lasHeader.max[0],lasHeader.max[1],lasHeader.max[2]))
- print('比例:%f,%f,%f'% (lasHeader.scale[0],lasHeader.scale[1],lasHeader.scale[2]))
- print('偏移量:%f,%f,%f'% (lasHeader.offset[0],lasHeader.offset[1],lasHeader.offset[2]))
- print('点云数量:%d'% (lasHeader.point_records_count))
- # 遍历点
- for point in f:
- # point = f[0]
- print('x=%f, y=%f, z=%f, intensity=%d, PointsourceID=%d, GPStime=%f,
- Red=%d, Green=%d, Blue=%d, Classification=%d, UserData=%d'
- % (point.x, point.y, point.z,
- point.intensity, point.point_source_id, point.raw_time,
- point.color.red, point.color.green, point.color.blue,
- point.classification, point.user_data))
- # 写
- las_header = header.Header()
- las_header.dataformat_id =1
- las_header.minor_version =2
- fw = file.File('F:/new.las', mode='w', header=las_header)
- pt = liblas.point.Point()
- for i in range(10):
- pt.x =118.0+i
- pt.y =532.0+i
- pt.z =112.0+i
- fw.write(pt)
- fw.close()
- print('ok666')
libLAS的升级版。PDAL(Point Data Abstraction Library)是一个C/C ++开源库,用于转换和处理点云数据。尽管库中许多重点工具源于LiDAR,但它不限于LiDAR数据。GitHub:https://github.com/PDAL
安装:pip install PDAL
兼容 libLAS 的点云处理python库,与 libLAS算是一家吧。Laspy是一个用于读取、修改和创建LAS LiDAR文件的python库。对LAZ的支持仅限于1.0-1.3版本。Laspy与Python 2.6+和3.5+兼容。Laspy包含一组命令行工具,可用于执行基本文件操作,例如格式转换和验证以及比较LAS文件。
GitHub:https://github.com/grantbrown/laspy
安装:pip install laspy
- import numpy as np
- import laspy
- from laspy.file import File
- # 读
- f = File('F:/test.las', mode='r')
- # 头
- lasHeader = f.header
- print('主版本号:'+ str(lasHeader.major_version))
- print('副版本号:'+ str(lasHeader.minor_version))
- print('最小值:%f,%f,%f'% (lasHeader.min[0],lasHeader.min[1],lasHeader.min[2]))
- print('最大值:%f,%f,%f'% (lasHeader.max[0],lasHeader.max[1],lasHeader.max[2]))
- print('比例:%f,%f,%f'% (lasHeader.scale[0],lasHeader.scale[1],lasHeader.scale[2]))
- print('偏移量:%f,%f,%f'% (lasHeader.offset[0],lasHeader.offset[1],lasHeader.offset[2]))
- print('点云数量:%d'% (lasHeader.point_records_count))
- # 遍历点
- points = f.points
- def scaled_x_dimension(las_file):
- x_dimension = las_file.X
- scale = las_file.header.scale[0]
- offset = las_file.header.offset[0]
- return(x_dimension*scale + offset)
- for i in range(66,666):
- print('x=%f, y=%f, z=%f, intensity=%d, GPStime=%f, Classification=%d, UserData=%d,
- Red=%d, Green=%d, Blue=%d'
- % (scaled_x_dimension(f)[i], f.y[i],f.z[i],
- f.intensity[i], f.gps_time[i],f.classification[i],f.user_data[i],
- f.red[i],f.green[i], f.blue[i]))
- # 筛选路面点
- c11 = f.Classification ==11
- # 保存路面点为新文件
- outFile = File('F:/output.las', mode='w', header=f.header)
- outFile.points = f.points[c11]
- outFile.close()
- print("ok")
plyfile用于读写ply文件。GitHub:https://github.com/dranjan/python-plyfile
安装:pip install plyfile 或者 pip3 install -i Simple Index plyfile
具体可以参考GitHub:https://github.com/fwilliams/point-cloud-utils
安装:pip install git+git://github.com/fwilliams/point-cloud-utils
pptk(Point Processing Toolkit)是用于可视化和处理二三维点云的python包。目前,其具有以下功能:
(1)点云查看,可接受任何3列numpy数组作为输入;
(2)基于Octree的LOD点云渲染可视化;
(3)支持点选,用于检查和注释点数据;
(4)并行化的点KD-tree;
(5)基于点云邻域PCA的法线估计。
介绍:https://heremaps.github.io/pptk/index.html
GitHub:https://github.com/heremaps/pptk
安装:pip install pptk
- # Create 100 random points
- xyz = pptk.rand(200,3)
-
- # Visualize points shaded by height
- v = pptk.viewer(xyz, xyz[:,2])
- v.set(point_size=0.005)
PyLidar用于从LiDAR设备中获取数据。其分为PyLidar2和PyLidar3,分别对应python2和python3版本。GitHub:https://github.com/lakshmanmallidi/PyLidar3
安装:pip install PyLidar3
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
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
十五、pypcd
安装(直接安装如果出现“No module named 'cStringIO'”错误,推荐使用如下方式安装)
pip3 install --upgrade git+https://github.com/klintan/pypcd.git
具体使用示例
- from pypcd import pypcd
-
- ###read from path
- cloud = pypcd.PointCloud.from_path(pcd_path)
- new_cloud_data_xyz = cloud.pc_data.view(np.float32).reshape(cloud.pc_data.shape + (-1,)) ##需要注意的是这里np.float32和np.int32数据类型不同,结果会有很大差异
- new_cloud_data_x=cloud.pc_data["x"] ##这样取单个维度数据
-
- # # save as binary compressed
- # new_cloud_data_xyz.save_pcd("xxx.pcd" , compression='binary_compressed')
-
-
- ###read from npy asc,ndarry
- cloud_data=np.loadtxt(asc_path).astype(np.float32)
- if is_change:
- tmp_curvature=np.array([2]*cloud_data[:,4].shape[0]).reshape(-1,1)
- else:
- tmp_curvature=cloud_data[:,4].reshape(-1,1)
- new_cloud_data=np.hstack([cloud_data[:,:4],cloud_data[:,5:],tmp_curvature]).astype(np.float32)
- #np.savetxt(asc_path.replace(".asc","_refine.asc"),new_cloud_data)
- new_cloud_data=new_cloud_data.view(np.dtype([('x', np.float32), ('y', np.float32), ('z', np.float32), ('intensity', np.float32),('normal_x',np.float32),('normal_y', np.float32), ('normal_z', np.float32),('curvature',np.float32)])).squeeze()
- pcd=pypcd.PointCloud.from_array(new_cloud_data) ###import
- pcd.save_pcd(asc_path.replace(".asc",".pcd"),compression='binary_compressed')
以上就是基于python常见库对于点云的操作,对于常见的pointxyz等数据结构,都基本满足使用,但是对于自定义的一些数据结构open3d就不一定满足了,这里就推荐使用pypcd了.如果上述的库不同通过pip实现安装,加上镜像也不行,可以试试去github上拉取源码进行编译安装。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。