Register fragments

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

Input arguments

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
23
24
# examples/Python/Tutorial/ReconstructionSystem/register_fragments.py

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.

Compute initial registration

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# examples/Python/Tutorial/ReconstructionSystem/register_fragments.py

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(join(path_dataset,
                config["template_fragment_posegraph_optimized"] % s))
        n_nodes = len(pose_graph_frag.nodes)
        transformation_init = np.linalg.inv(
                pose_graph_frag.nodes[n_nodes-1].pose)
        (transformation, information) = \
                multiscale_icp(source_down, target_down,
                [config["voxel_size"]], [50], config, transformation_init)
    else: # loop closure case
        (success, transformation, information) = register_point_cloud_fpfh(
                source_down, target_down,
                source_fpfh, target_fpfh, config)
        if not success:
            print("No resonable solution. Skip this pair")
            return (False, np.identity(4), np.zeros((6,6)))
    print(transformation)

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

This function computes a rough alignment between two fragments. 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].

Pairwise global registration

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# examples/Python/Tutorial/ReconstructionSystem/register_fragments.py

def register_point_cloud_fpfh(source, target,
        source_fpfh, target_fpfh, config):
    distance_threshold = config["voxel_size"] * 1.4
    if config["global_registration"] == "fgr":
        result = registration_fast_based_on_feature_matching(
                source, target, source_fpfh, target_fpfh,
                FastGlobalRegistrationOption(
                maximum_correspondence_distance = distance_threshold))
    if config["global_registration"] == "ransac":
        result = registration_ransac_based_on_feature_matching(
                source, target, source_fpfh, target_fpfh,
                distance_threshold,
                TransformationEstimationPointToPoint(False), 4,
                [CorrespondenceCheckerBasedOnEdgeLength(0.9),
                CorrespondenceCheckerBasedOnDistance(distance_threshold)],
                RANSACConvergenceCriteria(4000000, 500))
    if (result.transformation.trace() == 4.0):
        return (False, np.identity(4), np.zeros((6,6)))
    information = get_information_matrix_from_point_clouds(
            source, target, distance_threshold, result.transformation)
    if information[5,5] / min(len(source.points),len(target.points)) < 0.3:
        return (False, np.identity(4), np.zeros((6,6)))
    return (True, result.transformation, information)

This function uses RANSAC or Fast global registration for pairwise global registration.

Multiway registration

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# examples/Python/Tutorial/ReconstructionSystem/register_fragments.py

def update_posegrph_for_scene(s, t, transformation, information,
        odometry, pose_graph):
    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.

39
40
41
42
43
44
45
46
47
48
# examples/Python/Tutorial/ReconstructionSystem/optimize_posegraph.py

def optimize_posegraph_for_scene(path_dataset, config):
    pose_graph_name = join(path_dataset, config["template_global_posegraph"])
    pose_graph_optimized_name = join(path_dataset,
            config["template_global_posegraph_optimized"])
    run_posegraph_optimization(pose_graph_name, pose_graph_optimized_name,
            max_correspondence_distance = config["voxel_size"] * 1.4,
            preference_loop_closure = \
            config["preference_loop_closure_registration"])

Main registration loop

The function make_posegraph_for_scene below calls all the functions introduced above.

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# examples/Python/Tutorial/ReconstructionSystem/register_fragments.py

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

    n_files = len(ply_file_names)
    matching_results = {}
    for s in range(n_files):
        for t in range(s + 1, n_files):
            matching_results[s * n_files + t] = matching_result(s, t)

    if config["python_multi_threading"]:
        from joblib import Parallel, delayed
        import multiprocessing
        import subprocess
        MAX_THREAD = min(multiprocessing.cpu_count(),
                max(len(matching_results), 1))
        results = Parallel(n_jobs=MAX_THREAD)(
                delayed(register_point_cloud_pair)(ply_file_names,
                matching_results[r].s, matching_results[r].t, config)
                for r in matching_results)
        for i, r in enumerate(matching_results):
            matching_results[r].success = results[i][0]
            matching_results[r].transformation = results[i][1]
            matching_results[r].information = results[i][2]
    else:
        for r in matching_results:
            (matching_results[r].success, matching_results[r].transformation,
                    matching_results[r].information) = \
                    register_point_cloud_pair(ply_file_names,
                    matching_results[r].s, matching_results[r].t, config)

    for r in matching_results:
        if matching_results[r].success:
            (odometry, pose_graph) = update_posegrph_for_scene(
                    matching_results[r].s, matching_results[r].t,
                    matching_results[r].transformation,
                    matching_results[r].information,
                    odometry, pose_graph)
    write_pose_graph(join(config["path_dataset"],
            config["template_global_posegraph"]), pose_graph)

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

Results

The following is messages from pose graph optimization.

[GlobalOptimizationLM] Optimizing PoseGraph having 14 nodes and 42 edges.
Line process weight : 55.885667
[Initial     ] residual : 7.791139e+04, lambda : 1.205976e+00
[Iteration 00] residual : 6.094275e+02, valid edges : 22, time : 0.001 sec.
[Iteration 01] residual : 4.526879e+02, valid edges : 22, time : 0.000 sec.
[Iteration 02] residual : 4.515039e+02, valid edges : 22, time : 0.000 sec.
[Iteration 03] residual : 4.514832e+02, valid edges : 22, time : 0.000 sec.
[Iteration 04] residual : 4.514825e+02, valid edges : 22, time : 0.000 sec.
Current_residual - new_residual < 1.000000e-06 * current_residual
[GlobalOptimizationLM] total time : 0.003 sec.
[GlobalOptimizationLM] Optimizing PoseGraph having 14 nodes and 35 edges.
Line process weight : 60.762800
[Initial     ] residual : 6.336097e+01, lambda : 1.324043e+00
[Iteration 00] residual : 6.334147e+01, valid edges : 22, time : 0.000 sec.
[Iteration 01] residual : 6.334138e+01, valid edges : 22, time : 0.000 sec.
Current_residual - new_residual < 1.000000e-06 * current_residual
[GlobalOptimizationLM] total time : 0.001 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.