Main MRPT website > C++ reference for MRPT 1.9.9
CObservationStereoImages.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 "obs-precomp.h" // Precompiled headers
11 
13 #include <mrpt/math/CMatrix.h>
14 #include <mrpt/utils/CStream.h>
15 #if MRPT_HAS_MATLAB
16 #include <mexplus/mxarray.h>
17 #endif
18 
19 using namespace mrpt::obs;
20 using namespace mrpt::math;
21 using namespace mrpt::utils;
22 using namespace mrpt::poses;
23 
24 // This must be added to any CSerializable class implementation file.
26 
27 /*---------------------------------------------------------------
28  Constructor
29  ---------------------------------------------------------------*/
31  void* iplImageLeft, void* iplImageRight, void* iplImageDisparity,
32  bool ownMemory)
33  : imageLeft(UNINITIALIZED_IMAGE),
34  imageRight(UNINITIALIZED_IMAGE),
35  imageDisparity(UNINITIALIZED_IMAGE),
36  hasImageDisparity(iplImageDisparity != nullptr),
37  hasImageRight(iplImageRight != nullptr)
38 {
39  if (iplImageLeft)
40  ownMemory ? imageLeft.setFromIplImage(iplImageLeft)
41  : imageLeft.loadFromIplImage(iplImageLeft);
42  if (iplImageRight)
43  ownMemory ? imageRight.setFromIplImage(iplImageRight)
44  : imageRight.loadFromIplImage(iplImageRight);
45  if (iplImageDisparity)
46  ownMemory ? imageDisparity.setFromIplImage(iplImageDisparity)
47  : imageDisparity.loadFromIplImage(iplImageDisparity);
48 }
49 
50 /*---------------------------------------------------------------
51  Default Constructor
52  ---------------------------------------------------------------*/
54  : hasImageDisparity(false), hasImageRight(true)
55 {
56 }
57 
58 /*---------------------------------------------------------------
59  Destructor
60  ---------------------------------------------------------------*/
62 /*---------------------------------------------------------------
63  Implements the writing to a CStream capability of CSerializable objects
64  ---------------------------------------------------------------*/
66  mrpt::utils::CStream& out, int* version) const
67 {
68  if (version)
69  *version = 6;
70  else
71  {
72  // The data
74 
76 
77  if (hasImageRight) out << imageRight;
78 
80 
81  out << timestamp;
82  out << rightCameraPose;
83  out << sensorLabel;
84  }
85 }
86 
87 /*---------------------------------------------------------------
88  Implements the reading from a CStream capability of CSerializable objects
89  ---------------------------------------------------------------*/
91  mrpt::utils::CStream& in, int version)
92 {
93  switch (version)
94  {
95  case 6:
96  {
98 
100 
101  if (hasImageRight) in >> imageRight;
102 
104 
105  in >> timestamp;
106  in >> rightCameraPose;
107  in >> sensorLabel;
108  }
109  break;
110 
111  case 0:
112  case 1:
113  case 2:
114  case 3:
115  case 4:
116  case 5:
117  {
118  // This, for backwards compatibility before version 6:
119  hasImageRight = true;
120  hasImageDisparity = false;
121 
122  if (version < 5)
123  {
124  CPose3D aux;
125  in >> aux;
126  cameraPose = CPose3DQuat(aux);
127  }
128 
129  if (version >= 5)
130  {
132  }
133  else
134  {
135  CMatrix intParams;
136  in >> intParams; // Get the intrinsic params
138  CMatrixDouble33(intParams); // Set them to both cameras
140  intParams); // ... distortion parameters are set to zero
141  }
142 
143  in >> imageLeft >> imageRight; // For all the versions
144 
145  if (version >= 1)
146  in >> timestamp;
147  else
148  timestamp = INVALID_TIMESTAMP; // For version 1 to 5
149  if (version >= 2)
150  {
151  if (version < 5)
152  {
153  CPose3D aux;
154  in >> aux;
156  }
157  else
158  in >> rightCameraPose;
159  }
160  else
162  0.10f, 0, 0, mrpt::math::CQuaternionDouble(
163  1, 0, 0, 0)); // For version 1 to 5
164 
165  if (version >= 3 && version < 5) // For versions 3 & 4
166  {
167  double foc;
168  in >> foc; // Get the focal length in meters
170  foc; // ... and set it to both cameras
171  }
172  else if (version < 3)
174  0.002; // For version 0, 1 & 2 (from version 5, this
175  // parameter is included in the TCamera objects)
176 
177  if (version >= 4)
178  in >> sensorLabel;
179  else
180  sensorLabel = ""; // For version 1 to 5
181  }
182  break;
183  default:
185  };
186 }
187 
188 /*---------------------------------------------------------------
189  Implements the writing to a mxArray for Matlab
190  ---------------------------------------------------------------*/
191 #if MRPT_HAS_MATLAB
192 // Add to implement mexplus::from template specialization
194 
196 {
197  const char* fields[] = {"class", "ts", "sensorLabel", "imageL",
198  "imageR", "poseL", "poseLR", "poseR",
199  "paramsL", "paramsR"};
200  mexplus::MxArray obs_struct(
201  mexplus::MxArray::Struct(sizeof(fields) / sizeof(fields[0]), fields));
202 
203  obs_struct.set("class", this->GetRuntimeClass()->className);
204  obs_struct.set("ts", this->timestamp);
205  obs_struct.set("sensorLabel", this->sensorLabel);
206  obs_struct.set("imageL", this->imageLeft);
207  obs_struct.set("imageR", this->imageRight);
208  obs_struct.set("poseL", this->cameraPose);
209  obs_struct.set("poseR", this->cameraPose + this->rightCameraPose);
210  obs_struct.set("poseLR", this->rightCameraPose);
211  obs_struct.set("paramsL", this->leftCamera);
212  obs_struct.set("paramsR", this->rightCamera);
213  return obs_struct.release();
214 }
215 #endif
216 
217 /** Populates a TStereoCamera structure with the parameters in \a leftCamera, \a
218  * rightCamera and \a rightCameraPose */
220  mrpt::utils::TStereoCamera& out_params) const
221 {
222  out_params.leftCamera = this->leftCamera;
223  out_params.rightCamera = this->rightCamera;
224  out_params.rightCameraPose = this->rightCameraPose;
225 }
226 
227 /** Sets \a leftCamera, \a rightCamera and \a rightCameraPose from a
228  * TStereoCamera structure */
230  const mrpt::utils::TStereoCamera& in_params)
231 {
232  this->leftCamera = in_params.leftCamera;
233  this->rightCamera = in_params.rightCamera;
234  this->rightCameraPose = in_params.rightCameraPose;
235 }
236 
237 /** This method only checks whether ALL the distortion parameters in \a
238  * leftCamera are set to zero, which is
239  * the convention in MRPT to denote that this pair of stereo images has been
240  * rectified.
241  */
243 {
244  return (leftCamera.dist.array() == 0).all();
245 }
246 
247 // Do an efficient swap of all data members of this object with "o".
249 {
251 
255 
256  std::swap(hasImageDisparity, o.hasImageDisparity);
257  std::swap(hasImageRight, o.hasImageRight);
258 
259  std::swap(leftCamera, o.leftCamera);
260  std::swap(rightCamera, o.rightCamera);
261 
262  std::swap(cameraPose, o.cameraPose);
263  std::swap(rightCameraPose, o.rightCameraPose);
264 }
265 
267 {
268  using namespace std;
270 
271  o << "Homogeneous matrix for the sensor's 3D pose, relative to robot "
272  "base:\n";
273  o << cameraPose.getHomogeneousMatrixVal() << endl
274  << "Camera pose: " << cameraPose << endl
275  << "Camera pose (YPR): " << CPose3D(cameraPose) << endl
276  << endl;
277 
279  getStereoCameraParams(stParams);
280  o << stParams.dumpAsText() << endl;
281 
282  o << "Right camera pose wrt left camera (YPR):" << endl
283  << CPose3D(stParams.rightCameraPose) << endl;
284 
286  o << " Left image is stored externally in file: "
287  << imageLeft.getExternalStorageFile() << endl;
288 
289  o << " Right image";
290  if (hasImageRight)
291  {
293  o << " is stored externally in file: "
294  << imageRight.getExternalStorageFile() << endl;
295  }
296  else
297  o << " : No.\n";
298 
299  o << " Disparity image";
300  if (hasImageDisparity)
301  {
303  o << " is stored externally in file: "
305  }
306  else
307  o << " : No.\n";
308 
309  o << format(
310  " Image size: %ux%u pixels\n", (unsigned int)imageLeft.getWidth(),
311  (unsigned int)imageLeft.getHeight());
312 
313  o << " Channels order: " << imageLeft.getChannelsOrder() << endl;
314 
315  o << format(
316  " Rows are stored in top-bottom order: %s\n",
317  imageLeft.isOriginTopLeft() ? "YES" : "NO");
318 }
bool hasImageRight
Whether imageRight actually contains data (Default upon construction: true)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
mrpt::utils::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
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...
double focalLengthMeters
The focal length of the camera, in meters (can be used among &#39;intrinsicParams&#39; to determine the pixel...
Definition: TCamera.h:63
bool areImagesRectified() const
This method only checks whether ALL the distortion parameters in leftCamera are set to zero...
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
const char * getChannelsOrder() const
Returns a string of the form "BGR","RGB" or "GRAY" indicating the channels ordering.
Definition: CImage.cpp:1188
STL namespace.
bool hasImageDisparity
Whether imageDisparity actually contains data (Default upon construction: false)
CMatrixFixedNumeric< double, 3, 3 > CMatrixDouble33
Definition: eigen_frwds.h:55
void swap(CObservation &o)
Swap with another observation, ONLY the data defined here in the base class CObservation.
mrpt::math::CMatrixDouble44 getHomogeneousMatrixVal() const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
Definition: CPoseOrPoint.h:276
void getDescriptionAsText(std::ostream &o) const override
Build a detailed, multi-line textual description of the observation contents and dump it to the outpu...
Structure to hold the parameters of a pinhole stereo camera model.
Definition: TStereoCamera.h:25
mrpt::poses::CPose3DQuat cameraPose
The pose of the LEFT camera, relative to the robot.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:869
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:897
Observation class for either a pair of left+right or left+disparity images from a stereo camera...
std::string dumpAsText() const
Dumps all the parameters as a multi-line string, with the same format than saveToConfigFile.
This namespace contains representation of robot actions and observations.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
#define IMPLEMENTS_MEXPLUS_FROM(complete_type)
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
Definition: CPose3DQuat.h:48
void getStereoCameraParams(mrpt::utils::TStereoCamera &out_params) const
Populates a TStereoCamera structure with the parameters in leftCamera, rightCamera and rightCameraPos...
void swap(CImage &o)
Very efficient swap of two images (just swap the internal pointers)
Definition: CImage.cpp:138
mrpt::utils::TCamera leftCamera
Parameters for the left/right cameras: individual intrinsic and distortion parameters of the cameras...
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
mrpt::utils::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:16
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates) ...
Definition: TCamera.h:57
void setStereoCameraParams(const mrpt::utils::TStereoCamera &in_params)
Sets leftCamera, rightCamera and rightCameraPose from a TStereoCamera structure.
std::string sensorLabel
An arbitrary label that can be used to identify the sensor.
Definition: CObservation.h:60
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp.
Definition: CObservation.h:58
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
mrpt::math::CArrayDouble< 5 > dist
[k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (d...
Definition: TCamera.h:60
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
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:41
TCamera leftCamera
Intrinsic and distortion parameters of the left and right cameras.
Definition: TStereoCamera.h:30
void swap(CObservationStereoImages &o)
Do an efficient swap of all data members of this object with "o".
mrpt::poses::CPose3DQuat rightCameraPose
The pose of the right camera, relative to the left one: Note that using the conventional reference co...
GLuint in
Definition: glext.h:7274
bool isOriginTopLeft() const
Returns true if the coordinates origin is top-left, or false if it is bottom-left.
Definition: CImage.cpp:939
static constexpr const char * className
mrpt::utils::CImage imageDisparity
Disparity image, only contains a valid image if hasImageDisparity == true.
std::string getExternalStorageFile() const noexcept
Only if isExternallyStored() returns true.
Definition: CImage.h:783
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ...
Definition: CQuaternion.h:46
bool isExternallyStored() const noexcept
See setExternalStorage().
Definition: CImage.h:780
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:25
struct mxArray_tag mxArray
Forward declaration for mxArray (avoid #including as much as possible to speed up compiling) ...
Definition: CSerializable.h:19
mrpt::poses::CPose3DQuat rightCameraPose
Pose of the right camera with respect to the coordinate origin of the left camera.
Definition: TStereoCamera.h:33
virtual void getDescriptionAsText(std::ostream &o) const
Build a detailed, multi-line textual description of the observation contents and dump it to the outpu...



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