StereoVOExample_large.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 
27 #include <gtsam/geometry/Pose3.h>
29 #include <gtsam/nonlinear/Values.h>
33 #include <gtsam/inference/Symbol.h>
35 #include <gtsam/slam/dataset.h>
36 
37 #include <string>
38 #include <fstream>
39 #include <iostream>
40 
41 using namespace std;
42 using namespace gtsam;
43 
44 int main(int argc, char** argv) {
47  const auto model = noiseModel::Isotropic::Sigma(3, 1);
48 
49  string calibration_loc = findExampleDataFile("VO_calibration.txt");
50  string pose_loc = findExampleDataFile("VO_camera_poses_large.txt");
51  string factor_loc = findExampleDataFile("VO_stereo_factors_large.txt");
52 
53  // read camera calibration info from file
54  // focal lengths fx, fy, skew s, principal point u0, v0, baseline b
55  double fx, fy, s, u0, v0, b;
56  ifstream calibration_file(calibration_loc.c_str());
57  cout << "Reading calibration info" << endl;
58  calibration_file >> fx >> fy >> s >> u0 >> v0 >> b;
59 
60  // create stereo camera calibration object
61  const Cal3_S2Stereo::shared_ptr K(new Cal3_S2Stereo(fx, fy, s, u0, v0, b));
62 
63  ifstream pose_file(pose_loc.c_str());
64  cout << "Reading camera poses" << endl;
65  int pose_id;
66  MatrixRowMajor m(4, 4);
67  // read camera pose parameters and use to make initial estimates of camera
68  // poses
69  while (pose_file >> pose_id) {
70  for (int i = 0; i < 16; i++) {
71  pose_file >> m.data()[i];
72  }
73  initial_estimate.insert(Symbol('x', pose_id), Pose3(m));
74  }
75 
76  // camera and landmark keys
77  size_t x, l;
78 
79  // pixel coordinates uL, uR, v (same for left/right images due to
80  // rectification) landmark coordinates X, Y, Z in camera frame, resulting from
81  // triangulation
82  double uL, uR, v, X, Y, Z;
83  ifstream factor_file(factor_loc.c_str());
84  cout << "Reading stereo factors" << endl;
85  // read stereo measurement details from file and use to create and add
86  // GenericStereoFactor objects to the graph representation
87  while (factor_file >> x >> l >> uL >> uR >> v >> X >> Y >> Z) {
89  StereoPoint2(uL, uR, v), model, Symbol('x', x), Symbol('l', l), K);
90  // if the landmark variable included in this factor has not yet been added
91  // to the initial variable value estimate, add it
92  if (!initial_estimate.exists(Symbol('l', l))) {
93  Pose3 camPose = initial_estimate.at<Pose3>(Symbol('x', x));
94  // transformFrom() transforms the input Point3 from the camera pose space,
95  // camPose, to the global space
96  Point3 worldPoint = camPose.transformFrom(Point3(X, Y, Z));
97  initial_estimate.insert(Symbol('l', l), worldPoint);
98  }
99  }
100 
101  Pose3 first_pose = initial_estimate.at<Pose3>(Symbol('x', 1));
102  // constrain the first pose such that it cannot change from its original value
103  // 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> >(Symbol('x', 1), first_pose);
107 
108  cout << "Optimizing" << endl;
109  // create Levenberg-Marquardt optimizer to optimize the factor graph
111  params.orderingType = Ordering::METIS;
112  LevenbergMarquardtOptimizer optimizer(graph, initial_estimate, params);
113  Values result = optimizer.optimize();
114 
115  cout << "Final result sample:" << endl;
116  Values pose_values = result.filter<Pose3>();
117  pose_values.print("Final camera poses:\n");
118 
119  return 0;
120 }
Matrix3f m
bool exists(Key j) const
Definition: Values.cpp:104
virtual const Values & optimize()
Scalar * b
Definition: benchVecAdd.cpp:17
A non-templated config holding any types of Manifold-group elements.
Factor Graph consisting of non-linear factors.
noiseModel::Diagonal::shared_ptr model
void insert(Key j, const Value &val)
Definition: Values.cpp:140
ArrayXcf v
Definition: Cwise_arg.cpp:1
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Definition: Half.h:150
NonlinearFactorGraph graph
string findExampleDataFile(const string &name)
Definition: dataset.cpp:65
#define Z
Definition: icosphere.cpp:21
Point3 transformFrom(const Point3 &point, OptionalJacobian< 3, 6 > Hself=boost::none, OptionalJacobian< 3, 3 > Hpoint=boost::none) const
takes point in Pose coordinates and transforms it to world coordinates
Definition: Pose3.cpp:298
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&...args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:172
static const Line3 l(Rot3(), 1, 1)
const double fy
boost::shared_ptr< Cal3_S2Stereo > shared_ptr
Definition: Cal3_S2Stereo.h:38
Values result
const ValueType at(Key j) const
Definition: Values-inl.h:342
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
RealScalar s
static SmartStereoProjectionParams params
static const double u0
traits
Definition: chartTesting.h:28
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:77
The most common 5DOF 3D->2D calibration + Stereo baseline.
static Pose3 camPose(Rot3((Matrix3()<< 1., 0., 0., 0.,-1., 0., 0., 0.,-1.).finished()), Point3(0, 0, 6.25))
static const double v0
Vector3 Point3
Definition: Point3.h:35
const double fx
Ordering::OrderingType orderingType
The method of ordering use during variable elimination (default COLAMD)
The matrix class, also used for vectors and row-vectors.
#define X
Definition: icosphere.cpp:20
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
3D Pose
utility functions for loading datasets
int main(int argc, char **argv)
Filtered< Value > filter(const boost::function< bool(Key)> &filterFcn)
Definition: Values-inl.h:237


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