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>
34 #include <gtsam/inference/Symbol.h>
36 #include <gtsam/slam/dataset.h>
37 
38 #include <string>
39 #include <fstream>
40 #include <iostream>
41 
42 using namespace std;
43 using namespace gtsam;
44 
45 int main(int argc, char** argv) {
46  Values initial_estimate;
48  const auto model = noiseModel::Isotropic::Sigma(3, 1);
49 
50  string calibration_loc = findExampleDataFile("VO_calibration.txt");
51  string pose_loc = findExampleDataFile("VO_camera_poses_large.txt");
52  string factor_loc = findExampleDataFile("VO_stereo_factors_large.txt");
53 
54  // read camera calibration info from file
55  // focal lengths fx, fy, skew s, principal point u0, v0, baseline b
56  double fx, fy, s, u0, v0, b;
57  ifstream calibration_file(calibration_loc.c_str());
58  cout << "Reading calibration info" << endl;
59  calibration_file >> fx >> fy >> s >> u0 >> v0 >> b;
60 
61  // create stereo camera calibration object
62  const Cal3_S2Stereo::shared_ptr K(new Cal3_S2Stereo(fx, fy, s, u0, v0, b));
63 
64  ifstream pose_file(pose_loc.c_str());
65  cout << "Reading camera poses" << endl;
66  int pose_id;
67  MatrixRowMajor m(4, 4);
68  // read camera pose parameters and use to make initial estimates of camera
69  // poses
70  while (pose_file >> pose_id) {
71  for (int i = 0; i < 16; i++) {
72  pose_file >> m.data()[i];
73  }
74  initial_estimate.insert(Symbol('x', pose_id), Pose3(m));
75  }
76 
77  // camera and landmark keys
78  size_t x, l;
79 
80  // pixel coordinates uL, uR, v (same for left/right images due to
81  // rectification) landmark coordinates X, Y, Z in camera frame, resulting from
82  // triangulation
83  double uL, uR, v, X, Y, Z;
84  ifstream factor_file(factor_loc.c_str());
85  cout << "Reading stereo factors" << endl;
86  // read stereo measurement details from file and use to create and add
87  // GenericStereoFactor objects to the graph representation
88  while (factor_file >> x >> l >> uL >> uR >> v >> X >> Y >> Z) {
90  StereoPoint2(uL, uR, v), model, Symbol('x', x), Symbol('l', l), K);
91  // if the landmark variable included in this factor has not yet been added
92  // to the initial variable value estimate, add it
93  if (!initial_estimate.exists(Symbol('l', l))) {
94  Pose3 camPose = initial_estimate.at<Pose3>(Symbol('x', x));
95  // transformFrom() transforms the input Point3 from the camera pose space,
96  // camPose, to the global space
97  Point3 worldPoint = camPose.transformFrom(Point3(X, Y, Z));
98  initial_estimate.insert(Symbol('l', l), worldPoint);
99  }
100  }
101 
102  Pose3 first_pose = initial_estimate.at<Pose3>(Symbol('x', 1));
103  // constrain the first pose such that it cannot change from its original value
104  // during optimization
105  // NOTE: NonlinearEquality forces the optimizer to use QR rather than Cholesky
106  // QR is much slower than Cholesky, but numerically more stable
107  graph.emplace_shared<NonlinearEquality<Pose3> >(Symbol('x', 1), first_pose);
108 
109  cout << "Optimizing" << endl;
110  // create Levenberg-Marquardt optimizer to optimize the factor graph
112  params.orderingType = Ordering::METIS;
113  LevenbergMarquardtOptimizer optimizer(graph, initial_estimate, params);
114  Values result = optimizer.optimize();
115 
116  cout << "Final result sample:" << endl;
117  Values pose_values = utilities::allPose3s(result);
118  pose_values.print("Final camera poses:\n");
119 
120  return 0;
121 }
Matrix3f m
const char Y
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
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Definition: BFloat16.h:88
NonlinearFactorGraph graph
static const SmartProjectionParams params
static Pose3 camPose(Rot3((Matrix3()<< 1., 0., 0., 0.,-1., 0., 0., 0.,-1.).finished()), Point3(0, 0, 6.25))
#define Z
Definition: icosphere.cpp:21
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:66
static const Line3 l(Rot3(), 1, 1)
const double fy
Values result
The most common 5DOF 3D->2D calibration, stereo version.
Definition: Cal3_S2Stereo.h:30
Array< int, Dynamic, 1 > v
Point3 transformFrom(const Point3 &point, OptionalJacobian< 3, 6 > Hself={}, OptionalJacobian< 3, 3 > Hpoint={}) const
takes point in Pose coordinates and transforms it to world coordinates
Definition: Pose3.cpp:347
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
RealScalar s
Values allPose3s(const Values &values)
Extract all Pose3 values.
static const double u0
std::shared_ptr< Cal3_S2Stereo > shared_ptr
Definition: Cal3_S2Stereo.h:38
A non-linear factor for stereo measurements.
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
void insert(Key j, const Value &val)
Definition: Values.cpp:155
Vector3 Point3
Definition: Point3.h:38
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
bool exists(Key j) const
Definition: Values.cpp:93
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)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:36:20