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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020