SFMExample_bal.py
Go to the documentation of this file.
1 """
2  GTSAM Copyright 2010, Georgia Tech Research Corporation,
3  Atlanta, Georgia 30332-0415
4  All Rights Reserved
5  Authors: Frank Dellaert, et al. (see THANKS for the full author list)
6 
7  See LICENSE for the license information
8 
9  Solve a structure-from-motion problem from a "Bundle Adjustment in the Large" file
10  Author: Frank Dellaert (Python: Akshay Krishnan, John Lambert)
11 """
12 
13 import argparse
14 import logging
15 import sys
16 
17 import matplotlib.pyplot as plt
18 import numpy as np
19 
20 import gtsam
21 from gtsam import (
22  GeneralSFMFactorCal3Bundler,
23  PinholeCameraCal3Bundler,
24  PriorFactorPinholeCameraCal3Bundler,
25  readBal,
26  symbol_shorthand
27 )
28 
29 C = symbol_shorthand.C
30 P = symbol_shorthand.P
31 
32 
33 logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
34 
35 def run(args):
36  """ Run LM optimization with BAL input data and report resulting error """
37  input_file = gtsam.findExampleDataFile(args.input_file)
38 
39  # Load the SfM data from file
40  scene_data = readBal(input_file)
41  logging.info(f"read {scene_data.number_tracks()} tracks on {scene_data.number_cameras()} cameras\n")
42 
43  # Create a factor graph
45 
46  # We share *one* noiseModel between all projection factors
47  noise = gtsam.noiseModel.Isotropic.Sigma(2, 1.0) # one pixel in u and v
48 
49  # Add measurements to the factor graph
50  j = 0
51  for t_idx in range(scene_data.number_tracks()):
52  track = scene_data.track(t_idx) # SfmTrack
53  # retrieve the SfmMeasurement objects
54  for m_idx in range(track.number_measurements()):
55  # i represents the camera index, and uv is the 2d measurement
56  i, uv = track.measurement(m_idx)
57  # note use of shorthand symbols C and P
58  graph.add(GeneralSFMFactorCal3Bundler(uv, noise, C(i), P(j)))
59  j += 1
60 
61  # Add a prior on pose x1. This indirectly specifies where the origin is.
62  graph.push_back(
63  gtsam.PriorFactorPinholeCameraCal3Bundler(
64  C(0), scene_data.camera(0), gtsam.noiseModel.Isotropic.Sigma(9, 0.1)
65  )
66  )
67  # Also add a prior on the position of the first landmark to fix the scale
68  graph.push_back(
69  gtsam.PriorFactorPoint3(
70  P(0), scene_data.track(0).point3(), gtsam.noiseModel.Isotropic.Sigma(3, 0.1)
71  )
72  )
73 
74  # Create initial estimate
75  initial = gtsam.Values()
76 
77  i = 0
78  # add each PinholeCameraCal3Bundler
79  for cam_idx in range(scene_data.number_cameras()):
80  camera = scene_data.camera(cam_idx)
81  initial.insert(C(i), camera)
82  i += 1
83 
84  j = 0
85  # add each SfmTrack
86  for t_idx in range(scene_data.number_tracks()):
87  track = scene_data.track(t_idx)
88  initial.insert(P(j), track.point3())
89  j += 1
90 
91  # Optimize the graph and print results
92  try:
94  params.setVerbosityLM("ERROR")
95  lm = gtsam.LevenbergMarquardtOptimizer(graph, initial, params)
96  result = lm.optimize()
97  except Exception as e:
98  logging.exception("LM Optimization failed")
99  return
100  # Error drops from ~2764.22 to ~0.046
101  logging.info(f"final error: {graph.error(result)}")
102 
103 
104 if __name__ == "__main__":
105  parser = argparse.ArgumentParser()
106  parser.add_argument(
107  '-i',
108  '--input_file',
109  type=str,
110  default="dubrovnik-3-7-pre",
111  help='Read SFM data from the specified BAL file'
112  'The data format is described here: https://grail.cs.washington.edu/projects/bal/.'
113  'BAL files contain (nrPoses, nrPoints, nrObservations), followed by (i,j,u,v) tuples, '
114  'then (wx,wy,wz,tx,ty,tz,f,k1,k1) as Bundler camera calibrations w/ Rodrigues vector'
115  'and (x,y,z) 3d point initializations.'
116  )
117  run(parser.parse_args())
118 
SfmData readBal(const string &filename)
This function parses a "Bundle Adjustment in the Large" (BAL) file and returns the data as a SfmData ...
Definition: dataset.cpp:1133
static const Point3 point3(0.08, 0.08, 0.0)
static shared_ptr Sigma(size_t dim, double sigma, bool smart=true)
Definition: NoiseModel.cpp:567
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)
Definition: dataset.cpp:65


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:44:01