gtsam
Loading...
Searching...
No Matches
PinholeCamera.h
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
19#pragma once
20
24
25namespace gtsam {
26
33template<typename Calibration>
34class PinholeCamera: public PinholeBaseK<Calibration> {
35
36public:
37
43 typedef Point2Vector MeasurementVector;
44
45private:
46
47 typedef PinholeBaseK<Calibration> Base;
48 Calibration K_;
49
50 // Get dimensions of calibration type at compile time
51 static const int DimK = FixedDimension<Calibration>::value;
52
53public:
54
55 inline constexpr static auto calibration_dimension = DimK;
56 inline constexpr static auto dimension = 6 + DimK;
57
60
63 }
64
66 explicit PinholeCamera(const Pose3& pose) :
67 Base(pose) {
68 }
69
71 PinholeCamera(const Pose3& pose, const Calibration& K) :
72 Base(pose), K_(K) {
73 }
74
78
86 static PinholeCamera Level(const Calibration &K, const Pose2& pose2,
87 double height) {
88 return PinholeCamera(Base::LevelPose(pose2, height), K);
89 }
90
92 static PinholeCamera Level(const Pose2& pose2, double height) {
93 return PinholeCamera::Level(Calibration(), pose2, height);
94 }
95
105 static PinholeCamera Lookat(const Point3& eye, const Point3& target,
106 const Point3& upVector, const Calibration& K = Calibration()) {
107 return PinholeCamera(Base::LookatPose(eye, target, upVector), K);
108 }
109
110 // Create PinholeCamera, with derivatives
111 static PinholeCamera Create(const Pose3& pose, const Calibration &K,
113 OptionalJacobian<dimension, DimK> H2 = {}) {
114 typedef Eigen::Matrix<double, DimK, 6> MatrixK6;
115 if (H1)
116 *H1 << I_6x6, MatrixK6::Zero();
117 typedef Eigen::Matrix<double, 6, DimK> Matrix6K;
118 typedef Eigen::Matrix<double, DimK, DimK> MatrixK;
119 if (H2)
120 *H2 << Matrix6K::Zero(), MatrixK::Identity();
121 return PinholeCamera(pose,K);
122 }
123
127
129 explicit PinholeCamera(const Vector &v) :
130 Base(v.head<6>()) {
131 if (v.size() > 6)
132 K_ = Calibration(v.tail<DimK>());
133 }
134
136 PinholeCamera(const Vector &v, const Vector &K) :
137 Base(v), K_(K) {
138 }
139
143
145 bool equals(const Base &camera, double tol = 1e-9) const {
146 const PinholeCamera* e = dynamic_cast<const PinholeCamera*>(&camera);
147 return Base::equals(camera, tol) && K_.equals(e->calibration(), tol);
148 }
149
151 bool equals(const PinholeCamera& camera, double tol = 1e-9) const {
152 return Base::equals(camera, tol) && K_.equals(camera.calibration(), tol);
153 }
154
156 void print(const std::string& s = "PinholeCamera") const override {
157 Base::print(s);
158 K_.print(s + ".calibration");
159 }
160
164
165 ~PinholeCamera() override {
166 }
167
169 const Pose3& pose() const {
170 return Base::pose();
171 }
172
175 if (H) {
176 H->setZero();
177 H->template block<6, 6>(0, 0) = I_6x6;
178 }
179 return Base::pose();
180 }
181
183 const Calibration& calibration() const override {
184 return K_;
185 }
186
190
191 size_t dim() const {
192 return dimension;
193 }
194
195 static size_t Dim() {
196 return dimension;
197 }
198
199 typedef Eigen::Matrix<double, dimension, 1> VectorK6;
200
202 PinholeCamera retract(const Vector& d) const {
203 if ((size_t) d.size() == 6)
204 return PinholeCamera(this->pose().retract(d), calibration());
205 else
206 return PinholeCamera(this->pose().retract(d.head<6>()),
207 calibration().retract(d.tail<DimK>()));
208 }
209
211 VectorK6 localCoordinates(const PinholeCamera& T2) const {
212 VectorK6 d;
213 d.template head<6>() = this->pose().localCoordinates(T2.pose());
214 d.template tail<DimK>() = calibration().localCoordinates(T2.calibration());
215 return d;
216 }
217
220 return PinholeCamera(); // assumes that the default constructor is valid
221 }
222
226
227 typedef Eigen::Matrix<double, 2, DimK> Matrix2K;
228
232 template<class POINT>
234 OptionalJacobian<2, FixedDimension<POINT>::value> Dpoint) const {
235 // We just call 3-derivative version in Base
236 if (Dcamera){
237 Matrix26 Dpose;
238 Eigen::Matrix<double, 2, DimK> Dcal;
239 const Point2 pi = Base::project(pw, Dpose, Dpoint, Dcal);
240 *Dcamera << Dpose, Dcal;
241 return pi;
242 } else {
243 return Base::project(pw, {}, Dpoint, {});
244 }
245 }
246
249 {}, OptionalJacobian<2, 3> Dpoint = {}) const {
250 return _project2(pw, Dcamera, Dpoint);
251 }
252
255 {}, OptionalJacobian<2, 2> Dpoint = {}) const {
256 return _project2(pw, Dcamera, Dpoint);
257 }
258
264 double range(const Point3& point, OptionalJacobian<1, dimension> Dcamera =
265 {}, OptionalJacobian<1, 3> Dpoint = {}) const {
266 Matrix16 Dpose_ = Matrix16::Zero();
267 double result = this->pose().range(point, Dcamera ? &Dpose_ : 0, Dpoint);
268 if (Dcamera)
269 *Dcamera << Dpose_, Eigen::Matrix<double, 1, DimK>::Zero();
270 return result;
271 }
272
279 {}, OptionalJacobian<1, 6> Dpose = {}) const {
280 Matrix16 Dpose_ = Matrix16::Zero();
281 double result = this->pose().range(pose, Dcamera ? &Dpose_ : 0, Dpose);
282 if (Dcamera)
283 *Dcamera << Dpose_, Eigen::Matrix<double, 1, DimK>::Zero();
284 return result;
285 }
286
292 template<class CalibrationB>
293 double range(const PinholeCamera<CalibrationB>& camera,
296 Matrix16 Dcamera_ = Matrix16::Zero(), Dother_ = Matrix16::Zero();
297 double result = this->pose().range(camera.pose(), Dcamera ? &Dcamera_ : 0,
298 Dother ? &Dother_ : 0);
299 if (Dcamera) {
300 *Dcamera << Dcamera_, Eigen::Matrix<double, 1, DimK>::Zero();
301 }
302 if (Dother) {
303 Dother->setZero();
304 Dother->template block<1, 6>(0, 0) = Dother_;
305 }
306 return result;
307 }
308
314 double range(const CalibratedCamera& camera,
316 OptionalJacobian<1, 6> Dother = {}) const {
317 return range(camera.pose(), Dcamera, Dother);
318 }
319
321 Matrix34 cameraProjectionMatrix() const {
322 return K_.K() * PinholeBase::pose().inverse().matrix().block(0, 0, 3, 4);
323 }
324
327 return Eigen::Matrix<double,traits<Point2>::dimension,1>::Constant(2.0 * K_.fx());
328 }
329
330private:
331
332#if GTSAM_ENABLE_BOOST_SERIALIZATION
334 friend class boost::serialization::access;
335 template<class Archive>
336 void serialize(Archive & ar, const unsigned int /*version*/) {
337 ar
338 & boost::serialization::make_nvp("PinholeBaseK",
339 boost::serialization::base_object<Base>(*this));
340 ar & BOOST_SERIALIZATION_NVP(K_);
341 }
342#endif
343};
344
345// manifold traits
346
347template <typename Calibration>
348struct traits<PinholeCamera<Calibration> >
349 : public internal::Manifold<PinholeCamera<Calibration> > {};
350
351template <typename Calibration>
352struct traits<const PinholeCamera<Calibration> >
353 : public internal::Manifold<PinholeCamera<Calibration> > {};
354
355// range traits, used in RangeFactor
356template <typename Calibration, typename T>
357struct Range<PinholeCamera<Calibration>, T> : HasRange<PinholeCamera<Calibration>, T, double> {};
358
359} // \ gtsam
Macros for Matrix constants to avoid excessive template instantiation.
Pinhole camera with known calibration.
Bearing-Range product.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
Vector3 Point3
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point3 to Vector3...
Definition Point3.h:38
Vector2 Point2
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point2 to Vector2...
Definition Point2.h:32
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
TangentVector localCoordinates(const Class &g) const
localCoordinates as required by manifold concept: finds tangent vector between *this and g
Definition Lie.h:226
Both ManifoldTraits and Testable.
Definition Manifold.h:156
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
Definition BearingRange.h:42
Definition BearingRange.h:196
static Matrix26 Dpose(const Point2 &pn, double d)
Calculate Jacobian with respect to pose.
Definition CalibratedCamera.cpp:28
virtual void print(const std::string &s="PinholeBase") const
print
Definition CalibratedCamera.cpp:75
const Pose3 & pose() const
return pose, constant version
Definition CalibratedCamera.h:155
static Pose3 LevelPose(const Pose2 &pose2, double height)
Create a level pose at the given 2D pose and height.
Definition CalibratedCamera.cpp:50
bool equals(const PinholeBase &camera, double tol=1e-9) const
assert equality up to a tolerance
Definition CalibratedCamera.cpp:70
static Matrix23 Dpoint(const Point2 &pn, double d, const Matrix3 &Rt)
Calculate Jacobian with respect to point.
Definition CalibratedCamera.cpp:38
static Pose3 LookatPose(const Point3 &eye, const Point3 &target, const Point3 &upVector)
Create a camera pose at the given eye position looking at a target point in the scene with the specif...
Definition CalibratedCamera.cpp:59
A Calibrated camera class [R|-R't], calibration K=I.
Definition CalibratedCamera.h:252
This inverse() const
Group inverse.
Definition ExtendedPose3-inl.h:127
A pinhole camera class that has a Pose3 and a Calibration.
Definition PinholeCamera.h:34
Point2 project2(const Point3 &pw, OptionalJacobian< 2, dimension > Dcamera={}, OptionalJacobian< 2, 3 > Dpoint={}) const
project a 3D point from world coordinates into the image
Definition PinholeCamera.h:248
bool equals(const PinholeCamera &camera, double tol=1e-9) const
Compare with another camera of the same concrete type.
Definition PinholeCamera.h:151
void print(const std::string &s="PinholeCamera") const override
print
Definition PinholeCamera.h:156
Point2 _project2(const POINT &pw, OptionalJacobian< 2, dimension > Dcamera, OptionalJacobian< 2, FixedDimension< POINT >::value > Dpoint) const
Templated projection of a 3D point or a point at infinity into the image.
Definition PinholeCamera.h:233
Vector defaultErrorWhenTriangulatingBehindCamera() const
for Nonlinear Triangulation
Definition PinholeCamera.h:326
Point2 project2(const Unit3 &pw, OptionalJacobian< 2, dimension > Dcamera={}, OptionalJacobian< 2, 2 > Dpoint={}) const
project a point at infinity from world coordinates into the image
Definition PinholeCamera.h:254
double range(const Point3 &point, OptionalJacobian< 1, dimension > Dcamera={}, OptionalJacobian< 1, 3 > Dpoint={}) const
Calculate range to a landmark.
Definition PinholeCamera.h:264
const Calibration & calibration() const override
return calibration
Definition PinholeCamera.h:183
const Pose3 & getPose(OptionalJacobian< 6, dimension > H) const
return pose, with derivative
Definition PinholeCamera.h:174
PinholeCamera(const Pose3 &pose)
constructor with pose
Definition PinholeCamera.h:66
static PinholeCamera Level(const Pose2 &pose2, double height)
PinholeCamera::level with default calibration.
Definition PinholeCamera.h:92
PinholeCamera(const Pose3 &pose, const Calibration &K)
constructor with pose and calibration
Definition PinholeCamera.h:71
static PinholeCamera Level(const Calibration &K, const Pose2 &pose2, double height)
Create a level camera at the given 2D pose and height.
Definition PinholeCamera.h:86
double range(const PinholeCamera< CalibrationB > &camera, OptionalJacobian< 1, dimension > Dcamera={}, OptionalJacobian< 1, 6+CalibrationB::dimension > Dother={}) const
Calculate range to another camera.
Definition PinholeCamera.h:293
static PinholeCamera Identity()
for Canonical
Definition PinholeCamera.h:219
double range(const Pose3 &pose, OptionalJacobian< 1, dimension > Dcamera={}, OptionalJacobian< 1, 6 > Dpose={}) const
Calculate range to another pose.
Definition PinholeCamera.h:278
bool equals(const Base &camera, double tol=1e-9) const
assert equality up to a tolerance
Definition PinholeCamera.h:145
double range(const CalibratedCamera &camera, OptionalJacobian< 1, dimension > Dcamera={}, OptionalJacobian< 1, 6 > Dother={}) const
Calculate range to a calibrated camera.
Definition PinholeCamera.h:314
static constexpr auto dimension
Definition PinholeCamera.h:56
PinholeCamera(const Vector &v, const Vector &K)
Init from Vector and calibration.
Definition PinholeCamera.h:136
static PinholeCamera Lookat(const Point3 &eye, const Point3 &target, const Point3 &upVector, const Calibration &K=Calibration())
Create a camera at the given eye position looking at a target point in the scene with the specified u...
Definition PinholeCamera.h:105
Matrix34 cameraProjectionMatrix() const
for Linear Triangulation
Definition PinholeCamera.h:321
VectorK6 localCoordinates(const PinholeCamera &T2) const
return canonical coordinate
Definition PinholeCamera.h:211
PinholeCamera retract(const Vector &d) const
move a cameras according to d
Definition PinholeCamera.h:202
const Pose3 & pose() const
Definition PinholeCamera.h:169
PinholeCamera(const Vector &v)
Init from vector, can be 6D (default calibration) or dim.
Definition PinholeCamera.h:129
PinholeCamera()
default constructor
Definition PinholeCamera.h:62
Point2 Measurement
Some classes template on either PinholeCamera or StereoCamera, and this typedef informs those classes...
Definition PinholeCamera.h:42
PinholeBaseK()
Definition PinholePose.h:50
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}, OptionalJacobian< 2, DimK > Dcal={}) const
Definition PinholePose.h:112
A 2D pose (Point2,Rot2).
Definition Pose2.h:39
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
double range(const Point3 &point, OptionalJacobian< 1, 6 > Hself={}, OptionalJacobian< 1, 3 > Hpoint={}) const
Calculate range to a landmark.
Definition Pose3.cpp:232
Represents a 3D point on a unit sphere.
Definition Unit3.h:44