Main MRPT website > C++ reference
MRPT logo
TCamera.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef TCamera_H
29 #define TCamera_H
30 
36 #include <mrpt/poses/CPose3DQuat.h>
37 
38 namespace mrpt
39 {
40  namespace utils
41  {
42  using namespace mrpt::math;
43  using namespace mrpt::poses;
44 
46 
47  /** Structure to hold the parameters of a pinhole camera model.
48  * The parameters obtained for one camera resolution can be used for any other resolution by means of the method TCamera::scaleToResolution()
49  *
50  * \sa mrpt::vision::CCamModel, the application <a href="http://www.mrpt.org/Application:camera-calib-gui" >camera-calib-gui</a> for calibrating a camera
51  * \ingroup mrpt_base_grp
52  */
53  class BASE_IMPEXP TCamera : public mrpt::utils::CSerializable
54  {
56 
57  public:
58  TCamera() : ncols(640), nrows(480), focalLengthMeters(0)
59  {
60  intrinsicParams.set_unsafe(0,0,507.808);
61  intrinsicParams.set_unsafe(1,1,507.808);
62  intrinsicParams.set_unsafe(0,2,356.2368);
63  intrinsicParams.set_unsafe(1,2,252.9216);
64  intrinsicParams.set_unsafe(2,2,1);
65  for (size_t i=0;i<dist.SizeAtCompileTime ;i++)
66  dist[i] = 0;
67  }
68 
69  /** @name Camera parameters
70  @{ */
71 
72  uint32_t ncols,nrows; //!< Camera resolution
73  CMatrixDouble33 intrinsicParams; //!< Matrix of intrinsic parameters (containing the focal length and principal point coordinates)
74  CArrayDouble<5> dist; //!< [k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (default=0)
75  double focalLengthMeters; //!< The focal length of the camera, in meters (can be used among 'intrinsicParams' to determine the pixel size).
76 
77  /** @} */
78 
79  /** Rescale all the parameters for a new camera resolution (it raises an exception if the aspect ratio is modified, which is not permitted).
80  */
81  void scaleToResolution(unsigned int new_ncols, unsigned int new_nrows);
82 
83  /** Save as a config block:
84  * \code
85  * [SECTION]
86  * resolution = [NCOLS NROWS]
87  * cx = CX
88  * cy = CY
89  * fx = FX
90  * fy = FY
91  * dist = [K1 K2 T1 T2 K3]
92  * focal_length = FOCAL_LENGTH
93  * \endcode
94  */
95  void saveToConfigFile( const std::string &section, mrpt::utils::CConfigFileBase &cfg ) const;
96 
97  /** Load all the params from a config source, in the format used in saveToConfigFile(), that is:
98  *
99  * \code
100  * [SECTION]
101  * resolution = [NCOLS NROWS]
102  * cx = CX
103  * cy = CY
104  * fx = FX
105  * fy = FY
106  * dist = [K1 K2 T1 T2 K3]
107  * focal_length = FOCAL_LENGTH [optional field]
108  * \endcode
109  * \exception std::exception on missing fields
110  */
111  void loadFromConfigFile(const std::string &section, const mrpt::utils::CConfigFileBase &cfg );
112 
113  /** Dumps all the parameters as a multi-line string, with the same format than \a saveToConfigFile. \sa saveToConfigFile */
114  std::string dumpAsText() const;
115 
116 
117  /** Set the matrix of intrinsic params of the camera from the individual values of focal length and principal point coordinates (in pixels)
118  */
119  inline void setIntrinsicParamsFromValues ( double fx, double fy, double cx, double cy )
120  {
121  intrinsicParams.set_unsafe( 0, 0, fx );
122  intrinsicParams.set_unsafe( 1, 1, fy );
123  intrinsicParams.set_unsafe( 0, 2, cx );
124  intrinsicParams.set_unsafe( 1, 2, cy );
125  }
126 
127  /** Get the vector of distortion params of the camera */
128  inline void getDistortionParamsVector ( CMatrixDouble15 &distParVector ) const
129  {
130  for (size_t i=0;i<5;i++)
131  distParVector.set_unsafe(0,i, dist[i]);
132  }
133 
134  /** Get a vector with the distortion params of the camera */
135  inline std::vector<double> getDistortionParamsAsVector () const {
136  std::vector<double> v(5);
137  for (size_t i=0;i<5;i++)
138  v[i] = dist[i];
139  return v;
140  }
141 
142  /** Set the whole vector of distortion params of the camera */
143  void setDistortionParamsVector( const CMatrixDouble15 &distParVector )
144  {
145  for (size_t i=0;i<5;i++)
146  dist[i] = distParVector.get_unsafe(0,i);
147  }
148 
149  /** Set the whole vector of distortion params of the camera from a 4 or 5-vector */
150  template <class VECTORLIKE>
151  void setDistortionParamsVector( const VECTORLIKE &distParVector )
152  {
153  ASSERT_(distParVector.size()==4 || distParVector.size()==5)
154  dist[4] = 0; // Default value
155  for (typename VECTORLIKE::Index i=0;i<distParVector.size();i++)
156  dist[i] = distParVector[i];
157  }
158 
159  /** Set the vector of distortion params of the camera from the individual values of the distortion coefficients
160  */
161  inline void setDistortionParamsFromValues( 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 
179  /** Set the value of the principal point x-coordinate (in pixels). */
180  inline void cx(double val) { intrinsicParams(0,2)=val; }
181  /** Set the value of the principal point y-coordinate (in pixels). */
182  inline void cy(double val) { intrinsicParams(1,2)=val; }
183  /** Set the value of the focal length x-value (in pixels). */
184  inline void fx(double val) { intrinsicParams(0,0)=val; }
185  /** Set the value of the focal length y-value (in pixels). */
186  inline void fy(double val) { intrinsicParams(1,1)=val; }
187 
188  /** Get the value of the k1 distortion parameter. */
189  inline double k1() const { return dist[0]; }
190  /** Get the value of the k2 distortion parameter. */
191  inline double k2() const { return dist[1]; }
192  /** Get the value of the p1 distortion parameter. */
193  inline double p1() const { return dist[2]; }
194  /** Get the value of the p2 distortion parameter. */
195  inline double p2() const { return dist[3]; }
196  /** Get the value of the k3 distortion parameter. */
197  inline double k3() const { return dist[4]; }
198 
199  /** Get the value of the k1 distortion parameter. */
200  inline void k1(double val) { dist[0]=val; }
201  /** Get the value of the k2 distortion parameter. */
202  inline void k2(double val) { dist[1]=val; }
203  /** Get the value of the p1 distortion parameter. */
204  inline void p1(double val) { dist[2]=val; }
205  /** Get the value of the p2 distortion parameter. */
206  inline void p2(double val) { dist[3]=val; }
207  /** Get the value of the k3 distortion parameter. */
208  inline void k3(double val) { dist[4]=val; }
209  }; // end class TCamera
210 
211 
214 
215  } // End of namespace
216 } // end of namespace
217 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013