MRPT  2.0.4
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-2020, 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 #include <functional> // hash
16 
17 namespace mrpt::img
18 {
19 /** Parameters for the Brown-Conrady camera lens distortion model.
20  * The parameters obtained for one camera resolution can be used for any other
21  * resolution by means of the method TCamera::scaleToResolution()
22  *
23  * \sa The application camera-calib-gui for calibrating a camera
24  * \ingroup mrpt_img_grp
25  */
27 {
29 
30  // This must be added for declaration of MEX-related functions
32 
33  public:
34  TCamera();
35 
36  /** @name Camera parameters
37  @{ */
38 
39  /** Camera resolution */
40  uint32_t ncols{640}, nrows{480};
41 
42  /** Matrix of intrinsic parameters (containing the focal length and
43  * principal point coordinates):
44  *
45  * [ fx 0 cx ]
46  * A = [ 0 fy cy ]
47  * [ 0 0 1 ]
48  *
49  */
51  /** [k1 k2 t1 t2 k3 k4 k5 k6] -> k_i: parameters of radial distortion, t_i:
52  * parameters of tangential distortion (default=0) */
53  std::array<double, 8> dist{{.0, .0, .0, .0, .0, .0, .0, .0}};
54  /** The focal length of the camera, in meters (can be used among
55  * 'intrinsicParams' to determine the pixel size). */
56  double focalLengthMeters{.0};
57 
58  /** @} */
59 
60  /** Rescale all the parameters for a new camera resolution (it raises an
61  * exception if the aspect ratio is modified, which is not permitted).
62  */
63  void scaleToResolution(unsigned int new_ncols, unsigned int new_nrows);
64 
65  /** Save as a config block:
66  * \code
67  * [SECTION]
68  * resolution = [NCOLS NROWS]
69  * cx = CX
70  * cy = CY
71  * fx = FX
72  * fy = FY
73  * dist = [K1 K2 T1 T2 K3]
74  * focal_length = FOCAL_LENGTH
75  * \endcode
76  */
77  void saveToConfigFile(
78  const std::string& section, mrpt::config::CConfigFileBase& cfg) const;
79 
80  /** Load all the params from a config source, in the format used in
81  * saveToConfigFile(), that is:
82  *
83  * \code
84  * [SECTION]
85  * resolution = [NCOLS NROWS]
86  * cx = CX
87  * cy = CY
88  * fx = FX
89  * fy = FY
90  * dist = [K1 K2 T1 T2 K3]
91  * focal_length = FOCAL_LENGTH [optional field]
92  * \endcode
93  * \exception std::exception on missing fields
94  */
95  void loadFromConfigFile(
96  const std::string& section, const mrpt::config::CConfigFileBase& cfg);
97  /** overload This signature is consistent with the rest of MRPT APIs */
98  inline void loadFromConfigFile(
99  const mrpt::config::CConfigFileBase& cfg, const std::string& section)
100  {
101  loadFromConfigFile(section, cfg);
102  }
103 
104  /** Dumps all the parameters as a multi-line string, with the same format
105  * than \a saveToConfigFile. \sa saveToConfigFile */
106  std::string dumpAsText() const;
107 
108  /** Set the matrix of intrinsic params of the camera from the individual
109  * values of focal length and principal point coordinates (in pixels)
110  */
112  double fx, double fy, double cx, double cy)
113  {
115  intrinsicParams(0, 0) = fx;
116  intrinsicParams(1, 1) = fy;
117  intrinsicParams(0, 2) = cx;
118  intrinsicParams(1, 2) = cy;
119  intrinsicParams(2, 2) = 1.0;
120  }
121 
122  /** Get the vector of distortion params of the camera */
124  mrpt::math::CMatrixDouble15& distParVector) const
125  {
126  for (size_t i = 0; i < distParVector.size(); i++)
127  distParVector(0, i) = dist[i];
128  }
129 
130  /** Get a vector with the distortion params of the camera */
131  inline std::vector<double> getDistortionParamsAsVector() const
132  {
133  std::vector<double> v(8);
134  for (size_t i = 0; i < 8; i++) v[i] = dist[i];
135  return v;
136  }
137 
138  /** Set the whole vector of distortion params of the camera */
140  const mrpt::math::CMatrixDouble15& distParVector)
141  {
142  dist.fill(0);
143  for (size_t i = 0; i < 5; i++) dist[i] = distParVector(0, i);
144  }
145 
146  /** Set the whole vector of distortion params of the camera from a 4, 5, or
147  * 8-vector (see definition of \a dist for parameter order) */
148  template <class VECTORLIKE>
149  void setDistortionParamsVector(const VECTORLIKE& distParVector)
150  {
151  auto N = static_cast<size_t>(distParVector.size());
152  ASSERT_(N == 4 || N == 5 || N == 8);
153  dist.fill(0); // Default values
154  for (size_t i = 0; i < N; i++) dist[i] = distParVector[i];
155  }
156 
157  /** Set the vector of distortion params of the camera from the individual
158  * values of the distortion coefficients
159  */
161  double k1, double k2, double p1, double p2, double k3 = 0)
162  {
163  dist[0] = k1;
164  dist[1] = k2;
165  dist[2] = p1;
166  dist[3] = p2;
167  dist[4] = k3;
168  }
169 
170  /** Get the value of the principal point x-coordinate (in pixels). */
171  inline double cx() const { return intrinsicParams(0, 2); }
172  /** Get the value of the principal point y-coordinate (in pixels). */
173  inline double cy() const { return intrinsicParams(1, 2); }
174  /** Get the value of the focal length x-value (in pixels). */
175  inline double fx() const { return intrinsicParams(0, 0); }
176  /** Get the value of the focal length y-value (in pixels). */
177  inline double fy() const { return intrinsicParams(1, 1); }
178  /** Set the value of the principal point x-coordinate (in pixels). */
179  inline void cx(double val) { intrinsicParams(0, 2) = val; }
180  /** Set the value of the principal point y-coordinate (in pixels). */
181  inline void cy(double val) { intrinsicParams(1, 2) = val; }
182  /** Set the value of the focal length x-value (in pixels). */
183  inline void fx(double val) { intrinsicParams(0, 0) = val; }
184  /** Set the value of the focal length y-value (in pixels). */
185  inline void fy(double val) { intrinsicParams(1, 1) = val; }
186  /** Get the value of the k1 distortion parameter. */
187  inline double k1() const { return dist[0]; }
188  /** Get the value of the k2 distortion parameter. */
189  inline double k2() const { return dist[1]; }
190  /** Get the value of the p1 distortion parameter. */
191  inline double p1() const { return dist[2]; }
192  /** Get the value of the p2 distortion parameter. */
193  inline double p2() const { return dist[3]; }
194  /** Get the value of the k3 distortion parameter. */
195  inline double k3() const { return dist[4]; }
196  /** Get the value of the k4 distortion parameter. */
197  inline double k4() const { return dist[5]; }
198  /** Get the value of the k5 distortion parameter. */
199  inline double k5() const { return dist[6]; }
200  /** Get the value of the k6 distortion parameter. */
201  inline double k6() const { return dist[7]; }
202 
203  /** Set the value of the k1 distortion parameter. */
204  inline void k1(double val) { dist[0] = val; }
205  /** Set the value of the k2 distortion parameter. */
206  inline void k2(double val) { dist[1] = val; }
207  /** Set the value of the p1 distortion parameter. */
208  inline void p1(double val) { dist[2] = val; }
209  /** Set the value of the p2 distortion parameter. */
210  inline void p2(double val) { dist[3] = val; }
211  /** Set the value of the k3 distortion parameter. */
212  inline void k3(double val) { dist[4] = val; }
213  /** Set the value of the k4 distortion parameter. */
214  inline void k4(double val) { dist[5] = val; }
215  /** Set the value of the k5 distortion parameter. */
216  inline void k5(double val) { dist[6] = val; }
217  /** Set the value of the k6 distortion parameter. */
218  inline void k6(double val) { dist[7] = val; }
219 
220 }; // end class TCamera
221 
222 bool operator==(const mrpt::img::TCamera& a, const mrpt::img::TCamera& b);
223 bool operator!=(const mrpt::img::TCamera& a, const mrpt::img::TCamera& b);
224 
225 } // namespace mrpt::img
226 // Add for declaration of mexplus::from template specialization
227 DECLARE_MEXPLUS_FROM(mrpt::img::TCamera) // Not working at the beginning?
228 
229 namespace std
230 {
231 template <>
232 struct hash<mrpt::img::TCamera>
233 {
234  size_t operator()(const mrpt::img::TCamera& k) const
235  {
236  size_t res = 17;
237  res = res * 31 + hash<double>()(k.cx());
238  res = res * 31 + hash<double>()(k.cy());
239  res = res * 31 + hash<double>()(k.fx());
240  res = res * 31 + hash<double>()(k.fy());
241  res = res * 31 + hash<uint32_t>()(k.ncols);
242  res = res * 31 + hash<uint32_t>()(k.nrows);
243  for (unsigned int i = 0; i < k.dist.size(); i++)
244  res = res * 31 + hash<double>()(k.dist[i]);
245  return res;
246  }
247 };
248 
249 } // namespace std
double k3() const
Get the value of the k3 distortion parameter.
Definition: TCamera.h:195
uint32_t nrows
Definition: TCamera.h:40
void k2(double val)
Set the value of the k2 distortion parameter.
Definition: TCamera.h:206
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:98
constexpr matrix_size_t size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
Definition: CMatrixFixed.h:233
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:203
double fx() const
Get the value of the focal length x-value (in pixels).
Definition: TCamera.h:175
double k6() const
Get the value of the k6 distortion parameter.
Definition: TCamera.h:201
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:160
void p2(double val)
Set the value of the p2 distortion parameter.
Definition: TCamera.h:210
double fy() const
Get the value of the focal length y-value (in pixels).
Definition: TCamera.h:177
void fy(double val)
Set the value of the focal length y-value (in pixels).
Definition: TCamera.h:185
STL namespace.
std::string dumpAsText() const
Dumps all the parameters as a multi-line string, with the same format than saveToConfigFile.
Definition: TCamera.cpp:34
void k1(double val)
Set the value of the k1 distortion parameter.
Definition: TCamera.h:204
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:165
double k2() const
Get the value of the k2 distortion parameter.
Definition: TCamera.h:189
void k3(double val)
Set the value of the k3 distortion parameter.
Definition: TCamera.h:212
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates): ...
Definition: TCamera.h:50
#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:56
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:173
#define DECLARE_MEX_CONVERSION
This must be inserted if a custom conversion method for MEX API is implemented in the class...
DECLARE_MEXPLUS_FROM(mrpt::img::TCamera) namespace std
Definition: TCamera.h:227
void setDistortionParamsVector(const mrpt::math::CMatrixDouble15 &distParVector)
Set the whole vector of distortion params of the camera.
Definition: TCamera.h:139
void getDistortionParamsVector(mrpt::math::CMatrixDouble15 &distParVector) const
Get the vector of distortion params of the camera.
Definition: TCamera.h:123
int val
Definition: mrpt_jpeglib.h:957
Parameters for the Brown-Conrady camera lens distortion model.
Definition: TCamera.h:26
void cy(double val)
Set the value of the principal point y-coordinate (in pixels).
Definition: TCamera.h:181
double p1() const
Get the value of the p1 distortion parameter.
Definition: TCamera.h:191
std::array< double, 8 > dist
[k1 k2 t1 t2 k3 k4 k5 k6] -> k_i: parameters of radial distortion, t_i: parameters of tangential dist...
Definition: TCamera.h:53
void k4(double val)
Set the value of the k4 distortion parameter.
Definition: TCamera.h:214
double k5() const
Get the value of the k5 distortion parameter.
Definition: TCamera.h:199
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:231
double cx() const
Get the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:171
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void k6(double val)
Set the value of the k6 distortion parameter.
Definition: TCamera.h:218
void saveToConfigFile(const std::string &section, mrpt::config::CConfigFileBase &cfg) const
Save as a config block:
Definition: TCamera.cpp:143
void setDistortionParamsVector(const VECTORLIKE &distParVector)
Set the whole vector of distortion params of the camera from a 4, 5, or 8-vector (see definition of d...
Definition: TCamera.h:149
void fx(double val)
Set the value of the focal length x-value (in pixels).
Definition: TCamera.h:183
double k4() const
Get the value of the k4 distortion parameter.
Definition: TCamera.h:197
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
void p1(double val)
Set the value of the p1 distortion parameter.
Definition: TCamera.h:208
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void cx(double val)
Set the value of the principal point x-coordinate (in pixels).
Definition: TCamera.h:179
double p2() const
Get the value of the p2 distortion parameter.
Definition: TCamera.h:193
double k1() const
Get the value of the k1 distortion parameter.
Definition: TCamera.h:187
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:111
bool operator!=(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:238
uint32_t ncols
Camera resolution.
Definition: TCamera.h:40
void k5(double val)
Set the value of the k5 distortion parameter.
Definition: TCamera.h:216
std::vector< double > getDistortionParamsAsVector() const
Get a vector with the distortion params of the camera.
Definition: TCamera.h:131



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020