天天看點

處理點雲資料(三):三維點雲可視化

三維可視化點雲

使用Mayavi可視化

首先使用anaconda安裝mayavi,打開指令行界面

conda install mayavi
           

如果python版本是py3則會出錯,需要安裝python2.7版本:

然後在py2的環境下安裝mayavi:

conda install -n py2 mayavi
           

如果出現VTK版本不比對,則更新VTK:

py2的目錄在 D:\Anaconda\envs\py2裡面,之後使用pycharm切換版本:

import numpy as np

def viz_mayavi(points, vals="distance"):
    x = points[:, ]  # x position of point
    y = points[:, ]  # y position of point
    z = points[:, ]  # z position of point
    # r = lidar[:, 3]  # reflectance value of point
    d = np.sqrt(x **  + y ** )  # 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=(, , ), size=(, ))
    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 =    # Skip every n points

fig = plt.figure()
ax = fig.add_subplot(, projection='3d')
point_range = range(, points.shape[], skip) # skip points to prevent crash
ax.scatter(points[point_range, ],   # x
           points[point_range, ],   # y
           points[point_range, ],   # z
           c=points[point_range, ], # 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/

繼續閱讀