open3d可视化点云

open3d可视化点云

个人博客

将pcd文件转换成bin文件

import open3d as o3d
import numpy as np


pcd = o3d.io.read_point_cloud("data/output.pcd")
points = np.array(pcd.points)

lidar = []
for linestr in points:
    if len(linestr) == 3:  # only x,y,z
        linestr_convert = list(map(float, linestr))
        linestr_convert.append(0)
        lidar.append(linestr_convert)
    if len(linestr) == 4:  # x,y,z,i
        linestr_convert = list(map(float, linestr))
lidar = np.array(lidar).astype(np.float32)
lidar.tofile("666.bin")

将pcd文件转化成npy文件

import open3d as o3d
import numpy as np

lidar = []
pcd = o3d.io.read_point_cloud("data/output.pcd")
points = np.array(pcd.points)

for linestr in points:
    if len(linestr) == 3:  # only x,y,z
        linestr_convert = list(map(float, linestr))
        linestr_convert.append(0)
        lidar.append(linestr_convert)
    if len(linestr) == 4:  # x,y,z,i
        linestr_convert = list(map(float, linestr))

lidar = np.array(lidar).astype(np.float32)
np.save("666.npy", lidar)

同时显示多个可视化窗口

# 同时显示两个窗口,按ESC退出
import open3d as o3d
from pynput import keyboard    # 加载pynput用于键盘监听


# 当键盘按下时的回调函数
def on_press(key):
    global stop_flag
    if key == keyboard.Key.esc:
        stop_flag = True


pcd_1 = o3d.io.read_point_cloud("data/room.pcd")
vis_1 = o3d.visualization.Visualizer()

pcd_2 = o3d.io.read_point_cloud("data/bunny.pcd")
vis_2 = o3d.visualization.Visualizer()

# 创建窗口
vis_1.create_window(window_name="Window 1", width=1080, height=720)
vis_2.create_window(window_name="Window 2", width=1080, height=720)

# 添加点云数据
vis_1.add_geometry(pcd_1)
vis_2.add_geometry(pcd_2)

# 开始键盘监听
listener = keyboard.Listener(on_press=on_press)
listener.start()

# 窗口循环渲染
stop_flag = False
while not stop_flag:
    vis_1.poll_events()
    vis_1.update_renderer()
    
    vis_2.poll_events()
    vis_2.update_renderer()

# 销毁窗口
vis_1.destroy_window()
vis_2.destroy_window()

# 停止监听
listener.stop()

open3d可视化点云_第1张图片

根据最大边界点和最小边界点画边界框

import open3d as o3d
import numpy as np

# 生成一个随机点云
pcd = o3d.geometry.PointCloud()
points = np.random.rand(100, 3)
pcd.points = o3d.utility.Vector3dVector(points)

# 获取点云边界框的对角线两个点
min_bound = np.min(points, axis=0)
max_bound = np.max(points, axis=0)

# 使用对角线的两个点创建一个包围盒
bbox = o3d.geometry.AxisAlignedBoundingBox(min_bound, max_bound)

# 创建一个边界框
bbox_lines = o3d.geometry.LineSet.create_from_axis_aligned_bounding_box(bbox)
bbox_lines.paint_uniform_color([1, 0, 0])
# 可视化结果
o3d.visualization.draw_geometries([pcd, bbox_lines])

open3d可视化点云_第2张图片

根据长宽高画边界框

import open3d as o3d
import numpy as np

x = 10
y = 10
z = 10
# 创建立方体网格
mesh_box = o3d.geometry.TriangleMesh.create_box(width=x, height=y, depth=z)

# 将网格放置在原点
mesh_box.translate([-x/2, -y/2, -z/2])

# 创建一个边界框
bbox_lines = o3d.geometry.LineSet.create_from_triangle_mesh(mesh_box)
bbox_lines.paint_uniform_color([1, 0, 0])

# 可视化结果
o3d.visualization.draw_geometries([bbox_lines], width=1080, height=720)

open3d可视化点云_第3张图片

将可视化结果保存成图片

import open3d as o3d


pcd = o3d.io.read_point_cloud("data/bun_zipper.ply")

# 获取边界框
obb = pcd.get_oriented_bounding_box()

# 设置边界框颜色
obb.color = [1, 0, 0]

vis = o3d.visualization.Visualizer()
vis.create_window(width=1080, height=720)
vis.add_geometry(pcd)
vis.add_geometry(obb)
vis.run()
# 保存图片
vis.capture_screen_image("output.png")
vis.destroy_window()

open3d可视化点云_第4张图片

画坐标轴

import open3d as o3d

vis = o3d.visualization.Visualizer()
vis.create_window(width=1080, height=720)

vis.get_render_option().point_size = 1
vis.get_render_option().background_color = [0, 0, 0]

bin_path = '/home/lightning/work/bishe/VLP16/cap_data/17.bin'
bin = np.fromfile(bin_path, dtype=np.float32, count=-1).reshape(-1, 4)
bin_xyz = bin[:, :3]
pcd = o3d.open3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(bin_xyz)

vis.add_geometry(pcd)

# 画坐标轴,蓝色Z轴 绿色Y轴 红色X轴
axis_pcd = o3d.geometry.TriangleMesh.create_coordinate_frame(size=5, origin=[0, 0, 0])
vis.add_geometry(axis_pcd)

vis.run()
vis.destroy_window()

open3d可视化点云_第5张图片

你可能感兴趣的:(python,numpy,机器学习,三维点云,open3d)