有时在绘制点云图时需要用颜色来表征其高度。这个功能在成熟软件中比较常见,自己实现起来略显得繁琐,貌似Open3d绘图选项中没有这个功能(各位如若发现了告诉我),此处把实现代码公布一下,欢迎各位探讨交流。
首先上绘制效果图:
相关代码:
import numpy as np
import open3d as o3d
import open3d.visualization.gui as gui
import open3d.visualization.rendering as rendering
app = gui.Application.instance
app.initialize()
#cloud generation
pts = np.random.uniform(-1, 1, size=[1000, 3]) + [0, 0, 0]
cloud = o3d.geometry.PointCloud()
cloud.points = o3d.utility.Vector3dVector(pts)
#color generation
colors = np.zeros([pts.shape[0], 3])
height_max = np.max(pts[:, 2])
height_min = np.min(pts[:, 2])
delta_c = abs(height_max - height_min) / (255 * 2)
for j in range(pts.shape[0]):
color_n = (pts[j, 2] - height_min) / delta_c
if color_n <= 255:
colors[j, :] = [0, 1 - color_n / 255, 1]
else:
colors[j, :] = [(color_n - 255) / 255, 0, 1]
cloud.colors = o3d.utility.Vector3dVector(colors)
vis = o3d.visualization.O3DVisualizer("Open3D - 3D Text", 1024, 768)
vis.show_settings = True
vis.add_geometry("Points", cloud)
app.add_window(vis)
app.run()
绘制高度颜色图时需要自己选择一下渐变的颜色,如果颜色选择不好显示效果会不令人满意。此处选择三种颜色,如色环所示:
三种颜色为:[0, 255, 255]->[0, 0, 255]->[255, 0, 255]
选择好合适的颜色填入代码中即可(注意颜色要归一)。也可以选择更丰富的颜色以适应绘图需要。