SmartProjectionFactorExample.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8 * See LICENSE for the license information
9 
10 * -------------------------------------------------------------------------- */
11 
30 #include <gtsam/slam/dataset.h>
32 #include <gtsam/nonlinear/Values.h>
37 #include <gtsam/inference/Symbol.h>
38 
39 #include <string>
40 #include <fstream>
41 #include <iostream>
42 
43 using namespace std;
44 using namespace gtsam;
45 
46 int main(int argc, char** argv){
48 
49  Values initial_estimate;
51  const noiseModel::Isotropic::shared_ptr model = noiseModel::Isotropic::Sigma(2,1);
52 
53  string calibration_loc = findExampleDataFile("VO_calibration.txt");
54  string pose_loc = findExampleDataFile("VO_camera_poses_large.txt");
55  string factor_loc = findExampleDataFile("VO_stereo_factors_large.txt");
56 
57  //read camera calibration info from file
58  // focal lengths fx, fy, skew s, principal point u0, v0, baseline b
59  cout << "Reading calibration info" << endl;
60  ifstream calibration_file(calibration_loc.c_str());
61 
62  double fx, fy, s, u0, v0, b;
63  calibration_file >> fx >> fy >> s >> u0 >> v0 >> b;
64  const Cal3_S2::shared_ptr K(new Cal3_S2(fx, fy, s, u0, v0));
65 
66  cout << "Reading camera poses" << endl;
67  ifstream pose_file(pose_loc.c_str());
68 
69  int pose_index;
70  MatrixRowMajor m(4,4);
71  //read camera pose parameters and use to make initial estimates of camera poses
72  while (pose_file >> pose_index) {
73  for (int i = 0; i < 16; i++)
74  pose_file >> m.data()[i];
75  initial_estimate.insert(pose_index, Pose3(m));
76  }
77 
78  // landmark keys
79  size_t landmark_key;
80 
81  // pixel coordinates uL, uR, v (same for left/right images due to rectification)
82  // landmark coordinates X, Y, Z in camera frame, resulting from triangulation
83  double uL, uR, v, X, Y, Z;
84  ifstream factor_file(factor_loc.c_str());
85  cout << "Reading stereo factors" << endl;
86 
87  //read stereo measurements and construct smart factors
88 
89  SmartFactor::shared_ptr factor(new SmartFactor(model, K));
90  size_t current_l = 3; // hardcoded landmark ID from first measurement
91 
92  while (factor_file >> pose_index >> landmark_key >> uL >> uR >> v >> X >> Y >> Z) {
93 
94  if(current_l != landmark_key) {
95  graph.push_back(factor);
96  factor = SmartFactor::shared_ptr(new SmartFactor(model, K));
97  current_l = landmark_key;
98  }
99  factor->add(Point2(uL,v), pose_index);
100  }
101 
102  Pose3 firstPose = initial_estimate.at<Pose3>(1);
103  //constrain the first pose such that it cannot change from its original value during optimization
104  // NOTE: NonlinearEquality forces the optimizer to use QR rather than Cholesky
105  // QR is much slower than Cholesky, but numerically more stable
106  graph.emplace_shared<NonlinearEquality<Pose3> >(1,firstPose);
107 
109  params.verbosityLM = LevenbergMarquardtParams::TRYLAMBDA;
110  params.verbosity = NonlinearOptimizerParams::ERROR;
111 
112  cout << "Optimizing" << endl;
113  //create Levenberg-Marquardt optimizer to optimize the factor graph
114  LevenbergMarquardtOptimizer optimizer(graph, initial_estimate, params);
115  Values result = optimizer.optimize();
116 
117  cout << "Final result sample:" << endl;
118  Values pose_values = utilities::allPose3s(result);
119  pose_values.print("Final camera poses:\n");
120 
121  return 0;
122 }
Matrix3f m
const char Y
Smart factor on poses, assuming camera calibration is fixed.
virtual const Values & optimize()
Scalar * b
Definition: benchVecAdd.cpp:17
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:196
A non-templated config holding any types of Manifold-group elements.
Factor Graph consisting of non-linear factors.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
noiseModel::Diagonal::shared_ptr model
const ValueType at(Key j) const
Definition: Values-inl.h:204
Vector2 Point2
Definition: Point2.h:32
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:190
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Definition: BFloat16.h:88
NonlinearFactorGraph graph
static const SmartProjectionParams params
#define Z
Definition: icosphere.cpp:21
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:66
const double fy
std::shared_ptr< Cal3_S2 > shared_ptr
Definition: Cal3_S2.h:39
Values result
SmartProjectionPoseFactor< Cal3_S2 > SmartFactor
Array< int, Dynamic, 1 > v
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
RealScalar s
Values allPose3s(const Values &values)
Extract all Pose3 values.
The most common 5DOF 3D->2D calibration.
Definition: Cal3_S2.h:34
std::shared_ptr< Isotropic > shared_ptr
Definition: NoiseModel.h:542
static const double u0
traits
Definition: chartTesting.h:28
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)
Definition: dataset.cpp:70
The most common 5DOF 3D->2D calibration + Stereo baseline.
static const double v0
int main(int argc, char **argv)
void insert(Key j, const Value &val)
Definition: Values.cpp:155
const double fx
The matrix class, also used for vectors and row-vectors.
#define X
Definition: icosphere.cpp:20
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
utility functions for loading datasets


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:51