CameraResectioning.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 
19 #include <gtsam/inference/Symbol.h>
22 #include <gtsam/geometry/Cal3_S2.h>
23 
24 using namespace gtsam;
25 using namespace gtsam::noiseModel;
27 
32 class ResectioningFactor: public NoiseModelFactorN<Pose3> {
34 
38 
39 public:
40 
43  const Cal3_S2::shared_ptr& calib, const Point2& p, const Point3& P) :
44  Base(model, key), K_(calib), P_(P), p_(p) {
45  }
46 
48  Vector evaluateError(const Pose3& pose, OptionalMatrixType H) const override {
49  PinholeCamera<Cal3_S2> camera(pose, *K_);
50  return camera.project(P_, H, OptionalNone, OptionalNone) - p_;
51  }
52 };
53 
54 /*******************************************************************************
55  * Camera: f = 1, Image: 100x100, center: 50, 50.0
56  * Pose (ground truth): (Xw, -Yw, -Zw, [0,0,2.0]')
57  * Known landmarks:
58  * 3D Points: (10,10,0) (-10,10,0) (-10,-10,0) (10,-10,0)
59  * Perfect measurements:
60  * 2D Point: (55,45) (45,45) (45,55) (55,55)
61  *******************************************************************************/
62 int main(int argc, char* argv[]) {
63  /* read camera intrinsic parameters */
64  Cal3_S2::shared_ptr calib(new Cal3_S2(1, 1, 0, 50, 50));
65 
66  /* 1. create graph */
68 
69  /* 2. add factors to the graph */
70  // add measurement factors
71  SharedDiagonal measurementNoise = Diagonal::Sigmas(Vector2(0.5, 0.5));
72  std::shared_ptr<ResectioningFactor> factor;
73  graph.emplace_shared<ResectioningFactor>(measurementNoise, X(1), calib,
74  Point2(55, 45), Point3(10, 10, 0));
75  graph.emplace_shared<ResectioningFactor>(measurementNoise, X(1), calib,
76  Point2(45, 45), Point3(-10, 10, 0));
77  graph.emplace_shared<ResectioningFactor>(measurementNoise, X(1), calib,
78  Point2(45, 55), Point3(-10, -10, 0));
79  graph.emplace_shared<ResectioningFactor>(measurementNoise, X(1), calib,
80  Point2(55, 55), Point3(10, -10, 0));
81 
82  /* 3. Create an initial estimate for the camera pose */
84  initial.insert(X(1),
85  Pose3(Rot3(1, 0, 0, 0, -1, 0, 0, 0, -1), Point3(0, 0, 2)));
86 
87  /* 4. Optimize the graph using Levenberg-Marquardt*/
89  result.print("Final result:\n");
90 
91  return 0;
92 }
const gtsam::Symbol key('X', 0)
int main(int argc, char *argv[])
virtual const Values & optimize()
Cal3_S2::shared_ptr K_
camera&#39;s intrinsic parameters
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:196
noiseModel::Diagonal::shared_ptr model
Vector2 Point2
Definition: Point2.h:32
Values initial
Point2 p_
2D measurement of the 3D point
Key X(std::uint64_t j)
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 y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange [*:*] noreverse nowriteback set trange [*:*] noreverse nowriteback set urange [*:*] noreverse nowriteback set vrange [*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Vector evaluateError(const Pose3 &pose, OptionalMatrixType H) const override
evaluate the error
NonlinearFactorGraph graph
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
#define OptionalNone
Base class for all pinhole cameras.
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:66
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}, OptionalJacobian< 2, DimK > Dcal={}) const
project a 3D point from world coordinates into the image
Definition: PinholePose.h:112
Matrix * OptionalMatrixType
std::shared_ptr< Cal3_S2 > shared_ptr
Definition: Cal3_S2.h:39
Eigen::VectorXd Vector
Definition: Vector.h:38
Values result
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
The most common 5DOF 3D->2D calibration.
Definition: Cal3_S2.h:34
noiseModel::Diagonal::shared_ptr SharedDiagonal
Definition: NoiseModel.h:743
traits
Definition: chartTesting.h:28
static const Pose3 pose(Rot3(Vector3(1, -1, -1).asDiagonal()), Point3(0, 0, 0.5))
Eigen::Vector2d Vector2
Definition: Vector.h:42
ResectioningFactor(const SharedNoiseModel &model, const Key &key, const Cal3_S2::shared_ptr &calib, const Point2 &p, const Point3 &P)
Construct factor given known point P and its projection p.
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:270
Point3 P_
3D point on the calibration rig
float * p
NoiseModelFactorN< Pose3 > Base
void insert(Key j, const Value &val)
Definition: Values.cpp:155
Vector3 Point3
Definition: Point3.h:38
static const CalibratedCamera camera(kDefaultPose)
#define X
Definition: icosphere.cpp:20
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:741
The most common 5DOF 3D->2D calibration.
All noise models live in the noiseModel namespace.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:00