使用open3d可视化点云,需要将点云制作为ply
文件传入函数中。
安装open3d
直接使用pip安装
pip install open3d
ply文件
ply文件的结构为:文件头+数据内容,文件头从 ply 一行开始,到 end_header 为止,里面定义了此 ply文件中数据的组织形式,包括数据类型和个数。后面是点云的坐标信息及每个点的颜色属性的,参考自
如下为具有5个点的点云,end_header
后每一行表示一个点的x,y,z,R,G,B
信息
ply
format ascii 1.0
comment PCL generated
element vertex 5
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
end_header
-0.25412 -0.11759 0.01171 255 0 255
0.18866 0.0793 0.00609 255 0 255
0.03777 0.06232 -0.00753 255 0 255
-0.25185 -0.08387 -0.01735 255 0 255
-0.35174 -0.16687 -0.00875 0 255 0
假设该文件为test.ply
文件,则pyhon中使用open3d进行可视化命令为
import open3d as o3d
pcd = o3d.io.read_point_cloud('test.ply')
o3d.visualization.draw_geometries([pcd])
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200914140517166.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzQ4Njc4MA==,size_16,color_FFFFFF,t_70#pic_center)
自制ply文件
将numpy格式的点云转换为ply文件进行可视化很简单,根据上面的格式来写文件即可,下面给出一个参考
import open3d as o3d
import numpy as np
def makePlyFile(xyzs, labels, fileName='makeply.ply'):
'''Make a ply file for open3d.visualization.draw_geometries
:param xyzs: numpy array of point clouds 3D coordinate, shape (numpoints, 3).
:param labels: numpy array of point label, shape (numpoints, ).
'''
RGBS = [
(0, 255, 255),
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(0, 0, 0),
(255, 0, 255)
]
with open(fileName, 'w') as f:
f.write('ply\n')
f.write('format ascii 1.0\n')
f.write('comment PCL generated\n')
f.write('element vertex {}\n'.format(len(xyzs)))
f.write('property float x\n')
f.write('property float y\n')
f.write('property float z\n')
f.write('property uchar red\n')
f.write('property uchar green\n')
f.write('property uchar blue\n')
f.write('end_header\n')
for i in range(len(xyzs)):
r, g, b = RGBS[int(labels[i])-1]
x, y, z = xyzs[i]
f.write('{} {} {} {} {} {}\n'.format(x, y, z, r, g, b))
if __name__ == "__main__":
numpoints = 2000
xyzs = np.random.rand(numpoints, 3)
labels = np.random.randint(0, 4, numpoints)
makePlyFile(xyzs, labels, 'demo.ply')
pcd = o3d.io.read_point_cloud('demo.ply')
o3d.visualization.draw_geometries([pcd])
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200914141124190.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzQ4Njc4MA==,size_16,color_FFFFFF,t_70#pic_center)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)