?2021SC@SDUSC
通过draw_geometries()和draw_geometries_with_custom_animation()函数可以很方便的使用Open3d的可视化功能,所有的都可以通过GUI去完成。
import open3d as o3d
def custom_draw_geometry(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()
pcd = o3d.io.read_point_cloud("fragment.ply")
print("Customized visualization to mimic DrawGeometry")
custom_draw_geometry(pcd)
?通过Visualizer类模仿draw_geometries()功能,产生的功能与draw_geometries()完全相同。?用来实现对几何对象的渲染可视化。在可视化界面能够通过鼠标进行缩放,旋转和平移,改变渲染风格和屏幕截图等。
import open3d as o3d
def custom_draw_geometry_load_option(pcd):
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd)
vis.get_render_option().load_from_json("renderoption.json")
vis.run()
vis.destroy_window()
pcd = o3d.io.read_point_cloud("fragment.ply")
print("Customized visualization showing normal rendering")
custom_draw_geometry_load_option(pcd)
以上函数读取储存在json文件中预定义的RenderOption。Visualizer类中还有其他一些变量比如viewControl等。
?
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)
??draw_geometries_with_animation_callback注册一个Python回调函数notate_view作为主循环的空闲函数。当可视化器空闲时,它沿着x轴旋转视图。其控制了一个旋转的动画行为,展示给人的效果是三百六十度无死角旋转以达到好的用户体验。
import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt
def custom_draw_geometry_with_key_callback(pcd):
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(
"renderoption.json")
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)
pcd = o3d.io.read_point_cloud("侍女1.pcd")
print("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)
?当给程序一个按键时,也可以生成回调函数。上面的函数就注册了几个按键。比如按k将背景颜色更改为黑色。
控制改变视角
import open3d as o3d
def custom_draw_geometry_with_custom_fov(pcd, fov_step):
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()
pcd = o3d.io.read_point_cloud("fragment.ply")
print("Changing field of view")
custom_draw_geometry_with_custom_fov(pcd, 90.0)
custom_draw_geometry_with_custom_fov(pcd, -90.0)
要去改变我们观看的视角,必须先获取可视化控件的实例。使用change_field_of_view 函数改变视角。?
?视场(FoV)可以在范围[5,90]内设置一个度。注意函数change_field_of_view()将指定的FoV添加到当前FoV中。默认情况下,可视化器的视场为60度。调用以下代码:
custom_draw_geometry_with_custom_fov(pcd, 90.0)
custom_draw_geometry_with_custom_fov(pcd, -10.0)
FoV默认最大为60度, 当设置超过最大的FoV时,FoV将被设置为90度;设置的FoV低于最小的FoV时,FoV将被设置为5度。
?
捕获图像相关操作
import os
import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt
def custom_draw_geometry_with_camera_trajectory(pcd):
custom_draw_geometry_with_camera_trajectory.index = -1
custom_draw_geometry_with_camera_trajectory.trajectory =\
o3d.io.read_pinhole_camera_trajectory(
"../../test_data/camera_trajectory.json")
custom_draw_geometry_with_camera_trajectory.vis = o3d.visualization.Visualizer(
)
if not os.path.exists("../../test_data/image/"):
os.makedirs("../../test_data/image/")
if not os.path.exists("../../test_data/depth/"):
os.makedirs("../../test_data/depth/")
def move_forward(vis):
# This function is called within the o3d.visualization.Visualizer::run() loop
# The run loop calls the function, then re-render
# So the sequence in this function is to:
# 1. Capture frame
# 2. index++, check ending criteria
# 3. Set camera
# 4. (Re-render)
ctr = vis.get_view_control()
glb = custom_draw_geometry_with_camera_trajectory
if glb.index >= 0:
print("Capture image {:05d}".format(glb.index))
depth = vis.capture_depth_float_buffer(False)
image = vis.capture_screen_float_buffer(False)
plt.imsave("../../test_data/depth/{:05d}.png".format(glb.index),\
np.asarray(depth), dpi = 1)
plt.imsave("../../test_data/image/{:05d}.png".format(glb.index),\
np.asarray(image), dpi = 1)
# vis.capture_depth_image("depth/{:05d}.png".format(glb.index), False)
# vis.capture_screen_image("image/{:05d}.png".format(glb.index), False)
glb.index = glb.index + 1
if glb.index < len(glb.trajectory.parameters):
ctr.convert_from_pinhole_camera_parameters(
glb.trajectory.parameters[glb.index])
else:
custom_draw_geometry_with_camera_trajectory.vis.\
register_animation_callback(None)
return False
vis = custom_draw_geometry_with_camera_trajectory.vis
vis.create_window()
vis.add_geometry(pcd)
vis.get_render_option().load_from_json("renderoption.json")
vis.register_animation_callback(move_forward)
vis.run()
vis.destroy_window()
pcd = o3d.io.read_point_cloud("fragment.ply")
print("6. Customized visualization playing a camera trajectory")
custom_draw_geometry_with_camera_trajectory(pcd)
这个函数读取了相机轨迹,然后自定义了动画函数move_forward 以遍历相机轨迹。在这个函数中,彩色图像和深度图像分别使用visualizer.capture_depth_float_buffer和Visualizer.capture_screen_float_buffer去捕获,然后保存成文件。
|