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
gtsam::Cal3::fx_
double fx_
Definition: Cal3.h:71
gtsam_unstable.tests.test_ProjectionFactorRollingShutter.cal
cal
Definition: test_ProjectionFactorRollingShutter.py:27
gtsam::Cal3Bundler::D2d_calibration
Matrix23 D2d_calibration(const Point2 &p) const
Definition: Cal3Bundler.cpp:140
H
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
Definition: gnuplot_common_settings.hh:74
gtsam::Cal3Bundler::k
Vector4 k() const
Radial distortion parameters (4 of them, 2 0)
Definition: Cal3Bundler.cpp:35
base
Annotation indicating that a class derives from another given type.
Definition: attr.h:64
Vector.h
typedef and functions to augment Eigen's VectorXd
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::operator<<
std::ostream & operator<<(std::ostream &os, const Dih6 &m)
Definition: testGroup.cpp:109
gtsam::distance2
double distance2(const Point2 &p, const Point2 &q, OptionalJacobian< 1, 2 > H1, OptionalJacobian< 1, 2 > H2)
distance between two points
Definition: Point2.cpp:39
Matrix.h
typedef and functions to augment Eigen's MatrixXd
x
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
Definition: gnuplot_common_settings.hh:12
gtsam::Cal3Bundler::tol_
double tol_
tolerance value when calibrating
Definition: Cal3Bundler.h:35
gtsam::Cal3Bundler::D2d_intrinsic
Matrix2 D2d_intrinsic(const Point2 &p) const
Definition: Cal3Bundler.cpp:133
gtsam::Cal3Bundler::print
void print(const std::string &s="") const override
print with optional string
Definition: Cal3Bundler.cpp:52
Point3.h
3D Point
gtsam::Cal3Bundler::vector
Vector3 vector() const
Definition: Cal3Bundler.cpp:42
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:43
os
ofstream os("timeSchurFactors.csv")
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
gtsam::Cal3Bundler::D2d_intrinsic_calibration
Matrix25 D2d_intrinsic_calibration(const Point2 &p) const
Definition: Cal3Bundler.cpp:147
gtsam::Cal3::u0_
double u0_
Definition: Cal3.h:73
Point2.h
2D Point
boost::multiprecision::fabs
Real fabs(const Real &a)
Definition: boostmultiprec.cpp:119
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
gtsam::Cal3Bundler::uncalibrate
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
gtsam::Cal3
Common base class for all calibration models.
Definition: Cal3.h:69
gtsam::Cal3Bundler
Calibration used by Bundler.
Definition: Cal3Bundler.h:32
gtsam::Cal3::fy_
double fy_
focal length
Definition: Cal3.h:71
gtsam::Point2
Vector2 Point2
Definition: Point2.h:32
gtsam::Cal3Bundler::k1_
double k1_
Definition: Cal3Bundler.h:34
g
void g(const string &key, int i)
Definition: testBTree.cpp:41
y
Scalar * y
Definition: level1_cplx_impl.h:124
gtsam::Cal3::equals
bool equals(const Cal3 &K, double tol=10e-9) const
Check if equal up to specified tolerance.
Definition: Cal3.cpp:59
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
gtsam
traits
Definition: chartTesting.h:28
gtsam::Cal3Bundler::px
double px() const
image center in x
Definition: Cal3Bundler.h:93
K
#define K
Definition: igam.h:8
gtsam::OptionalJacobian
Definition: OptionalJacobian.h:38
gtsam::Cal3Bundler::k2_
double k2_
radial distortion
Definition: Cal3Bundler.h:34
p
float * p
Definition: Tutorial_Map_using.cpp:9
gtsam::Cal3Bundler::equals
bool equals(const Cal3Bundler &K, double tol=10e-9) const
assert equality up to a tolerance
Definition: Cal3Bundler.cpp:58
gtsam::Cal3Bundler::K
Matrix3 K() const override
Standard 3*3 calibration matrix.
Definition: Cal3Bundler.cpp:27
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::tol
const G double tol
Definition: Group.h:79
pybind11
Definition: wrap/pybind11/pybind11/__init__.py:1
gtsam::Cal3Bundler::calibrate
Point2 calibrate(const Point2 &pi, OptionalJacobian< 2, 3 > Dcal={}, OptionalJacobian< 2, 2 > Dp={}) const
Definition: Cal3Bundler.cpp:95
Cal3Bundler.h
Calibration used by Bundler.
gtsam::Cal3::v0_
double v0_
principal point
Definition: Cal3.h:73


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:01:50