python利用open3d以及mayavi可视化pcd点云(二进制)

pcd点云的存储形式一共有两种,分别是binary和ascll码的形式,其详解见https://blog.csdn.net/qq_37534947/article/details/107187907
下面的代码是针对两者的,即都可以适用。
前期准备:
open3d的安装
pip install open3d
pip install open3d-python==0.7.0.0 (这个必须有)
mayavi的安装:
需要顺序安装的包vtk、mayavi
这里推荐whl下载,因为pip直接安装vtk比较慢,下载地址:
https://download.csdn.net/download/qq_37534947/12515312

#binary
import open3d as o3d
import numpy as np
import numpy as np
import mayavi.mlab
def read_pcd(file_path1):
    pcd = o3d.io.read_point_cloud(file_path1)
    #pcd2 = o3d.io.read_point_cloud(file_path2)
    print(np.asarray(pcd.points))
    #print(np.asarray(pcd2.points))
    #colors = np.asarray(pcd.colors) * 255
    pointcloud = np.asarray(pcd.points)
    #pointcloud2 = np.asarray(pcd2.points)
    print(pointcloud.shape)
    #print(pointcloud2.shape)
    x = pointcloud[:, 0]  # x position of point
    xmin = np.amin(x, axis=0)
    xmax = np.amax(x, axis=0 )
    y = pointcloud[:, 1]  # y position of point
    ymin = np.amin(y, axis=0)
    ymax = np.amax(y, axis=0)
    z = pointcloud[:, 2]  # z position of point
    zmin = np.amin(z, axis=0)
    zmax = np.amax(z, axis=0)
    print(xmin,xmax,ymin,ymax,zmin,zmax)
    d = np.sqrt(x ** 2 + y ** 2)  # Map Distance from sensor
    vals = 'height'
    if vals == "height":
        col = z
    else:
        col = d
    fig = mayavi.mlab.figure(bgcolor=(0, 0, 0), size=(640, 500))
    mayavi.mlab.points3d(x, y, z,
                         col,  # Values used for Color
                         mode="point",
                         # 灰度图的伪彩映射
                         colormap='spectral',  # 'bone', 'copper', 'gnuplot'
                         # color=(0, 1, 0),   # Used a fixed (r,g,b) instead
                         figure=fig,
                         )
    # 绘制原点
    mayavi.mlab.points3d(0, 0, 0, color=(1, 1, 1), mode="sphere",scale_factor=0.2)
    # 绘制坐标
    axes = np.array(
        [[20.0, 0.0, 0.0, 0.0], [0.0, 20.0, 0.0, 0.0], [0.0, 0.0, 20.0, 0.0]],
        dtype=np.float64,
    )
    #x轴
    mayavi.mlab.plot3d(
        [0, axes[0, 0]],
        [0, axes[0, 1]],
        [0, axes[0, 2]],
        color=(1, 0, 0),
        tube_radius=None,
        figure=fig,
    )
    #y轴
    mayavi.mlab.plot3d(
        [0, axes[1, 0]],
        [0, axes[1, 1]],
        [0, axes[1, 2]],
        color=(0, 1, 0),
        tube_radius=None,
        figure=fig,
    )
    #z轴
    mayavi.mlab.plot3d(
        [0, axes[2, 0]],
        [0, axes[2, 1]],
        [0, axes[2, 2]],
        color=(0, 0, 1),
        tube_radius=None,
        figure=fig,
    )
    mayavi.mlab.show()

#mayavi显示点云
read_pcd("pcdfolder/Benewake_Horn_X2_PointCloud_0023.pcd")#,"pcdfolder/Benewake_Horn_X2_PointCloud_0001.pcd")

分析:
1)这里open3d的主要作用是打开pcd格式的文件,然后存储到pcd 变量中,可以从shape中看到其大小是(x,3),其中x是指点云的个数。
2)mayavi主要是用于显示点云,其中包括显示所有的点以及绘制横坐标以及纵坐标和绘制原点等。
其使用参考链接:
https://www.cnblogs.com/dalanjing/p/12289517.html
https://www.cnblogs.com/ssyfj/p/9303698.html

结果截图:
python利用open3d以及mayavi可视化pcd点云(二进制)_第1张图片

你可能感兴趣的:(tensorflow,深度学习)