Cal3Bundler.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 
18 #include <gtsam/base/Matrix.h>
19 #include <gtsam/base/Vector.h>
21 #include <gtsam/geometry/Point2.h>
22 #include <gtsam/geometry/Point3.h>
23 
24 namespace gtsam {
25 
26 /* ************************************************************************* */
27 Matrix3 Cal3Bundler::K() const {
28  // This function is needed to ensure skew = 0;
29  Matrix3 K;
30  K << fx_, 0, u0_, 0, fy_, v0_, 0, 0, 1.0;
31  return K;
32 }
33 
34 /* ************************************************************************* */
35 Vector4 Cal3Bundler::k() const {
36  Vector4 rvalue_;
37  rvalue_ << k1_, k2_, 0, 0;
38  return rvalue_;
39 }
40 
41 /* ************************************************************************* */
42 Vector3 Cal3Bundler::vector() const { return Vector3(fx_, k1_, k2_); }
43 
44 /* ************************************************************************* */
45 std::ostream& operator<<(std::ostream& os, const Cal3Bundler& cal) {
46  os << "f: " << cal.fx() << ", k1: " << cal.k1() << ", k2: " << cal.k2()
47  << ", px: " << cal.px() << ", py: " << cal.py();
48  return os;
49 }
50 
51 /* ************************************************************************* */
52 void Cal3Bundler::print(const std::string& s) const {
53  gtsam::print((Vector)(Vector(5) << fx_, k1_, k2_, u0_, v0_).finished(),
54  s + ".K");
55 }
56 
57 /* ************************************************************************* */
58 bool Cal3Bundler::equals(const Cal3Bundler& K, double tol) const {
59  const Cal3* base = dynamic_cast<const Cal3*>(&K);
60  return (Cal3::equals(*base, tol) && std::fabs(k1_ - K.k1_) < tol &&
61  std::fabs(k2_ - K.k2_) < tol && std::fabs(u0_ - K.u0_) < tol &&
62  std::fabs(v0_ - K.v0_) < tol);
63 }
64 
65 /* ************************************************************************* */
67  OptionalJacobian<2, 2> Dp) const {
68  // r = x² + y²;
69  // g = (1 + k(1)*r + k(2)*r²);
70  // pi(:,i) = g * pn(:,i)
71  const double x = p.x(), y = p.y();
72  const double r = x * x + y * y;
73  const double g = 1. + (k1_ + k2_ * r) * r;
74  const double u = g * x, v = g * y;
75 
76  const double f_ = fx_;
77 
78  // Derivatives make use of intermediate variables above
79  if (Dcal) {
80  double rx = r * x, ry = r * y;
81  *Dcal << u, f_ * rx, f_ * r * rx, v, f_ * ry, f_ * r * ry;
82  }
83 
84  if (Dp) {
85  const double a = 2. * (k1_ + 2. * k2_ * r);
86  const double axx = a * x * x, axy = a * x * y, ayy = a * y * y;
87  *Dp << g + axx, axy, axy, g + ayy;
88  *Dp *= f_;
89  }
90 
91  return Point2(u0_ + f_ * u, v0_ + f_ * v);
92 }
93 
94 /* ************************************************************************* */
96  OptionalJacobian<2, 2> Dp) const {
97  // Copied from Cal3DS2
98  // but specialized with k1, k2 non-zero only and fx=fy and s=0
99  double px = (pi.x() - u0_) / fx_, py = (pi.y() - v0_) / fx_;
100  const Point2 invKPi(px, py);
101  Point2 pn;
102 
103  // iterate until the uncalibrate is close to the actual pixel coordinate
104  const int maxIterations = 10;
105  int iteration = 0;
106  do {
107  // initialize pn with distortion included
108  const double rr = (px * px) + (py * py);
109  const double g = (1 + k1_ * rr + k2_ * rr * rr);
110  pn = invKPi / g;
111 
112  if (distance2(uncalibrate(pn), pi) <= tol_) break;
113 
114  // Set px and py using intrinsic coordinates since that is where radial
115  // distortion correction is done.
116  px = pn.x();
117  py = pn.y();
118  iteration++;
119 
120  } while (iteration < maxIterations);
121 
122  if (iteration >= maxIterations)
123  throw std::runtime_error(
124  "Cal3Bundler::calibrate fails to converge. need a better "
125  "initialization");
126 
127  calibrateJacobians<Cal3Bundler, dimension>(*this, pn, Dcal, Dp);
128 
129  return pn;
130 }
131 
132 /* ************************************************************************* */
133 Matrix2 Cal3Bundler::D2d_intrinsic(const Point2& p) const {
134  Matrix2 Dp;
135  uncalibrate(p, {}, Dp);
136  return Dp;
137 }
138 
139 /* ************************************************************************* */
140 Matrix23 Cal3Bundler::D2d_calibration(const Point2& p) const {
141  Matrix23 Dcal;
142  uncalibrate(p, Dcal, {});
143  return Dcal;
144 }
145 
146 /* ************************************************************************* */
148  Matrix23 Dcal;
149  Matrix2 Dp;
150  uncalibrate(p, Dcal, Dp);
151  Matrix25 H;
152  H << Dp, Dcal;
153  return H;
154 }
155 
156 } // namespace gtsam
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
GTSAM_EXPORT friend std::ostream & operator<<(std::ostream &os, const Cal3Bundler &cal)
Output stream operator.
Definition: Cal3Bundler.cpp:45
Matrix25 D2d_intrinsic_calibration(const Point2 &p) const
Scalar * y
Eigen::Vector3d Vector3
Definition: Vector.h:43
Vector3 vector() const
Definition: Cal3Bundler.cpp:42
Vector2 Point2
Definition: Point2.h:32
double u0_
Definition: Cal3.h:73
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
Real fabs(const Real &a)
Matrix3 K() const override
Standard 3*3 calibration matrix.
Definition: Cal3Bundler.cpp:27
void g(const string &key, int i)
Definition: testBTree.cpp:41
Common base class for all calibration models.
Definition: Cal3.h:69
Vector4 k() const
Radial distortion parameters (4 of them, 2 0)
Definition: Cal3Bundler.cpp:35
Eigen::VectorXd Vector
Definition: Vector.h:38
double tol_
tolerance value when calibrating
Definition: Cal3Bundler.h:35
double fy_
focal length
Definition: Cal3.h:71
bool equals(const Cal3 &K, double tol=10e-9) const
Check if equal up to specified tolerance.
Definition: Cal3.cpp:59
Point2 calibrate(const Point2 &pi, OptionalJacobian< 2, 3 > Dcal={}, OptionalJacobian< 2, 2 > Dp={}) const
Definition: Cal3Bundler.cpp:95
double k1() const
distorsion parameter k1
Definition: Cal3Bundler.h:87
double fx() const
focal length x
Definition: Cal3.h:139
Array< int, Dynamic, 1 > v
bool equals(const Cal3Bundler &K, double tol=10e-9) const
assert equality up to a tolerance
Definition: Cal3Bundler.cpp:58
RealScalar s
double v0_
principal point
Definition: Cal3.h:73
traits
Definition: chartTesting.h:28
typedef and functions to augment Eigen&#39;s VectorXd
double k2() const
distorsion parameter k2
Definition: Cal3Bundler.h:90
Calibration used by Bundler.
Definition: Cal3Bundler.h:32
ofstream os("timeSchurFactors.csv")
double distance2(const Point2 &p, const Point2 &q, OptionalJacobian< 1, 2 > H1, OptionalJacobian< 1, 2 > H2)
distance between two points
Definition: Point2.cpp:39
3D Point
Matrix2 D2d_intrinsic(const Point2 &p) const
float * p
double px() const
image center in x
Definition: Cal3Bundler.h:93
Matrix23 D2d_calibration(const Point2 &p) const
double k2_
radial distortion
Definition: Cal3Bundler.h:34
Annotation indicating that a class derives from another given type.
Definition: attr.h:61
const G double tol
Definition: Group.h:86
Point2 uncalibrate(const Point2 &p, OptionalJacobian< 2, 3 > Dcal={}, OptionalJacobian< 2, 2 > Dp={}) const
: convert intrinsic coordinates xy to image coordinates uv Version of uncalibrate with derivatives ...
Definition: Cal3Bundler.cpp:66
double fx_
Definition: Cal3.h:71
void print(const std::string &s="") const override
print with optional string
Definition: Cal3Bundler.cpp:52
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
2D Point
Calibration used by Bundler.
double py() const
image center in y
Definition: Cal3Bundler.h:96


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