点云数据(激光雷达)显示python代码

  点云数据显示是查看点云数据及感性认识必不可少的一个环节。因为在实际场景中,可能会涉及到很多的算法步骤,处理结果是不是我们自己想要的,就需要查看点云数据。

为此,我将以此博客记录如何使用python代码实现点云数据的显示,如下:

 

复制即可使用代码如下:

import numpy as np
import mayavi.mlab
import os
def build_dir(root):
    if not os.path.exists(root):
        os.makedirs(root)
    return root
def visul_velodyne(velodyne_root, save_path=None, show=False):
    # 000010.bin这里需要填写文件的位置
    pointcloud = np.fromfile(velodyne_root, dtype=np.float32, count=-1).reshape([-1, 4])
    print(pointcloud.shape)
    x = pointcloud[:, 0]  # x position of point
    y = pointcloud[:, 1]  # y position of point
    z = pointcloud[:, 2]  # z position of point
    r = pointcloud[:, 3]  # reflectance value of point
    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))
    # fig = mayavi.mlab.figure(bgcolor=(0, 0, 0), size=(1908, 1024))
    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,
                         )
    x = np.linspace(5, 5, 50)
    y = np.linspace(0, 0, 50)
    z = np.linspace(0, 5, 50)
    mayavi.mlab.plot3d(x, y, z)
    if save_path is not None:
        mayavi.mlab.savefig(filename=save_path)  # 保存图片 filename为“.../../*.png”
    if show:
        mayavi.mlab.show()

if __name__ == '__main__':
    velodyne_file = r'F:\ThrDet\data\kitti_d\training\velodyne'
    for name in os.listdir(velodyne_file):
        if name[-3:] == 'bin':
            velodyne_root = os.path.join(velodyne_file, name)
            out_dir = build_dir(os.path.join(velodyne_file, 'out_dir'))
            save_path = os.path.join(out_dir, name[:-3] + 'png')
            visul_velodyne(velodyne_root, save_path=save_path, show=True)

 

 

代码测试结果显示如下:

 

 

 

posted @ 2022-11-15 15:18  tangjunjun  阅读(1775)  评论(0编辑  收藏  举报
https://rpc.cnblogs.com/metaweblog/tangjunjun