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 x = (pi.x() - u0_) / fx_, y = (pi.y() - v0_) / fx_;
100  const Point2 invKPi(x, y);
101 
102  // initialize by ignoring the distortion at all, might be problematic for
103  // pixels around boundary
104  Point2 pn(x, y);
105 
106  // iterate until the uncalibrate is close to the actual pixel coordinate
107  const int maxIterations = 10;
108  int iteration;
109  for (iteration = 0; iteration < maxIterations; ++iteration) {
110  if (distance2(uncalibrate(pn), pi) <= tol_) break;
111  const double px = pn.x(), py = pn.y(), xx = px * px, yy = py * py;
112  const double rr = xx + yy;
113  const double g = (1 + k1_ * rr + k2_ * rr * rr);
114  pn = invKPi / g;
115  }
116 
117  if (iteration >= maxIterations)
118  throw std::runtime_error(
119  "Cal3Bundler::calibrate fails to converge. need a better "
120  "initialization");
121 
122  calibrateJacobians<Cal3Bundler, dimension>(*this, pn, Dcal, Dp);
123 
124  return pn;
125 }
126 
127 /* ************************************************************************* */
128 Matrix2 Cal3Bundler::D2d_intrinsic(const Point2& p) const {
129  Matrix2 Dp;
130  uncalibrate(p, boost::none, Dp);
131  return Dp;
132 }
133 
134 /* ************************************************************************* */
135 Matrix23 Cal3Bundler::D2d_calibration(const Point2& p) const {
136  Matrix23 Dcal;
137  uncalibrate(p, Dcal, boost::none);
138  return Dcal;
139 }
140 
141 /* ************************************************************************* */
143  Matrix23 Dcal;
144  Matrix2 Dp;
145  uncalibrate(p, Dcal, Dp);
146  Matrix25 H;
147  H << Dp, Dcal;
148  return H;
149 }
150 
151 } // \ 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
Vector3 vector() const
Definition: Cal3Bundler.cpp:42
Scalar * y
Eigen::Vector3d Vector3
Definition: Vector.h:43
double k1() const
distorsion parameter k1
Definition: Cal3Bundler.h:84
Vector2 Point2
Definition: Point2.h:27
ArrayXcf v
Definition: Cwise_arg.cpp:1
double distance2(const Point2 &p, const Point2 &q, OptionalJacobian< 1, 2 > H1, OptionalJacobian< 1, 2 > H2)
distance between two points
Definition: Point2.cpp:39
double u0_
Definition: Cal3.h:73
double k2() const
distorsion parameter k2
Definition: Cal3Bundler.h:87
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:43
Array33i a
Point2 calibrate(const Point2 &pi, OptionalJacobian< 2, 3 > Dcal=boost::none, OptionalJacobian< 2, 2 > Dp=boost::none) const
Definition: Cal3Bundler.cpp:95
Eigen::VectorXd Vector
Definition: Vector.h:38
Matrix23 D2d_calibration(const Point2 &p) const
double tol_
tolerance value when calibrating
Definition: Cal3Bundler.h:35
double fy_
focal length
Definition: Cal3.h:71
bool equals(const Cal3Bundler &K, double tol=10e-9) const
assert equality up to a tolerance
Definition: Cal3Bundler.cpp:58
RealScalar s
bool equals(const Cal3 &K, double tol=10e-9) const
Check if equal up to specified tolerance.
Definition: Cal3.cpp:59
double v0_
principal point
Definition: Cal3.h:73
double px() const
image center in x
Definition: Cal3Bundler.h:90
traits
Definition: chartTesting.h:28
typedef and functions to augment Eigen&#39;s VectorXd
ofstream os("timeSchurFactors.csv")
3D Point
float * p
double fx() const
focal length x
Definition: Cal3.h:139
double k2_
radial distortion
Definition: Cal3Bundler.h:34
Annotation indicating that a class derives from another given type.
Definition: attr.h:42
const G double tol
Definition: Group.h:83
double fx_
Definition: Cal3.h:71
Matrix2 D2d_intrinsic(const Point2 &p) const
Vector4 k() const
Radial distortion parameters (4 of them, 2 0)
Definition: Cal3Bundler.cpp:35
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
double py() const
image center in y
Definition: Cal3Bundler.h:93
2D Point
Calibration used by Bundler.
Matrix25 D2d_intrinsic_calibration(const Point2 &p) const
Point2 uncalibrate(const Point2 &p, OptionalJacobian< 2, 3 > Dcal=boost::none, OptionalJacobian< 2, 2 > Dp=boost::none) const
: convert intrinsic coordinates xy to image coordinates uv Version of uncalibrate with derivatives ...
Definition: Cal3Bundler.cpp:66


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