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:
linestr_convert = list(map(float, linestr))
linestr_convert.append(0)
lidar.append(linestr_convert)
if len(linestr) == 4:
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:
linestr_convert = list(map(float, linestr))
linestr_convert.append(0)
lidar.append(linestr_convert)
if len(linestr) == 4:
linestr_convert = list(map(float, linestr))
lidar = np.array(lidar).astype(np.float32)
np.save("666.npy", lidar)
同时显示多个可视化窗口
import open3d as o3d
from pynput import keyboard
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()
根据最大边界点和最小边界点画边界框
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])
根据长宽高画边界框
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)
将可视化结果保存成图片
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()
画坐标轴
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)
axis_pcd = o3d.geometry.TriangleMesh.create_coordinate_frame(size=5, origin=[0, 0, 0])
vis.add_geometry(axis_pcd)
vis.run()
vis.destroy_window()