三维可视化点云
使用Mayavi可视化
首先使用anaconda安装mayavi,打开命令行界面
conda install mayavi
如果python版本是py3则会出错,需要安装python2.7版本:
conda create -n py2 python=2
然后在py2的环境下安装mayavi:
conda install -n py2 mayavi
如果出现VTK版本不匹配,则更新VTK:
conda update -n py2 vtk
py2的目录在 D:\Anaconda\envs\py2里面,之后使用pycharm切换版本:
import numpy as np
def viz_mayavi(points, vals="distance"):
x = points[:, 0] # x position of point
y = points[:, 1] # y position of point
z = points[:, 2] # z position of point
# r = lidar[:, 3] # reflectance value of point
d = np.sqrt(x ** 2 + y ** 2) # Map Distance from sensor
# Plot using mayavi -Much faster and smoother than matplotlib
import mayavi.mlab
if vals == "height":
col = z
else:
col = d
fig = mayavi.mlab.figure(bgcolor=(0, 0, 0), size=(640, 360))
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()
points = np.loadtxt('0000000000.txt')
viz_mayavi(points)
使用Matplotlib可视化
pycharm切换回py3版本:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
points = np.loadtxt('0000000000.txt')
skip = 20 # Skip every n points
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
point_range = range(0, points.shape[0], skip) # skip points to prevent crash
ax.scatter(points[point_range, 0], # x
points[point_range, 1], # y
points[point_range, 2], # z
c=points[point_range, 2], # height data for color
cmap='spectral',
marker="x")
ax.axis('scaled') # {equal, scaled}
plt.show()
特别说明:本文为本人学习所做笔记。
具体参考:http://ronny.rest/tutorials/module/pointclouds_01/point_cloud_birdseye/