MRPT  1.9.9
TCamera.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/math/CMatrixFixed.h>
14 #include <array>
15 
16 namespace mrpt::img
17 {
18 /** Structure to hold the parameters of a pinhole camera model.
19  * The parameters obtained for one camera resolution can be used for any other
20  * resolution by means of the method TCamera::scaleToResolution()
21  *
22  * \sa The application camera-calib-gui for calibrating a camera
23  * \ingroup mrpt_img_grp
24  */
26 {
28 
29  // This must be added for declaration of MEX-related functions
31 
32  public:
33  /** @name Camera parameters
34  @{ */
35 
36  /** Camera resolution */
37  uint32_t ncols{640}, nrows{480};
38  /** Matrix of intrinsic parameters (containing the focal length and
39  * principal point coordinates) */
41  /** [k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i:
42  * parameters of tangential distortion (default=0) */
43  std::array<double, 5> dist{{.0, .0, .0, .0, .0}};
44  /** The focal length of the camera, in meters (can be used among
45  * 'intrinsicParams' to determine the pixel size). */
46  double focalLengthMeters{.0};
47 
48  /** @} */
49 
50  /** Rescale all the parameters for a new camera resolution (it raises an
51  * exception if the aspect ratio is modified, which is not permitted).
52  */
53  void scaleToResolution(unsigned int new_ncols, unsigned int new_nrows);
54 
55  /** Save as a config block:
56  * \code
57  * [SECTION]
58  * resolution = [NCOLS NROWS]
59  * cx = CX
60  * cy = CY
61  * fx = FX
62  * fy = FY
63  * dist = [K1 K2 T1 T2 K3]
64  * focal_length = FOCAL_LENGTH
65  * \endcode
66  */
67  void saveToConfigFile(
68  const std::string& section, mrpt::config::CConfigFileBase& cfg) const;
69 
70  /** Load all the params from a config source, in the format used in
71  * saveToConfigFile(), that is:
72  *
73  * \code
74  * [SECTION]
75  * resolution = [NCOLS NROWS]
76  * cx = CX
77  * cy = CY
78  * fx = FX
79  * fy = FY
80  * dist = [K1 K2 T1 T2 K3]
81  * focal_length = FOCAL_LENGTH [optional field]
82  * \endcode
83  * \exception std::exception on missing fields
84  */
85  void loadFromConfigFile(
86  const std::string& section, const mrpt::config::CConfigFileBase& cfg);
87  /** overload This signature is consistent with the rest of MRPT APIs */
88  inline void loadFromConfigFile(
89  const mrpt::config::CConfigFileBase& cfg, const std::string& section)
90  {
91  loadFromConfigFile(section, cfg);
92  }
93 
94  /** Dumps all the parameters as a multi-line string, with the same format
95  * than \a saveToConfigFile. \sa saveToConfigFile */
96  std::string dumpAsText() const;
97 
98  /** Set the matrix of intrinsic params of the camera from the individual
99  * values of focal length and principal point coordinates (in pixels)
100  */
102  double fx, double fy, double cx, double cy)
103  {
104  intrinsicParams(0, 0) = fx;
105  intrinsicParams(1, 1) = fy;
106  intrinsicParams(0, 2) = cx;
107  intrinsicParams(1, 2) = cy;
108  }
109 
110  /** Get the vector of distortion params of the camera */
112  mrpt::math::CMatrixDouble15& distParVector) const
113  {
114  for (size_t i = 0; i < 5; i++) distParVector(0, i) = dist[i];
115  }
116 
117  /** Get a vector with the distortion params of the camera */
118  inline std::vector<double> getDistortionParamsAsVector() const
119  {
120  std::vector<double> v(5);
121  for (size_t i = 0; i < 5; i++) v[i] = dist[i];
122  return v;
123  }
124 
125  /** Set the whole vector of distortion params of the camera */
127  const mrpt::math::CMatrixDouble15& distParVector)
128  {
129  for (size_t i = 0; i < 5; i++) dist[i] = distParVector(0, i);
130  }
131 
132  /** Set the whole vector of distortion params of the camera from a 4 or
133  * 5-vector */
134  template <class VECTORLIKE>
135  void setDistortionParamsVector(const VECTORLIKE& distParVector)
136  {
137  auto N = static_cast<size_t>(distParVector.size());
138  ASSERT_(N == 4 || N == 5);
139  dist[4] = 0; // Default value
140  for (size_t i = 0; i < N; i++) dist[i] = distParVector[i];
141  }
142 
143  /** Set the vector of distortion params of the camera from the individual
144  * values of the distortion coefficients
145  */
147  double k1, double k2, double p1, double p2, double k3 = 0)
148  {
149  dist[0] = k1;
150  dist[1] = k2;
151  dist[2] = p1;
152  dist[3] = p2;
153  dist[4] = k3;
154  }
155 
156  /** Get the value of the principal point x-coordinate (in pixels). */
157  inline double cx() const { return intrinsicParams(0, 2); }
158  /** Get the value of the principal point y-coordinate (in pixels). */
159  inline double cy() const { return intrinsicParams(1, 2); }
160  /** Get the value of the focal length x-value (in pixels). */
161  inline double fx() const { return intrinsicParams(0, 0); }
162  /** Get the value of the focal length y-value (in pixels). */
163  inline double fy() const { return intrinsicParams(1, 1); }
164  /** Set the value of the principal point x-coordinate (in pixels). */
165  inline void cx(double val) { intrinsicParams(0, 2) = val; }
166  /** Set the value of the principal point y-coordinate (in pixels). */
167  inline void cy(double val) { intrinsicParams(1, 2) = val; }
168  /** Set the value of the focal length x-value (in pixels). */
169  inline void fx(double val) { intrinsicParams(0, 0) = val; }
170  /** Set the value of the focal length y-value (in pixels). */
171  inline void fy(double val) { intrinsicParams(1, 1) = val; }
172  /** Get the value of the k1 distortion parameter. */
173  inline double k1() const { return dist[0]; }
174  /** Get the value of the k2 distortion parameter. */
175  inline double k2() const { return dist[1]; }
176  /** Get the value of the p1 distortion parameter. */
177  inline double p1() const { return dist[2]; }
178  /** Get the value of the p2 distortion parameter. */
179  inline double p2() const { return dist[3]; }
180  /** Get the value of the k3 distortion parameter. */
181  inline double k3() const { return dist[4]; }
182  /** Get the value of the k1 distortion parameter. */
183  inline void k1(double val) { dist[0] = val; }
184  /** Get the value of the k2 distortion parameter. */
185  inline void k2(double val) { dist[1] = val; }
186  /** Get the value of the p1 distortion parameter. */
187  inline void p1(double val) { dist[2] = val; }
188  /** Get the value of the p2 distortion parameter. */
189  inline void p2(double val) { dist[3] = val; }
190  /** Get the value of the k3 distortion parameter. */
191  inline void k3(double val) { dist[4] = val; }
192 }; // end class TCamera
193 
194 bool operator==(const mrpt::img::TCamera& a, const mrpt::img::TCamera& b);
195 bool operator!=(const mrpt::img::TCamera& a, const mrpt::img::TCamera& b);
196 
197 } // namespace mrpt::img
198 // Add for declaration of mexplus::from template specialization
199 DECLARE_MEXPLUS_FROM(mrpt::img::TCamera) // Not working at the beginning?
double k3() const
Get the value of the k3 distortion parameter.
Definition: TCamera.h:181
uint32_t nrows
Definition: TCamera.h:37
void k2(double val)
Get the value of the k2 distortion parameter.
Definition: TCamera.h:185
void loadFromConfigFile(const mrpt::config::CConfigFileBase &cfg, const std::string &section)
overload This signature is consistent with the rest of MRPT APIs
Definition: TCamera.h:88
void scaleToResolution(unsigned int new_ncols, unsigned int new_nrows)
Rescale all the parameters for a new camera resolution (it raises an exception if the aspect ratio is...
Definition: TCamera.cpp:174
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:161
void setDistortionParamsFromValues(double k1, double k2, double p1, double p2, double k3=0)
Set the vector of distortion params of the camera from the individual values of the distortion coeffi...
Definition: TCamera.h:146
#define DECLARE_MEXPLUS_FROM(complete_type)
This must be inserted if a custom conversion method for MEX API is implemented in the class...
void p2(double val)
Get the value of the p2 distortion parameter.
Definition: TCamera.h:189
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:163
void fy(double val)
Set the value of the focal length y-value (in pixels).
Definition: TCamera.h:171
std::string dumpAsText() const
Dumps all the parameters as a multi-line string, with the same format than saveToConfigFile.
Definition: TCamera.cpp:28
void k1(double val)
Get the value of the k1 distortion parameter.
Definition: TCamera.h:183
void loadFromConfigFile(const std::string &section, const mrpt::config::CConfigFileBase &cfg)
Load all the params from a config source, in the format used in saveToConfigFile(), that is:
Definition: TCamera.cpp:136
double k2() const
Get the value of the k2 distortion parameter.
Definition: TCamera.h:175
void k3(double val)
Get the value of the k3 distortion parameter.
Definition: TCamera.h:191
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates) ...
Definition: TCamera.h:40
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
double focalLengthMeters
The focal length of the camera, in meters (can be used among &#39;intrinsicParams&#39; to determine the pixel...
Definition: TCamera.h:46
This class allows loading and storing values and vectors of different types from a configuration text...
double cy() const
Get the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:159
#define DECLARE_MEX_CONVERSION
This must be inserted if a custom conversion method for MEX API is implemented in the class...
void setDistortionParamsVector(const mrpt::math::CMatrixDouble15 &distParVector)
Set the whole vector of distortion params of the camera.
Definition: TCamera.h:126
void getDistortionParamsVector(mrpt::math::CMatrixDouble15 &distParVector) const
Get the vector of distortion params of the camera.
Definition: TCamera.h:111
int val
Definition: mrpt_jpeglib.h:957
GLubyte GLubyte b
Definition: glext.h:6372
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:25
void cy(double val)
Set the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:167
GLsizei const GLchar ** string
Definition: glext.h:4116
double p1() const
Get the value of the p1 distortion parameter.
Definition: TCamera.h:177
std::array< double, 5 > dist
[k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (d...
Definition: TCamera.h:43
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:202
double cx() const
Get the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:157
const GLdouble * v
Definition: glext.h:3684
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void saveToConfigFile(const std::string &section, mrpt::config::CConfigFileBase &cfg) const
Save as a config block:
Definition: TCamera.cpp:115
void setDistortionParamsVector(const VECTORLIKE &distParVector)
Set the whole vector of distortion params of the camera from a 4 or 5-vector.
Definition: TCamera.h:135
void fx(double val)
Set the value of the focal length x-value (in pixels).
Definition: TCamera.h:169
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
void p1(double val)
Get the value of the p1 distortion parameter.
Definition: TCamera.h:187
void cx(double val)
Set the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:165
double p2() const
Get the value of the p2 distortion parameter.
Definition: TCamera.h:179
double k1() const
Get the value of the k1 distortion parameter.
Definition: TCamera.h:173
void setIntrinsicParamsFromValues(double fx, double fy, double cx, double cy)
Set the matrix of intrinsic params of the camera from the individual values of focal length and princ...
Definition: TCamera.h:101
unsigned __int32 uint32_t
Definition: rptypes.h:50
bool operator!=(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:209
GLubyte GLubyte GLubyte a
Definition: glext.h:6372
uint32_t ncols
Camera resolution.
Definition: TCamera.h:37
std::vector< double > getDistortionParamsAsVector() const
Get a vector with the distortion params of the camera.
Definition: TCamera.h:118



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019