Customized visualization
The usage of Open3D convenient visualization functions draw_geometries
and draw_geometries_with_custom_animation
is straightforward. Everything can be done with the GUI. Press h inside the visualizer window to see helper information. For more details, see /tutorial/visualization/visualization.ipynb
.
This tutorial focuses on more advanced functionalities to customize the behavior of the visualizer window. Please refer to examples/python/visualization/customized_visualization.py to try the following examples.
Mimic draw_geometries() with Visualizer class
35def custom_draw_geometry(pcd):
36 # The following code achieves the same effect as:
37 # o3d.visualization.draw_geometries([pcd])
38 vis = o3d.visualization.Visualizer()
39 vis.create_window()
40 vis.add_geometry(pcd)
41 vis.run()
42 vis.destroy_window()
This function produces exactly the same functionality as the convenience function draw_geometries
.

Class Visualizer
has a couple of variables such as a ViewControl
and a RenderOption
. The following function reads a predefined RenderOption
stored in a json file.
68def custom_draw_geometry_load_option(pcd):
69 vis = o3d.visualization.Visualizer()
70 vis.create_window()
71 vis.add_geometry(pcd)
72 vis.get_render_option().load_from_json("../../test_data/renderoption.json")
73 vis.run()
74 vis.destroy_window()
Outputs:

Change field of view
To change field of view of the camera, it is first necessary to get an instance of the visualizer control. To modify the field of view, use change_field_of_view
.
45def custom_draw_geometry_with_custom_fov(pcd, fov_step):
46 vis = o3d.visualization.Visualizer()
47 vis.create_window()
48 vis.add_geometry(pcd)
49 ctr = vis.get_view_control()
50 print("Field of view (before changing) %.2f" % ctr.get_field_of_view())
51 ctr.change_field_of_view(step=fov_step)
52 print("Field of view (after changing) %.2f" % ctr.get_field_of_view())
53 vis.run()
54 vis.destroy_window()
The field of view (FoV) can be set to a degree in the range [5,90]. Note that change_field_of_view
adds the specified FoV to the current FoV. By default, the visualizer has an FoV of 60 degrees. Calling the following code
custom_draw_geometry_with_custom_fov(pcd, 90.0)
will add the specified 90 degrees to the default 60 degrees. As it exceeds the maximum allowable FoV, the FoV is set to 90 degrees.

The following code
custom_draw_geometry_with_custom_fov(pcd, -90.0)
will set FoV to 5 degrees, because 60 - 90 = -30 is less than 5 degrees.

Callback functions
57def custom_draw_geometry_with_rotation(pcd):
58
59 def rotate_view(vis):
60 ctr = vis.get_view_control()
61 ctr.rotate(10.0, 0.0)
62 return False
63
64 o3d.visualization.draw_geometries_with_animation_callback([pcd],
65 rotate_view)
Function draw_geometries_with_animation_callback
registers a Python callback function rotate_view
as the idle function of the main loop. It rotates the view along the x-axis whenever the visualizer is idle. This defines an animation behavior.

77def custom_draw_geometry_with_key_callback(pcd):
78
79 def change_background_to_black(vis):
80 opt = vis.get_render_option()
81 opt.background_color = np.asarray([0, 0, 0])
82 return False
83
84 def load_render_option(vis):
85 vis.get_render_option().load_from_json(
86 "../../test_data/renderoption.json")
87 return False
88
89 def capture_depth(vis):
90 depth = vis.capture_depth_float_buffer()
91 plt.imshow(np.asarray(depth))
92 plt.show()
93 return False
94
95 def capture_image(vis):
96 image = vis.capture_screen_float_buffer()
97 plt.imshow(np.asarray(image))
98 plt.show()
99 return False
100
101 key_to_callback = {}
102 key_to_callback[ord("K")] = change_background_to_black
103 key_to_callback[ord("R")] = load_render_option
104 key_to_callback[ord(",")] = capture_depth
105 key_to_callback[ord(".")] = capture_image
106 o3d.visualization.draw_geometries_with_key_callbacks([pcd], key_to_callback)
Callback functions can also be registered upon key press event. This script registered four keys. For example, pressing k changes the background color to black.

Capture images in a customized animation
109def custom_draw_geometry_with_camera_trajectory(pcd):
110 custom_draw_geometry_with_camera_trajectory.index = -1
111 custom_draw_geometry_with_camera_trajectory.trajectory =\
112 o3d.io.read_pinhole_camera_trajectory(
113 "../../test_data/camera_trajectory.json")
114 custom_draw_geometry_with_camera_trajectory.vis = o3d.visualization.Visualizer(
115 )
116 if not os.path.exists("../../test_data/image/"):
117 os.makedirs("../../test_data/image/")
118 if not os.path.exists("../../test_data/depth/"):
119 os.makedirs("../../test_data/depth/")
120
121 def move_forward(vis):
122 # This function is called within the o3d.visualization.Visualizer::run() loop
123 # The run loop calls the function, then re-render
124 # So the sequence in this function is to:
125 # 1. Capture frame
126 # 2. index++, check ending criteria
127 # 3. Set camera
128 # 4. (Re-render)
129 ctr = vis.get_view_control()
130 glb = custom_draw_geometry_with_camera_trajectory
131 if glb.index >= 0:
132 print("Capture image {:05d}".format(glb.index))
133 depth = vis.capture_depth_float_buffer(False)
134 image = vis.capture_screen_float_buffer(False)
135 plt.imsave("../../test_data/depth/{:05d}.png".format(glb.index),\
136 np.asarray(depth), dpi = 1)
137 plt.imsave("../../test_data/image/{:05d}.png".format(glb.index),\
138 np.asarray(image), dpi = 1)
139 # vis.capture_depth_image("depth/{:05d}.png".format(glb.index), False)
140 # vis.capture_screen_image("image/{:05d}.png".format(glb.index), False)
141 glb.index = glb.index + 1
142 if glb.index < len(glb.trajectory.parameters):
143 ctr.convert_from_pinhole_camera_parameters(
144 glb.trajectory.parameters[glb.index], allow_arbitrary=True)
145 else:
146 custom_draw_geometry_with_camera_trajectory.vis.\
147 register_animation_callback(None)
148 return False
149
150 vis = custom_draw_geometry_with_camera_trajectory.vis
151 vis.create_window()
152 vis.add_geometry(pcd)
153 vis.get_render_option().load_from_json("../../test_data/renderoption.json")
154 vis.register_animation_callback(move_forward)
155 vis.run()
156 vis.destroy_window()
This function reads a camera trajectory, then defines an animation function move_forward
to travel through the camera trajectory. In this animation function, both color image and depth image are captured using Visualizer.capture_depth_float_buffer
and Visualizer.capture_screen_float_buffer
respectively. The images are saved as png files.
The captured image sequence:

The captured depth sequence:
