点云数据的网格化处理

此代码主要做了以下工作:
1.对文件夹下的点云加载,并将所有的points存入一个ndarray里(N*3)
2.进行voxel down sample
3.设置块大小,按块进行取deltas = zmax-z_min
4.最终用pygal进行渲染展示(由于连接的服务器,输出svg查看)
查看可使用smpy包查看
python3 -m smpy.tools.svg_player2 statistics.svg

from typing import List
import o3d_hdmap.open3d as o3d
import numpy as np
import glob
from collections import defaultdict
import pygal
 
def read_point_cloud(pcds_path:List):
    # 读取所有pcd,存储到cloud中 ==> cloud: N*3
    clouds = [np.asarray(o3d.io.read_point_cloud(p).points) for p in pcds_path]
    cloud = np.vstack(clouds)
    return cloud
 
if __name__ == '__main__':
 
    # paths = glob.iglob('/data/hongyuan/work/test/**/*.pcd', recursive=True)
    paths = glob.iglob('/data/mpcv_lspo_download_data/prod/PLEF06803-2021-10-27-15-41-02/**/*.pcd', recursive=True)  # 若报错加个r不进行转义
    points = read_point_cloud(paths)
    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(points)
    # voxel_down_sample
    pcd = pcd.voxel_down_sample(voxel_size=0.2)
    points = np.asarray(pcd.points)
    box_min = points.min(axis=0)[:2]
    # split_point_cloud
    grid_size = 1
    indices = (np.floor(
            (points[:, :2] - box_min) / grid_size)).astype(int)
    # grids = sorted(set([tuple(t) for t in indices]))
    splited = defaultdict(list)
    for i in range(indices.shape[0]):
        splited[(indices[i,0], indices[i,1])].append(points[i])
    # z_max,z_min by grid
    deltas = []
    for ind,points in splited.items():
        z_list = []
        for point in points:
            z_list.append(point[2])
        z_max = max(z_list)
        z_min = min(z_list)
        print(ind, z_min, z_max)
        deltas.append(z_max - z_min)
    deltas.sort()
    # 用pygal 将块的zmax-zmin值进行展示
    line_chart = pygal.Line()
    line_chart.title = 'statistics'
    line_chart.add('zmax - zmin',  deltas)
    line_chart.render()
    line_chart.render_to_file('/data/hongyuan/work/statistics.svg')
 
    print()

你可能感兴趣的:(自动驾驶,python,深度学习)