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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST