点云可视化 实例代码

2024-01-08 02:42:07

目录

bin格式mayavi 效果好

1.安装依赖库 python3.10 ok

2. bin文件可视化 ok

npy可视化

open3d可视化:

显示3d box

npy可视化

matplotlib可视化


bin格式mayavi 效果好

1.安装依赖库 python3.10 ok

pip install PyQt5

pip install vtk

pip install mayavi -i https://pypi.tuna.tsinghua.edu.cn/simple


2. bin文件可视化 ok

import mayavi.mlab
import numpy as np
import os
?
?
def viz_mayavi(points, vals="distance"): ?# 可视化只用到了3维数据(x,y,z)!
? ? x=points[:, 0]
? ? y=points[:, 1]
? ? z=points[:, 2]
? ? r=points[:, 3] ?# reflectance value of point
? ? d=np.sqrt(x**2+y**2)
?
? ? if vals == "height":
? ? ? ? col = z
? ? else:
? ? ? ? col = d
? ? # 创建可视化模板的尺寸
? ? fig=mayavi.mlab.figure(bgcolor=(0, 0, 0), size=(1280, 720))
? ? mayavi.mlab.points3d(x, y, z,
? ? ? ? ? ? ? ? ? ? ? ? ?col,
? ? ? ? ? ? ? ? ? ? ? ? ?mode="point",
? ? ? ? ? ? ? ? ? ? ? ? ?colormap='spectral',
? ? ? ? ? ? ? ? ? ? ? ? ?figure=fig,
? ? ? ? ? ? ? ? ? ? ? ? ?)
?
? ? mayavi.mlab.show()
?
?
if __name__ == "__main__":
? ? bin_file_path = 'bin_2pcd'
? ? bin_files = os.listdir(bin_file_path)
? ? for bin_file in bin_files:
? ? ? ? if bin_file.endswith(".bin"):
? ? ? ? ? ? mypointcloud = np.fromfile(bin_file_path + '/' + bin_file, dtype=np.float32, count=-1).reshape([-1, 4])
? ? ? ? ? ? viz_mayavi(mypointcloud,vals="height")

npy可视化

    import numpy as np
    from mayavi import mlab

    # 加载npy文件
    data = np.load('0001.npy')

    # 可视化数据
    src = mlab.pipeline.scalar_field(data)
    mlab.pipeline.iso_surface(src, contours=[data.min() + 0.1 * data.ptp(), ], opacity=0.5)
    mlab.show()

open3d可视化:

【Open3D】点云可视化 - 知乎

成功可视化bin文件

import math
import numpy as np
import struct
import os
import open3d as o3d


def read_velodyne_bin(path):
    pc_list = []
    with open(path, 'rb') as f:
        content = f.read()
        pc_iter = struct.iter_unpack('ffff', content)
        for idx, point in enumerate(pc_iter):
            pc_list.append([point[0], point[1], point[2]])
    return np.asarray(pc_list, dtype=np.float32)


def show_point(data):
    pcd = o3d.geometry.PointCloud()
    # pcd.points = o3d.utility.Vector3dVector(origin)
    # o3d.visualization.draw_geometries([pcd])

    pcd.points = o3d.utility.Vector3dVector(data)
    o3d.visualization.draw_geometries([pcd])


def main():
    root_dir = r'\\tsclient\velodyne'  # 激光雷达bin文件路径
    cat = os.listdir(root_dir)
    iteration_num = len(cat)
    for i in range(iteration_num):
        filename = os.path.join(root_dir, cat[i])
        db_np = read_velodyne_bin(filename)
        show_point(db_np)


if __name__ == '__main__':

    main()

参考:Open3d显示点云数据_open3d 加载bin代码-CSDN博客

显示3d box

import open3d as o3d
import numpy as np

# 创建一个缩小一半的3D盒子
def create_half_size_3d_box():
    vertices = [
        [0, 0, 0],
        [0.5, 0, 0],
        [0.5, 0.5, 0],
        [0, 0.5, 0],
        [0, 0, 0.5],
        [0.5, 0, 0.5],
        [0.5, 0.5, 0.5],
        [0, 0.5, 0.5]
    ]

    lines = [
        [0, 1], [1, 2], [2, 3], [3, 0],
        [4, 5], [5, 6], [6, 7], [7, 4],
        [0, 4], [1, 5], [2, 6], [3, 7]
    ]

    colors = [
        [1, 0, 0] for _ in range(len(lines))
    ]

    line_set = o3d.geometry.LineSet()
    line_set.points = o3d.utility.Vector3dVector(vertices)
    line_set.lines = o3d.utility.Vector2iVector(lines)
    line_set.colors = o3d.utility.Vector3dVector(colors)

    return line_set

# 显示缩小一半的3D盒子,并将盒子最小化显示在窗口中
def show_half_size_3d_box_with_minimized_display():
    half_size_box = create_half_size_3d_box()

    # 设置摄像机参数
    vis = o3d.visualization.Visualizer()
    vis.create_window(window_name='Open3D', width=800, height=600)
    render_option = vis.get_render_option()
    render_option.background_color = np.asarray([0, 0, 0])  # 设置背景色为黑色 RGB=[0,0,0]

    ctr = vis.get_view_control()
    params = ctr.convert_to_pinhole_camera_parameters()
    intrinsic = params.intrinsic
    intrinsic.set_intrinsics(400, 300, 525, 525, 200, 150)  # 设置视角范围
    ctr.convert_from_pinhole_camera_parameters(params)

    vis.add_geometry(half_size_box)
    vis.run()
    vis.destroy_window()

show_half_size_3d_box_with_minimized_display()

npy可视化

matplotlib可视化

import numpy as np
import matplotlib.pyplot as plt
depthmap = np.load('0000.npy') #使用numpy载入npy文件
plt.imshow(depthmap) #执行这一行后并不会立即看到图像,这一行更像是将depthmap载入到plt里
plt.savefig('depthmap.jpg') #执行后可以将文件保存为jpg格式图像,可以双击直接查看。也可以不执行这一行,直接执行下一行命令进行可视化。但是如果要使用命令行将其保存,则需要将这行代码置于下一行代码之前,不然保存的图像是空白的
plt.show() #在线显示图像

文章来源:https://blog.csdn.net/jacke121/article/details/135376344
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。