Main MRPT website > C++ reference for MRPT 1.9.9
TCamera.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, 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 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/TCamera.h>
14 #include <mrpt/math/matrix_serialization.h> // For "<<" ">>" operators.
15 #include <mrpt/math/utils_matlab.h>
16 
17 using namespace mrpt::utils;
18 using namespace mrpt::math;
19 using namespace std;
20 
21 /* Implements serialization for the TCamera struct as it will be included within
22  * CObservations objects */
24 
25 /** Dumps all the parameters as a multi-line string, with the same format than
26  * \a saveToConfigFile. \sa saveToConfigFile */
27 std::string TCamera::dumpAsText() const
28 {
30  saveToConfigFile("", cfg);
31  return cfg.getContent();
32 }
33 
34 // WriteToStream
35 void TCamera::writeToStream(CStream& out, int* version) const
36 {
37  if (version)
38  *version = 2;
39  else
40  {
41  out << focalLengthMeters;
42  for (unsigned int k = 0; k < 5; k++) out << dist[k];
43  out << intrinsicParams;
44  // version 0 did serialize here a "CMatrixDouble15"
45  out << nrows << ncols; // New in v2
46  } // end else
47 }
48 
49 // ReadFromStream
50 void TCamera::readFromStream(CStream& in, int version)
51 {
52  switch (version)
53  {
54  case 0:
55  case 1:
56  case 2:
57  {
58  in >> focalLengthMeters;
59 
60  for (unsigned int k = 0; k < 5; k++) in >> dist[k];
61 
62  in >> intrinsicParams;
63 
64  if (version == 0)
65  {
66  CMatrixDouble15 __distortionParams;
67  in >> __distortionParams;
68  }
69 
70  if (version >= 2)
71  in >> nrows >> ncols;
72  else
73  {
74  nrows = 480;
75  ncols = 640;
76  }
77  }
78  break;
79  default:
81  }
82 }
83 
84 /*---------------------------------------------------------------
85  Implements the writing to a mxArray for Matlab
86  ---------------------------------------------------------------*/
87 #if MRPT_HAS_MATLAB
88 // Add to implement mexplus::from template specialization
90 
92 {
93  const char* fields[] = {"K", "dist", "f", "ncols", "nrows"};
94  mexplus::MxArray params_struct(
95  mexplus::MxArray::Struct(sizeof(fields) / sizeof(fields[0]), fields));
96  params_struct.set("K", mrpt::math::convertToMatlab(this->intrinsicParams));
97  params_struct.set("dist", mrpt::math::convertToMatlab(this->dist));
98  params_struct.set("f", this->focalLengthMeters);
99  params_struct.set("ncols", this->ncols);
100  params_struct.set("nrows", this->nrows);
101  return params_struct.release();
102 }
103 #endif
104 
105 /** Save as a config block:
106  * \code
107  * [SECTION]
108  * resolution = NCOLS NROWS
109  * cx = CX
110  * cy = CY
111  * fx = FX
112  * fy = FY
113  * dist = K1 K2 T1 T2 T3
114  * focal_length = FOCAL_LENGTH
115  * \endcode
116  */
118  const std::string& section, mrpt::utils::CConfigFileBase& cfg) const
119 {
120  cfg.write(
121  section, "resolution",
122  format("[%u %u]", (unsigned int)ncols, (unsigned int)nrows));
123  cfg.write(section, "cx", format("%.05f", cx()));
124  cfg.write(section, "cy", format("%.05f", cy()));
125  cfg.write(section, "fx", format("%.05f", fx()));
126  cfg.write(section, "fy", format("%.05f", fy()));
127  cfg.write(
128  section, "dist",
129  format(
130  "[%e %e %e %e %e]", dist[0], dist[1], dist[2], dist[3], dist[4]));
131  if (focalLengthMeters != 0)
132  cfg.write(section, "focal_length", focalLengthMeters);
133 }
134 
135 /** Load all the params from a config source, in the format described in
136  * saveToConfigFile()
137  */
139  const std::string& section, const mrpt::utils::CConfigFileBase& cfg)
140 {
141  vector<uint64_t> out_res;
142  cfg.read_vector(section, "resolution", vector<uint64_t>(), out_res, true);
143  if (out_res.size() != 2)
144  THROW_EXCEPTION("Expected 2-length vector in field 'resolution'");
145  ncols = out_res[0];
146  nrows = out_res[1];
147 
148  double fx, fy, cx, cy;
149  fx = cfg.read_double(section, "fx", 0, true);
150  fy = cfg.read_double(section, "fy", 0, true);
151  cx = cfg.read_double(section, "cx", 0, true);
152  cy = cfg.read_double(section, "cy", 0, true);
153 
154  if (fx < 2.0) fx *= ncols;
155  if (fy < 2.0) fy *= nrows;
156  if (cx < 2.0) cx *= ncols;
157  if (cy < 2.0) cy *= nrows;
158 
159  setIntrinsicParamsFromValues(fx, fy, cx, cy);
160 
161  CVectorDouble dists;
162  cfg.read_vector(section, "dist", CVectorDouble(), dists, true);
163  if (dists.size() != 4 && dists.size() != 5)
164  THROW_EXCEPTION("Expected 4 or 5-length vector in field 'dist'");
165 
166  dist.Constant(0);
167  for (CVectorDouble::Index i = 0; i < dists.size(); i++) dist[i] = dists[i];
168 
169  focalLengthMeters =
170  cfg.read_double(section, "focal_length", 0, false /* optional value */);
171 }
172 
173 /** Rescale all the parameters for a new camera resolution (it raises an
174  * exception if the aspect ratio is modified, which is not permitted).
175  */
176 void TCamera::scaleToResolution(unsigned int new_ncols, unsigned int new_nrows)
177 {
178  if (ncols == new_ncols && nrows == new_nrows) return; // already done
179 
180  ASSERT_(new_nrows > 0 && new_ncols > 0)
181 
182  const double prev_aspect_ratio = ncols / double(nrows);
183  const double new_aspect_ratio = new_ncols / double(new_nrows);
184 
185  ASSERTMSG_(
186  std::abs(prev_aspect_ratio - new_aspect_ratio) < 1e-3,
187  "TCamera: Trying to scale camera parameters for a resolution of "
188  "different aspect ratio.")
189 
190  const double K = new_ncols / double(ncols);
191 
192  ncols = new_ncols;
193  nrows = new_nrows;
194 
195  // fx fy cx cy
196  intrinsicParams(0, 0) *= K;
197  intrinsicParams(1, 1) *= K;
198  intrinsicParams(0, 2) *= K;
199  intrinsicParams(1, 2) *= K;
200 
201  // distortion params: unmodified.
202 }
203 
206 {
207  return a.ncols == b.ncols && a.nrows == b.nrows &&
208  a.intrinsicParams == b.intrinsicParams && a.dist == b.dist &&
209  a.focalLengthMeters == b.focalLengthMeters;
210 }
213 {
214  return !(a == b);
215 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
mxArray * convertToMatlab(const Eigen::EigenBase< Derived > &mat)
Convert vectors, arrays and matrices into Matlab vectors/matrices.
Definition: utils_matlab.h:38
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
#define THROW_EXCEPTION(msg)
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:176
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
STL namespace.
bool operator==(const mrpt::utils::TCamera &a, const mrpt::utils::TCamera &b)
Definition: TCamera.cpp:204
This class allows loading and storing values and vectors of different types from a configuration text...
This class implements a config file-like interface over a memory-stored string list.
void getContent(std::string &str) const
Return the current contents of the virtual "config file".
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
bool operator!=(const mrpt::utils::TCamera &a, const mrpt::utils::TCamera &b)
Definition: TCamera.cpp:211
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void read_vector(const std::string &section, const std::string &name, const VECTOR_TYPE &defaultValue, VECTOR_TYPE &outValues, bool failIfNotFound=false) const
Reads a configuration parameter of type vector, stored in the file as a string: "[v1 v2 v3 ...
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Definition: TCamera.cpp:50
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLubyte GLubyte b
Definition: glext.h:6279
#define IMPLEMENTS_MEXPLUS_FROM(complete_type)
GLsizei const GLchar ** string
Definition: glext.h:4101
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
Definition: TCamera.cpp:35
void write(const std::string &section, const std::string &name, enum_t value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
virtual mxArray * writeToMatlab() const
Introduces a pure virtual method responsible for writing to a mxArray Matlab object, typically a MATLAB struct whose contents are documented in each derived class.
Definition: CSerializable.h:89
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
This file implements matrix/vector text and binary serialization.
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
dynamic_vector< double > CVectorDouble
Column vector, like Eigen::MatrixXd, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
#define ASSERTMSG_(f, __ERROR_MSG)
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
void loadFromConfigFile(const std::string &section, const mrpt::utils::CConfigFileBase &cfg)
Load all the params from a config source, in the format used in saveToConfigFile(), that is:
Definition: TCamera.cpp:138
struct mxArray_tag mxArray
Forward declaration for mxArray (avoid #including as much as possible to speed up compiling) ...
Definition: CSerializable.h:19
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:32
void saveToConfigFile(const std::string &section, mrpt::utils::CConfigFileBase &cfg) const
Save as a config block:
Definition: TCamera.cpp:117



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019