Color Map Optimization
Consider color mapping to the geometry reconstructed from depth cameras. As color and depth frames are not perfectly aligned, the texture mapping using color images is subject to results in blurred color map. Open3D provides color map optimization method proposed by [Zhou2014]. Before begin, download fountain dataset from here. The following script shows an example of color map optimization.
5# examples/Python/Advanced/o3d.color_map.color_map_optimization.py
6
7import open3d as o3d
8from trajectory_io import *
9import os, sys
10sys.path.append("../Utility")
11from file import *
12
13path = "[path_to_fountain_dataset]"
14debug_mode = False
15
16if __name__ == "__main__":
17 o3d.utility.set_verbosity_level(o3d.utility.VerbosityLevel.Debug)
18
19 # Read RGBD images
20 rgbd_images = []
21 depth_image_path = get_file_list(os.path.join(path, "depth/"),
22 extension=".png")
23 color_image_path = get_file_list(os.path.join(path, "image/"),
24 extension=".jpg")
25 assert (len(depth_image_path) == len(color_image_path))
26 for i in range(len(depth_image_path)):
27 depth = o3d.io.read_image(os.path.join(depth_image_path[i]))
28 color = o3d.io.read_image(os.path.join(color_image_path[i]))
29 rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
30 color, depth, convert_rgb_to_intensity=False)
31 if debug_mode:
32 pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
33 rgbd_image,
34 o3d.camera.PinholeCameraIntrinsic(
35 o3d.camera.PinholeCameraIntrinsicParameters.
36 PrimeSenseDefault))
37 o3d.visualization.draw_geometries([pcd])
38 rgbd_images.append(rgbd_image)
39
40 # Read camera pose and mesh
41 camera = o3d.io.read_pinhole_camera_trajectory(
42 os.path.join(path, "scene/key.log"))
43 mesh = o3d.io.read_triangle_mesh(
44 os.path.join(path, "scene", "integrated.ply"))
45
46 # Before full optimization, let's just visualize texture map
47 # with given geometry, RGBD images, and camera poses.
48 option = o3d.color_map.ColorMapOptimizationOption()
49 option.maximum_iteration = 0
50 o3d.color_map.color_map_optimization(mesh, rgbd_images, camera, option)
51 o3d.visualization.draw_geometries([mesh])
52 o3d.io.write_triangle_mesh(
53 os.path.join(path, "scene", "color_map_before_optimization.ply"), mesh)
54
55 # Optimize texture and save the mesh as texture_mapped.ply
56 # This is implementation of following paper
57 # Q.-Y. Zhou and V. Koltun,
58 # Color Map Optimization for 3D Reconstruction with Consumer Depth Cameras,
59 # SIGGRAPH 2014
60 option.maximum_iteration = 300
61 option.non_rigid_camera_coordinate = True
62 o3d.color_map.color_map_optimization(mesh, rgbd_images, camera, option)
63 o3d.visualization.draw_geometries([mesh])
64 o3d.io.write_triangle_mesh(
65 os.path.join(path, "scene", "color_map_after_optimization.ply"), mesh)
Input
19 # Read RGBD images
20 rgbd_images = []
21 depth_image_path = get_file_list(os.path.join(path, "depth/"),
22 extension=".png")
23 color_image_path = get_file_list(os.path.join(path, "image/"),
24 extension=".jpg")
25 assert (len(depth_image_path) == len(color_image_path))
26 for i in range(len(depth_image_path)):
27 depth = o3d.io.read_image(os.path.join(depth_image_path[i]))
28 color = o3d.io.read_image(os.path.join(color_image_path[i]))
29 rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
30 color, depth, convert_rgb_to_intensity=False)
31 if debug_mode:
32 pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
33 rgbd_image,
34 o3d.camera.PinholeCameraIntrinsic(
35 o3d.camera.PinholeCameraIntrinsicParameters.
36 PrimeSenseDefault))
37 o3d.visualization.draw_geometries([pcd])
38 rgbd_images.append(rgbd_image)
This script reads color and depth image pairs and makes rgbd_image
. Note that convert_rgb_to_intensity
flag is False
. This is to preserve 8-bit color channels instead of using single channel float type image.
It is always good practice to visualize RGBD image before applying it to color map optimization. debug_mode
switch is for visualizing RGBD image.
40 # Read camera pose and mesh
41 camera = o3d.io.read_pinhole_camera_trajectory(
42 os.path.join(path, "scene/key.log"))
43 mesh = o3d.io.read_triangle_mesh(
44 os.path.join(path, "scene", "integrated.ply"))
The script reads camera trajectory and mesh.
46 # Before full optimization, let's just visualize texture map
47 # with given geometry, RGBD images, and camera poses.
48 option = o3d.color_map.ColorMapOptimizationOption()
49 option.maximum_iteration = 0
50 o3d.color_map.color_map_optimization(mesh, rgbd_images, camera, option)
51 o3d.visualization.draw_geometries([mesh])
52 o3d.io.write_triangle_mesh(
53 os.path.join(path, "scene", "color_map_before_optimization.ply"), mesh)
To visualize how the camera poses are not good for color mapping, this script intentionally set the iteration number as 0, which means no optimization. color_map_optimization
paints a mesh using corresponding RGBD images and camera poses. Without optimization, the texture map is blurred.


Rigid Optimization
The next step is to optimize camera poses to get a sharp color map.
55 # Optimize texture and save the mesh as texture_mapped.ply
56 # This is implementation of following paper
57 # Q.-Y. Zhou and V. Koltun,
58 # Color Map Optimization for 3D Reconstruction with Consumer Depth Cameras,
59 # SIGGRAPH 2014
60 option.maximum_iteration = 300
61 option.non_rigid_camera_coordinate = True
62 o3d.color_map.color_map_optimization(mesh, rgbd_images, camera, option)
63 o3d.visualization.draw_geometries([mesh])
64 o3d.io.write_triangle_mesh(
65 os.path.join(path, "scene", "color_map_after_optimization.ply"), mesh)
The script sets maximum_iteration = 300
for actual iterations. The optimization displays the following energy profile.
[ColorMapOptimization] :: Rigid Optimization
[Iteration 0001] Residual error : 21639.276499 (avg : 0.004615)
[Iteration 0002] Residual error : 21461.765357 (avg : 0.004577)
[Iteration 0003] Residual error : 21284.579715 (avg : 0.004540)
:
[Iteration 0298] Residual error : 8891.042884 (avg : 0.001903)
[Iteration 0299] Residual error : 8890.037077 (avg : 0.001903)
[Iteration 0300] Residual error : 8888.970765 (avg : 0.001903)
Residual error implies inconsistency of image intensities. Lower residual leads better color map quality. By default, ColorMapOptimizationOption
enables rigid optimization. It optimizes 6-dimentional pose of every cameras.


Non-rigid Optimization
For better alignment quality, there is an option for non-rigid optimization. To enable, simply add
option.non_rigid_camera_coordinate = True
before calling color_map_optimization
. Besides 6-dimentional camera poses, non-rigid optimization even consider local image warping represented by anchor points. This adds even more flexibility and leads higher quality color mapping. The residual error is smaller than the case of rigid optimization.
[ColorMapOptimization] :: Non-Rigid Optimization
[Iteration 0001] Residual error : 21639.276499, reg : 0.000000
[Iteration 0002] Residual error : 21187.225206, reg : 13.918495
[Iteration 0003] Residual error : 20745.248996, reg : 42.234724
:
[Iteration 0298] Residual error : 5589.018747, reg : 2745.364742
[Iteration 0299] Residual error : 5587.180145, reg : 2746.619137
[Iteration 0300] Residual error : 5585.066255, reg : 2747.902979
Results of non-rigid optimization follow.


Note
If the residual error does not stably decrease, it is mainly because images are being bended abruptly. In this case, consider making iteration more conservative by increasing option.non_rigid_anchor_point_weight
.