2 GTSAM Copyright 2010-2018, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
5 Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 See LICENSE for the license information
9 This example shows how 1dsfm uses outlier rejection (MFAS) and optimization (translation recovery)
10 together for estimating global translations from relative translation directions and global rotations.
11 The purpose of this example is to illustrate the connection between these two classes using a small SfM dataset.
13 Author: Akshay Krishnan
17 from collections
import defaultdict
18 from typing
import List, Tuple
25 MAX_1DSFM_PROJECTION_DIRECTIONS = 48
26 OUTLIER_WEIGHT_THRESHOLD = 0.1
29 def get_data() -> Tuple[gtsam.Values, List[gtsam.BinaryMeasurementUnit3]]:
30 """"Returns global rotations and unit translation directions between 8 cameras
31 that lie on a circle and face the center. The poses of 8 cameras are obtained from SFMdata
32 and the unit translations directions between some camera pairs are computed from their
33 global translations. """
34 wTc_list = SFMdata.createPoses()
40 for i
in range(0,
len(wTc_list) - 2):
43 wRc_values.insert(i, wRi)
45 for j
in range(i + 1, i + 3):
49 i_itj = wRi.unrotate(w_itj)
52 i_iZj_list.append(gtsam.BinaryMeasurementUnit3(
55 wRc_values.insert(
len(wTc_list) - 1, wTc_list[-1].
rotation())
56 wRc_values.insert(
len(wTc_list) - 2, wTc_list[-2].
rotation())
57 return wRc_values, i_iZj_list
60 def filter_outliers(w_iZj_list: List[gtsam.BinaryMeasurementUnit3]) -> List[gtsam.BinaryMeasurementUnit3]:
61 """Removes outliers from a list of Unit3 measurements that are the
62 translation directions from camera i to camera j in the world frame."""
66 num_directions_to_sample =
min(
67 MAX_1DSFM_PROJECTION_DIRECTIONS,
len(w_iZj_list))
68 sampled_indices = np.random.choice(
69 len(w_iZj_list), num_directions_to_sample, replace=
False)
72 projection_directions = [w_iZj_list[idx].
measured()
73 for idx
in sampled_indices]
77 for direction
in projection_directions:
79 outlier_weights.append(algorithm.computeOutlierWeights())
84 avg_outlier_weights = defaultdict(float)
85 for outlier_weight_dict
in outlier_weights:
86 for keypair, weight
in outlier_weight_dict.items():
87 avg_outlier_weights[keypair] += weight /
len(outlier_weights)
91 [w_iZj_inliers.append(w_iZj)
for w_iZj
in w_iZj_list
if avg_outlier_weights[(
92 w_iZj.key1(), w_iZj.key2())] < OUTLIER_WEIGHT_THRESHOLD]
99 """Estimate poses given rotations and normalized translation directions between cameras.
102 i_iZj_list: List of normalized translation direction measurements between camera pairs,
103 Z here refers to measurements. The measurements are of camera j with reference
104 to camera i (iZj), in camera i's coordinate frame (i_). iZj represents a unit
105 vector to j in i's frame and is not a transformation.
106 wRc_values: Rotations of the cameras in the world frame.
109 gtsam.Values: Estimated poses.
114 for i_iZj
in i_iZj_list:
115 w_iZj =
gtsam.Unit3(wRc_values.atRot3(i_iZj.key1())
117 w_iZj_list.append(gtsam.BinaryMeasurementUnit3(
118 i_iZj.key1(), i_iZj.key2(), w_iZj, i_iZj.noiseModel()))
127 for key
in wRc_values.keys():
129 wRc_values.atRot3(key), wtc_values.atPoint3(key)))
136 print(
"**** Translation averaging output ****")
138 print(
"**************************************")
141 if __name__ ==
'__main__':