前言
最近在复现PointRCNN论文过程中发现没有可视化代码,于是查找资料找到了:
pointRCNN 结果可视化——tiatiatiatia
按照参考链接下载代码【轮子库】并可视化成功了
mayavi库的学习
主要是利用了mlab模块:
Mayavi2 旨在提供一个简单、可交互的3D数据或3D绘图的可视化。其做法如下:
(可选)一个丰富的用户界面,可以和数据、对象进行交互。简洁清晰的Python接口,以及可以使用类似于matlab和matplotlib的3D可视化函数(参考:ref:mlab ),或者面向对象的交互界面。其底层封装VTK,但是学习它并不是必须的。此外,Mayavi2是一个可复用的工具箱,您可以用不同的方法整合到您的库或者应用中,结合Envisage应用程序框架组装成特定领域的工具。
import os
import numpy as np
import mayavi.mlab as mlab
rootpath = "/home/rock/PointRCNN/kitti_object_vis/"
datapath = os.path.join(rootpath, "data/object/training/velodyne/")
data = np.fromfile(datapath + "000000.bin", dtype=np.float32).reshape(-1, 4)
fig = mlab.figure(figure=None,
bgcolor=(0, 0, 0),
fgcolor=None,
engine=None,
size=(1600, 1000))
#mayavi显示data颜色按照高度改变
mlab.points3d(data[:, 0],
data[:, 1],
data[:, 2],
data[:, 2],
mode="point",
colormap="gnuplot",
scale_factor=1,
figure=fig)
mlab.points3d(0, 0, 0, color=(1, 1, 1), mode="sphere", scale_factor=0.5)
axes = np.array(
[[2.0, 0.0, 0.0, 0.0], [0.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 0.0]],
dtype=np.float64,
)
mlab.plot3d(
[0, axes[0, 0]],
[0, axes[0, 1]],
[0, axes[0, 2]],
color=(1, 0, 0),
tube_radius=None,
figure=fig,
)
mlab.plot3d(
[0, axes[1, 0]],
[0, axes[1, 1]],
[0, axes[1, 2]],
color=(0, 1, 0),
tube_radius=None,
figure=fig,
)
mlab.plot3d(
[0, axes[2, 0]],
[0, axes[2, 1]],
[0, axes[2, 2]],
color=(0, 0, 1),
tube_radius=None,
figure=fig,
)
fov = np.array(
[[20.0, 20.0, 0.0, 0.0], [20.0, -20.0, 0.0, 0.0]],
dtype=np.float64 # 45 degree
)
mlab.plot3d(
[0, fov[0, 0]],
[0, fov[0, 1]],
[0, fov[0, 2]],
color=(1, 1, 1),
tube_radius=None,
line_width=1,
figure=fig,
)
mlab.plot3d(
[0, fov[1, 0]],
[0, fov[1, 1]],
[0, fov[1, 2]],
color=(1, 1, 1),
tube_radius=None,
line_width=1,
figure=fig,
)
# draw square region
TOP_Y_MIN = -21
TOP_Y_MAX = 21
TOP_X_MIN = 0
TOP_X_MAX = 40
x1 = TOP_X_MIN
x2 = TOP_X_MAX
y1 = TOP_Y_MIN
y2 = TOP_Y_MAX
mlab.plot3d(
[x1, x1],
[y1, y2],
[0, 0],
color=(0.5, 0.5, 0.5),
tube_radius=0.1,
line_width=1,
figure=fig,
)
mlab.plot3d(
[x2, x2],
[y1, y2],
[0, 0],
color=(0.5, 0.5, 0.5),
tube_radius=0.1,
line_width=1,
figure=fig,
)
mlab.plot3d(
[x1, x2],
[y1, y1],
[0, 0],
color=(0.5, 0.5, 0.5),
tube_radius=0.1,
line_width=1,
figure=fig,
)
mlab.plot3d(
[x1, x2],
[y2, y2],
[0, 0],
color=(0.5, 0.5, 0.5),
tube_radius=0.1,
line_width=1,
figure=fig,
)
# mlab.orientation_axes()
#初始化正方体的8个点
p1 = np.array([2, 4, 2])
p2 = np.array([2, 0, 2])
p3 = np.array([0, 0, 2])
p4 = np.array([0, 4, 2])
p5 = np.array([2, 4, -1])
p6 = np.array([2, 0, -1])
p7 = np.array([0, 0, -1])
p8 = np.array([0, 4, -1])
b = np.array([p1, p2, p3, p4, p5, p6, p7, p8])
#平移正方体
b[:, 0] += 5
b[:, 1] += 1
for k in range(0, 4):
# http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html
i, j = k, (k + 1) % 4
mlab.plot3d(
[b[i, 0], b[j, 0]],
[b[i, 1], b[j, 1]],
[b[i, 2], b[j, 2]],
color=(0, 1, 0),
tube_radius=None,
line_width=2,
figure=fig,
)
i, j = k + 4, (k + 1) % 4 + 4
mlab.plot3d(
[b[i, 0], b[j, 0]],
[b[i, 1], b[j, 1]],
[b[i, 2], b[j, 2]],
color=(0, 1, 0),
tube_radius=None,
line_width=2,
figure=fig,
)
i, j = k, k + 4
mlab.plot3d(
[b[i, 0], b[j, 0]],
[b[i, 1], b[j, 1]],
[b[i, 2], b[j, 2]],
color=(0, 1, 0),
tube_radius=None,
line_width=2,
figure=fig,
)
label = "car"
mlab.text3d(
b[1, 0],
b[1, 1],
b[1, 2],
label,
scale=1,
color=(1, 0, 0),
figure=fig,
)
mlab.view(
azimuth=180,
elevation=50,
focalpoint=[12.0909996, -1.04700089, -2.03249991],
distance=62.0,
figure=fig,
)
mlab.show()
tip:另外发现了VSCODE中代码缩进的技巧,非常好用,尤其是对于CV大法过来的代码。
pip install yapf
打开setting 搜索 python.formatting.provider
选中需要自动格式化缩进的代码点击右键 点击Format Selection就好了