目录
1. 简单可视化
import os
import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt
pyexample_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
test_data_path = os.path.join(os.path.dirname(pyexample_path), 'test_data')
def custom_draw_geometry(pcd):
"""没有任何骚操作, 单纯显示pcd点云"""
# The following code achieves the same effect as:
# o3d.visualization.draw_geometries([pcd])
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd)
vis.run()
vis.destroy_window()
if __name__ == "__main__":
# 1. load pcd
sample_data = o3d.data.DemoCustomVisualization()
pcd_flipped = o3d.io.read_point_cloud(sample_data.point_cloud_path)
# Flip it, otherwise the pointcloud will be upside down
pcd_flipped.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0],
[0, 0, 0, 1]])
print("1. Customized visualization to mimic DrawGeometry")
custom_draw_geometry(pcd_flipped)
2. 改变视场角
看不出来有啥变化
def custom_draw_geometry_with_custom_fov(pcd, fov_step):
"""
改变相机的视场角: 表征视场范围
:param pcd:
:param fov_step:
:return:
"""
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd)
ctr = vis.get_view_control()
print("Field of view (before changing) %.2f" % ctr.get_field_of_view())
ctr.change_field_of_view(step=fov_step)
print("Field of view (after changing) %.2f" % ctr.get_field_of_view())
vis.run()
vis.destroy_window()
if __name__ == "__main__":
# 1. load pcd
sample_data = o3d.data.DemoCustomVisualization()
pcd_flipped = o3d.io.read_point_cloud(sample_data.point_cloud_path)
# Flip it, otherwise the pointcloud will be upside down
pcd_flipped.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0],
[0, 0, 0, 1]])
print("2. Changing field of view")
custom_draw_geometry_with_custom_fov(pcd_flipped, 90.0)
custom_draw_geometry_with_custom_fov(pcd_flipped, -90.0)
3. 旋转视场
def custom_draw_geometry_with_rotation(pcd):
"""通过回调函数
旋转视场
"""
def rotate_view(vis):
ctr = vis.get_view_control()
ctr.rotate(10.0, 0.0) # ?
return False
o3d.visualization.draw_geometries_with_animation_callback([pcd],
rotate_view)
if __name__ == "__main__":
# 1. load pcd
sample_data = o3d.data.DemoCustomVisualization()
pcd_flipped = o3d.io.read_point_cloud(sample_data.point_cloud_path)
# Flip it, otherwise the pointcloud will be upside down
pcd_flipped.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0],
[0, 0, 0, 1]])
print("3. Customized visualization with a rotating view")
custom_draw_geometry_with_rotation(pcd_flipped)
4. 法向量渲染
def custom_draw_geometry_load_option(pcd, render_option_path):
"""渲染文件控制"""
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd)
vis.get_render_option().load_from_json(render_option_path)
vis.run()
vis.destroy_window()
if __name__ == "__main__":
# 1. load pcd
sample_data = o3d.data.DemoCustomVisualization()
pcd_flipped = o3d.io.read_point_cloud(sample_data.point_cloud_path)
# Flip it, otherwise the pointcloud will be upside down
pcd_flipped.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0],
[0, 0, 0, 1]])
print("4. Customized visualization showing normal rendering")
custom_draw_geometry_load_option(pcd_flipped,
sample_data.render_option_path)
5. 键盘控制
def custom_draw_geometry_with_key_callback(pcd, render_option_path):
"""键盘控制"""
def change_background_to_black(vis):
opt = vis.get_render_option()
opt.background_color = np.asarray([0, 0, 0])
return False
def load_render_option(vis):
vis.get_render_option().load_from_json(render_option_path)
return False
def capture_depth(vis):
depth = vis.capture_depth_float_buffer()
plt.imshow(np.asarray(depth))
plt.show()
return False
def capture_image(vis):
image = vis.capture_screen_float_buffer()
plt.imshow(np.asarray(image))
plt.show()
return False
key_to_callback = {}
key_to_callback[ord("K")] = change_background_to_black
key_to_callback[ord("R")] = load_render_option
key_to_callback[ord(",")] = capture_depth
key_to_callback[ord(".")] = capture_image
o3d.visualization.draw_geometries_with_key_callbacks([pcd], key_to_callback)
if __name__ == "__main__":
# 1. load pcd
sample_data = o3d.data.DemoCustomVisualization()
pcd_flipped = o3d.io.read_point_cloud(sample_data.point_cloud_path)
# Flip it, otherwise the pointcloud will be upside down
pcd_flipped.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0],
[0, 0, 0, 1]])
print("5. Customized visualization with key press callbacks")
print(" Press 'K' to change background color to black")
print(" Press 'R' to load a customized render option, showing normals")
print(" Press ',' to capture the depth buffer and show it")
print(" Press '.' to capture the screen and show it")
custom_draw_geometry_with_key_callback(pcd_flipped,
sample_data.render_option_path)