目录
pip install PyQt5
pip install vtk
pip install mayavi -i https://pypi.tuna.tsinghua.edu.cn/simple
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"
# coding=utf-8
import numpy as np
import mayavi.mlab
# lidar_path换成自己的.bin文件路径
def main(lidar_path):
pointcloud = np.fromfile(lidar_path, dtype=np.float32, count=-1).reshape([-1, 4])
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
degr = np.degrees(np.arctan(z / d))
vals = 'height'
if vals == "height":
col = z
else:
col = d
fig = mayavi.mlab.figure(bgcolor=(0, 0, 0), size=(640, 500))
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,
)
mayavi.mlab.show()
if __name__ == '__main__':
lidar_path = r'\\velodyne\000000.bin'
main(lidar_path)
成功可视化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博客
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()
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() #在线显示图像