Register fragments

Once the fragments of the scene are created, the next step is to align them in a global space.

Input arguments

190
191
192
193
194
195
196
197
def run(config):
    print("register fragments.")
    set_verbosity_level(VerbosityLevel.Debug)
    ply_file_names = get_file_list(os.path.join(
            config["path_dataset"], folder_fragment), ".ply")
    make_folder(os.path.join(config["path_dataset"], folder_scene))
    make_posegraph_for_scene(ply_file_names, config)
    optimize_posegraph_for_scene(config["path_dataset"], config)

This script runs with python run_system.py [config] --register. In [config], ["path_dataset"] should have subfolders fragments which stores fragments in .ply files and a pose graph in a .json file.

The main function runs make_posegraph_for_scene and optimize_posegraph_for_scene. The first function performs pairwise registration. The second function performs multiway registration.

Preprocess point cloud

15
16
17
18
19
20
21
22
def preprocess_point_cloud(pcd, config):
    voxel_size = config["voxel_size"]
    pcd_down = voxel_down_sample(pcd, voxel_size)
    estimate_normals(pcd_down,
            KDTreeSearchParamHybrid(radius = voxel_size * 2.0, max_nn = 30))
    pcd_fpfh = compute_fpfh_feature(pcd_down,
            KDTreeSearchParamHybrid(radius = voxel_size * 5.0, max_nn = 100))
    return (pcd_down, pcd_fpfh)

This function downsample point cloud to make a point cloud sparser and regularly distributed. Normals and FPFH feature are precomputed. See Voxel downsampling, Vertex normal estimation, and Extract geometric feature for more details.

Pairwise global registration

25
26
27
28
29
30
31
32
33
34
35
def register_point_cloud_fpfh(source, target,
        source_fpfh, target_fpfh, config):
    distance_threshold = config["voxel_size"] * 1.5
    result = registration_fast_based_on_feature_matching(
            source, target, source_fpfh, target_fpfh,
            FastGlobalRegistrationOption(
            maximum_correspondence_distance = config["voxel_size"] * 1.4))
    if (result.transformation.trace() == 4.0):
        return (False, np.identity(4))
    else:
        return (True, result)

This function uses RANSAC for pairwise global registration.

Compute initial registration

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def compute_initial_registration(s, t, source_down, target_down,
        source_fpfh, target_fpfh, path_dataset, config):

    if t == s + 1: # odometry case
        print("Using RGBD odometry")
        pose_graph_frag = read_pose_graph(path_dataset +
                template_fragment_posegraph_optimized % s)
        n_nodes = len(pose_graph_frag.nodes)
        transformation = np.linalg.inv(
                pose_graph_frag.nodes[n_nodes-1].pose)
        print(transformation)
    else: # loop closure case
        print("register_point_cloud_fpfh")
        (success_ransac, result_ransac) = register_point_cloud_fpfh(
                source_down, target_down,
                source_fpfh, target_fpfh, config)
        if not success_ransac:
            print("No resonable solution. Skip this pair")
            return (False, np.identity(4))
        else:
            transformation = result_ransac.transformation
        print(transformation)

    if config["debug_mode"]:
        draw_registration_result(source_down, target_down,
                transformation)
    return (True, transformation)

This function computes a rough alignment between two fragments. The rough alignments are used to initialize ICP refinement. If the fragments are neighboring fragments, the rough alignment is determined by an aggregating RGBD odometry obtained from Make fragments. Otherwise, register_point_cloud_fpfh is called to perform global registration. Note that global registration is less reliable according to [Choi2015].

Fine-grained registration

 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def multiscale_icp(source, target, voxel_size, max_iter,
        config, init_transformation = np.identity(4)):
    current_transformation = init_transformation
    for scale in range(len(max_iter)): # multi-scale approach
        iter = max_iter[scale]
        print("voxel_size %f" % voxel_size[scale])
        source_down = voxel_down_sample(source, voxel_size[scale])
        target_down = voxel_down_sample(target, voxel_size[scale])
        estimate_normals(source_down, KDTreeSearchParamHybrid(
                radius = voxel_size[scale] * 2.0, max_nn = 30))
        estimate_normals(target_down, KDTreeSearchParamHybrid(
                radius = voxel_size[scale] * 2.0, max_nn = 30))
        if config["icp_method"] == "point_to_point":
            result_icp = registration_icp(source_down, target_down,
                    voxel_size[scale] * 1.4, current_transformation,
                    TransformationEstimationPointToPlane(),
                    ICPConvergenceCriteria(max_iteration = iter))
        else:
            # colored pointcloud registration
            # This is implementation of following paper
            # J. Park, Q.-Y. Zhou, V. Koltun,
            # Colored Point Cloud Registration Revisited, ICCV 2017
            result_icp = registration_colored_icp(source_down, target_down,
                    voxel_size[scale], current_transformation,
                    ICPConvergenceCriteria(relative_fitness = 1e-6,
                    relative_rmse = 1e-6, max_iteration = iter))
        current_transformation = result_icp.transformation

    maximum_correspondence_distance = config["voxel_size"] * 1.4
    information_matrix = get_information_matrix_from_point_clouds(
            source, target, maximum_correspondence_distance,
            result_icp.transformation)
    if config["debug_mode"]:
        draw_registration_result_original_color(source, target,
                result_icp.transformation)
    return (result_icp.transformation, information_matrix)


def local_refinement(s, t, source, target, transformation_init, config):
    voxel_size = config["voxel_size"]
    if t == s + 1: # odometry case
        print("register_point_cloud_icp")
        (transformation, information) = \
                multiscale_icp(
                source, target, [voxel_size / 4.0], [30],
                # source, target, [voxel_size], [30],
                config, transformation_init)
    else: # loop closure case
        print("register_colored_point_cloud")
        (transformation, information) = \
                multiscale_icp(
                source, target,
                [voxel_size, voxel_size/2.0, voxel_size/4.0], [50, 30, 14],
                # [voxel_size], [30],
                config, transformation_init)

    success_local = False
    if information[5,5] / min(len(source.points),len(target.points)) > 0.3:
        success_local = True
    if config["debug_mode"]:
        draw_registration_result_original_color(
                source, target, transformation)
    return (success_local, transformation, information)

Two options are given for the fine-grained registration. The registration_colored_icp is recommended since it uses color information to prevent drift. Details see [Park2017].

Multiway registration

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def update_posegrph_for_scene(s, t, transformation, information,
        odometry, pose_graph):
    print("Update PoseGraph")
    if t == s + 1: # odometry case
        odometry = np.dot(transformation, odometry)
        odometry_inv = np.linalg.inv(odometry)
        pose_graph.nodes.append(PoseGraphNode(odometry_inv))
        pose_graph.edges.append(
                PoseGraphEdge(s, t, transformation,
                information, uncertain = False))
    else: # loop closure case
        pose_graph.edges.append(
                PoseGraphEdge(s, t, transformation,
                information, uncertain = True))
    return (odometry, pose_graph)

This script uses the technique demonstrated in Multiway registration. Function update_posegrph_for_scene builds a pose graph for multiway registration of all fragments. Each graph node represents a fragments and its pose which transforms the geometry to the global space.

Once a pose graph is built, function optimize_posegraph_for_scene is called for multiway registration.

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def run_posegraph_optimization(pose_graph_name, pose_graph_optmized_name,
        max_correspondence_distance, preference_loop_closure):
    # to display messages from global_optimization
    set_verbosity_level(VerbosityLevel.Debug)
    method = GlobalOptimizationLevenbergMarquardt()
    criteria = GlobalOptimizationConvergenceCriteria()
    option = GlobalOptimizationOption(
            max_correspondence_distance = max_correspondence_distance,
            edge_prune_threshold = 0.25,
            preference_loop_closure = preference_loop_closure,
            reference_node = 0)
    pose_graph = read_pose_graph(pose_graph_name)
    global_optimization(pose_graph, method, criteria, option)
    write_pose_graph(pose_graph_optmized_name, pose_graph)
    set_verbosity_level(VerbosityLevel.Error)
39
40
41
42
43
44
45
def optimize_posegraph_for_scene(path_dataset, config):
    pose_graph_name = os.path.join(path_dataset, template_global_posegraph)
    pose_graph_optmized_name = os.path.join(path_dataset,
            template_global_posegraph_optimized)
    run_posegraph_optimization(pose_graph_name, pose_graph_optmized_name,
            max_correspondence_distance = config["voxel_size"] * 1.4,
            preference_loop_closure = 2.0)

Main registration loop

The function make_posegraph_for_scene below calls all the functions introduced above.

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def register_point_cloud_pair(ply_file_names, s, t, config):
    print("reading %s ..." % ply_file_names[s])
    source = read_point_cloud(ply_file_names[s])
    print("reading %s ..." % ply_file_names[t])
    target = read_point_cloud(ply_file_names[t])
    (source_down, source_fpfh) = preprocess_point_cloud(source, config)
    (target_down, target_fpfh) = preprocess_point_cloud(target, config)
    (success_global, transformation_init) = \
            compute_initial_registration(
            s, t, source_down, target_down,
            source_fpfh, target_fpfh, config["path_dataset"], config)
    if t != s + 1 and not success_global:
        return (False, np.identity(4), np.identity(6))
    (success_local, transformation_icp, information_icp) = \
            local_refinement(s, t, source, target,
            transformation_init, config)
    if t != s + 1 and not success_local:
        return (False, np.identity(4), np.identity(6))
    return (True, transformation_icp, information_icp)


def make_posegraph_for_scene(ply_file_names, config):
    pose_graph = PoseGraph()
    odometry = np.identity(4)
    pose_graph.nodes.append(PoseGraphNode(odometry))
    info = np.identity(6)

    n_files = len(ply_file_names)
    for s in range(n_files):
        for t in range(s + 1, n_files):
            (success, transformation_icp, information_icp) = \
                    register_point_cloud_pair(ply_file_names, s, t, config)
            if success:
                (odometry, pose_graph) = update_posegrph_for_scene(s, t,
                        transformation_icp, information_icp,
                        odometry, pose_graph)
                print(pose_graph)
    write_pose_graph(os.path.join(config["path_dataset"],
            template_global_posegraph), pose_graph)

The main workflow is: pairwise global registration -> local refinement -> multiway registration.

Results

The following is messages from pose graph optimization.

PoseGraph with 14 nodes and 52 edges.
[GlobalOptimizationLM] Optimizing PoseGraph having 14 nodes and 52 edges.
Line process weight : 49.899808
[Initial     ] residual : 1.307073e+06, lambda : 8.415505e+00
[Iteration 00] residual : 1.164909e+03, valid edges : 31, time : 0.000 sec.
[Iteration 01] residual : 1.026223e+03, valid edges : 34, time : 0.000 sec.
[Iteration 02] residual : 9.263710e+02, valid edges : 41, time : 0.000 sec.
[Iteration 03] residual : 8.434943e+02, valid edges : 40, time : 0.000 sec.
:
[Iteration 22] residual : 8.002788e+02, valid edges : 41, time : 0.000 sec.
Current_residual - new_residual < 1.000000e-06 * current_residual
[GlobalOptimizationLM] total time : 0.006 sec.
[GlobalOptimizationLM] Optimizing PoseGraph having 14 nodes and 41 edges.
Line process weight : 52.121020
[Initial     ] residual : 3.490871e+02, lambda : 1.198591e+01
[Iteration 00] residual : 3.409909e+02, valid edges : 40, time : 0.000 sec.
[Iteration 01] residual : 3.393578e+02, valid edges : 40, time : 0.000 sec.
[Iteration 02] residual : 3.390909e+02, valid edges : 40, time : 0.000 sec.
[Iteration 03] residual : 3.390108e+02, valid edges : 40, time : 0.000 sec.
:
[Iteration 08] residual : 3.389679e+02, valid edges : 40, time : 0.000 sec.
Current_residual - new_residual < 1.000000e-06 * current_residual
[GlobalOptimizationLM] total time : 0.002 sec.
CompensateReferencePoseGraphNode : reference : 0

There are 14 fragments and 52 valid matching pairs between fragments. After 23 iteration, 11 edges are detected to be false positive. After they are pruned, pose graph optimization runs again to achieve tight alignment.