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-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 
10 #include "obs-precomp.h" // Precompiled headers
11 
13 #include <mrpt/math/CMatrix.h>
15 #if MRPT_HAS_MATLAB
16 #include <mexplus/mxarray.h>
17 #endif
18 #include <algorithm> // all_of
19 
20 using namespace mrpt::obs;
21 using namespace mrpt::math;
22 using namespace mrpt::poses;
23 using namespace mrpt::img;
24 
25 // This must be added to any CSerializable class implementation file.
27 
28 /*---------------------------------------------------------------
29  Constructor
30  ---------------------------------------------------------------*/
32  void* iplImageLeft, void* iplImageRight, void* iplImageDisparity,
33  bool ownMemory)
34  : imageLeft(UNINITIALIZED_IMAGE),
35  imageRight(UNINITIALIZED_IMAGE),
36  imageDisparity(UNINITIALIZED_IMAGE),
37  hasImageDisparity(iplImageDisparity != nullptr),
38  hasImageRight(iplImageRight != nullptr)
39 {
40  if (iplImageLeft)
41  ownMemory ? imageLeft.setFromIplImage(iplImageLeft)
42  : imageLeft.loadFromIplImage(iplImageLeft);
43  if (iplImageRight)
44  ownMemory ? imageRight.setFromIplImage(iplImageRight)
45  : imageRight.loadFromIplImage(iplImageRight);
46  if (iplImageDisparity)
47  ownMemory ? imageDisparity.setFromIplImage(iplImageDisparity)
48  : imageDisparity.loadFromIplImage(iplImageDisparity);
49 }
50 
54 {
55  // The data
56  out << cameraPose << leftCamera << rightCamera << imageLeft;
57  out << hasImageDisparity << hasImageRight;
58  if (hasImageRight) out << imageRight;
59  if (hasImageDisparity) out << imageDisparity;
60  out << timestamp;
61  out << rightCameraPose;
62  out << sensorLabel;
63 }
64 
67 {
68  switch (version)
69  {
70  case 6:
71  {
72  in >> cameraPose >> leftCamera >> rightCamera >> imageLeft;
73  in >> hasImageDisparity >> hasImageRight;
74  if (hasImageRight) in >> imageRight;
75  if (hasImageDisparity) in >> imageDisparity;
76  in >> timestamp;
77  in >> rightCameraPose;
78  in >> sensorLabel;
79  }
80  break;
81 
82  case 0:
83  case 1:
84  case 2:
85  case 3:
86  case 4:
87  case 5:
88  {
89  // This, for backwards compatibility before version 6:
90  hasImageRight = true;
91  hasImageDisparity = false;
92 
93  if (version < 5)
94  {
95  CPose3D aux;
96  in >> aux;
97  cameraPose = CPose3DQuat(aux);
98  }
99 
100  if (version >= 5)
101  {
102  in >> cameraPose >> leftCamera >> rightCamera;
103  }
104  else
105  {
106  CMatrix intParams;
107  in >> intParams; // Get the intrinsic params
108  leftCamera.intrinsicParams =
109  CMatrixDouble33(intParams); // Set them to both cameras
110  rightCamera.intrinsicParams = CMatrixDouble33(
111  intParams); // ... distortion parameters are set to zero
112  }
113 
114  in >> imageLeft >> imageRight; // For all the versions
115 
116  if (version >= 1)
117  in >> timestamp;
118  else
119  timestamp = INVALID_TIMESTAMP; // For version 1 to 5
120  if (version >= 2)
121  {
122  if (version < 5)
123  {
124  CPose3D aux;
125  in >> aux;
126  rightCameraPose = CPose3DQuat(aux);
127  }
128  else
129  in >> rightCameraPose;
130  }
131  else
132  rightCameraPose = CPose3DQuat(
133  0.10f, 0, 0, mrpt::math::CQuaternionDouble(
134  1, 0, 0, 0)); // For version 1 to 5
135 
136  if (version >= 3 && version < 5) // For versions 3 & 4
137  {
138  double foc;
139  in >> foc; // Get the focal length in meters
140  leftCamera.focalLengthMeters = rightCamera.focalLengthMeters =
141  foc; // ... and set it to both cameras
142  }
143  else if (version < 3)
144  leftCamera.focalLengthMeters = rightCamera.focalLengthMeters =
145  0.002; // For version 0, 1 & 2 (from version 5, this
146  // parameter is included in the TCamera objects)
147 
148  if (version >= 4)
149  in >> sensorLabel;
150  else
151  sensorLabel = ""; // For version 1 to 5
152  }
153  break;
154  default:
156  };
157 }
158 
159 /*---------------------------------------------------------------
160  Implements the writing to a mxArray for Matlab
161  ---------------------------------------------------------------*/
162 #if MRPT_HAS_MATLAB
163 // Add to implement mexplus::from template specialization
165 #endif
166 
168 {
169 #if MRPT_HAS_MATLAB
170  const char* fields[] = {"class", "ts", "sensorLabel", "imageL",
171  "imageR", "poseL", "poseLR", "poseR",
172  "paramsL", "paramsR"};
173  mexplus::MxArray obs_struct(
174  mexplus::MxArray::Struct(sizeof(fields) / sizeof(fields[0]), fields));
175 
176  obs_struct.set("class", this->GetRuntimeClass()->className);
177  obs_struct.set("ts", this->timestamp);
178  obs_struct.set("sensorLabel", this->sensorLabel);
179  obs_struct.set("imageL", this->imageLeft);
180  obs_struct.set("imageR", this->imageRight);
181  obs_struct.set("poseL", this->cameraPose);
182  obs_struct.set("poseR", this->cameraPose + this->rightCameraPose);
183  obs_struct.set("poseLR", this->rightCameraPose);
184  obs_struct.set("paramsL", this->leftCamera);
185  obs_struct.set("paramsR", this->rightCamera);
186  return obs_struct.release();
187 #else
188  THROW_EXCEPTION("MRPT was built without MEX (Matlab) support!");
189 #endif
190 }
191 
192 /** Populates a TStereoCamera structure with the parameters in \a leftCamera, \a
193  * rightCamera and \a rightCameraPose */
195  mrpt::img::TStereoCamera& out_params) const
196 {
197  out_params.leftCamera = this->leftCamera;
198  out_params.rightCamera = this->rightCamera;
199  out_params.rightCameraPose = this->rightCameraPose.asTPose();
200 }
201 
202 /** Sets \a leftCamera, \a rightCamera and \a rightCameraPose from a
203  * TStereoCamera structure */
205  const mrpt::img::TStereoCamera& in_params)
206 {
207  this->leftCamera = in_params.leftCamera;
208  this->rightCamera = in_params.rightCamera;
209  this->rightCameraPose = in_params.rightCameraPose;
210 }
211 
212 /** This method only checks whether ALL the distortion parameters in \a
213  * leftCamera are set to zero, which is
214  * the convention in MRPT to denote that this pair of stereo images has been
215  * rectified.
216  */
218 {
219  return std::all_of(
220  leftCamera.dist.begin(), leftCamera.dist.end(),
221  [](auto v) { return v == 0; });
222 }
223 
224 // Do an efficient swap of all data members of this object with "o".
226 {
228 
229  imageLeft.swap(o.imageLeft);
230  imageRight.swap(o.imageRight);
231  imageDisparity.swap(o.imageDisparity);
232 
233  std::swap(hasImageDisparity, o.hasImageDisparity);
234  std::swap(hasImageRight, o.hasImageRight);
235 
236  std::swap(leftCamera, o.leftCamera);
237  std::swap(rightCamera, o.rightCamera);
238 
239  std::swap(cameraPose, o.cameraPose);
240  std::swap(rightCameraPose, o.rightCameraPose);
241 }
242 
244 {
245  using namespace std;
247 
248  o << "Homogeneous matrix for the sensor's 3D pose, relative to robot "
249  "base:\n";
250  o << cameraPose.getHomogeneousMatrixVal<CMatrixDouble44>() << endl
251  << "Camera pose: " << cameraPose << endl
252  << "Camera pose (YPR): " << CPose3D(cameraPose) << endl
253  << endl;
254 
255  mrpt::img::TStereoCamera stParams;
256  getStereoCameraParams(stParams);
257  o << stParams.dumpAsText() << endl;
258 
259  o << "Right camera pose wrt left camera (YPR):" << endl
260  << CPose3D(stParams.rightCameraPose) << endl;
261 
262  if (imageLeft.isExternallyStored())
263  o << " Left image is stored externally in file: "
264  << imageLeft.getExternalStorageFile() << endl;
265 
266  o << " Right image";
267  if (hasImageRight)
268  {
269  if (imageRight.isExternallyStored())
270  o << " is stored externally in file: "
271  << imageRight.getExternalStorageFile() << endl;
272  }
273  else
274  o << " : No.\n";
275 
276  o << " Disparity image";
277  if (hasImageDisparity)
278  {
279  if (imageDisparity.isExternallyStored())
280  o << " is stored externally in file: "
281  << imageDisparity.getExternalStorageFile() << endl;
282  }
283  else
284  o << " : No.\n";
285 
286  o << format(
287  " Image size: %ux%u pixels\n", (unsigned int)imageLeft.getWidth(),
288  (unsigned int)imageLeft.getHeight());
289 
290  o << " Channels order: " << imageLeft.getChannelsOrder() << endl;
291 
292  o << format(
293  " Rows are stored in top-bottom order: %s\n",
294  imageLeft.isOriginTopLeft() ? "YES" : "NO");
295 }
mrpt::obs::CObservationStereoImages::setStereoCameraParams
void setStereoCameraParams(const mrpt::img::TStereoCamera &in_params)
Sets leftCamera, rightCamera and rightCameraPose from a TStereoCamera structure.
Definition: CObservationStereoImages.cpp:204
mrpt::obs::CObservationStereoImages::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CObservationStereoImages.cpp:51
mrpt::obs::CObservationStereoImages::rightCamera
mrpt::img::TCamera rightCamera
Definition: CObservationStereoImages.h:93
mrpt::obs::CObservationStereoImages::serializeFrom
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CObservationStereoImages.cpp:65
INVALID_TIMESTAMP
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:15
CMatrix.h
mrpt::obs::CObservationStereoImages::areImagesRectified
bool areImagesRectified() const
This method only checks whether ALL the distortion parameters in leftCamera are set to zero,...
Definition: CObservationStereoImages.cpp:217
mrpt::obs::CObservationStereoImages::getDescriptionAsText
void getDescriptionAsText(std::ostream &o) const override
Build a detailed, multi-line textual description of the observation contents and dump it to the outpu...
Definition: CObservationStereoImages.cpp:243
mrpt::math::CMatrixDouble33
CMatrixFixedNumeric< double, 3, 3 > CMatrixDouble33
Definition: eigen_frwds.h:57
obs-precomp.h
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::poses::CPose3DQuat
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,...
Definition: CPose3DQuat.h:48
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::obs::CObservationStereoImages::imageRight
mrpt::img::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
Definition: CObservationStereoImages.h:72
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::obs::CObservationStereoImages::serializeTo
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CObservationStereoImages.cpp:52
CObservationStereoImages.h
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
mrpt::img::TStereoCamera::rightCamera
TCamera rightCamera
Definition: TStereoCamera.h:30
v
const GLdouble * v
Definition: glext.h:3678
mrpt::img
Definition: CCanvas.h:17
mrpt::obs::CObservationStereoImages::leftCamera
mrpt::img::TCamera leftCamera
Parameters for the left/right cameras: individual intrinsic and distortion parameters of the cameras.
Definition: CObservationStereoImages.h:93
mrpt::obs::CObservationStereoImages::imageLeft
mrpt::img::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
Definition: CObservationStereoImages.h:68
mrpt::math::CMatrix
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:24
mrpt::img::TStereoCamera
Structure to hold the parameters of a pinhole stereo camera model.
Definition: TStereoCamera.h:25
mrpt::obs::CObservation::getDescriptionAsText
virtual void getDescriptionAsText(std::ostream &o) const
Build a detailed, multi-line textual description of the observation contents and dump it to the outpu...
Definition: CObservation.cpp:44
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::img::TStereoCamera::rightCameraPose
mrpt::math::TPose3DQuat rightCameraPose
Pose of the right camera with respect to the coordinate origin of the left camera.
Definition: TStereoCamera.h:33
mrpt::obs::CObservationStereoImages::swap
void swap(CObservationStereoImages &o)
Do an efficient swap of all data members of this object with "o".
Definition: CObservationStereoImages.cpp:225
mrpt::obs::CObservationStereoImages::getStereoCameraParams
void getStereoCameraParams(mrpt::img::TStereoCamera &out_params) const
Populates a TStereoCamera structure with the parameters in leftCamera, rightCamera and rightCameraPos...
Definition: CObservationStereoImages.cpp:194
mrpt::obs::CObservationStereoImages::imageDisparity
mrpt::img::CImage imageDisparity
Disparity image, only contains a valid image if hasImageDisparity == true.
Definition: CObservationStereoImages.h:78
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Definition: CSerializable.h:114
mrpt::math::CMatrixFixedNumeric
A numeric matrix of compile-time fixed size.
Definition: CMatrixFixedNumeric.h:40
mrpt::math::CQuaternion
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ,...
Definition: CQuaternion.h:46
mrpt::obs::CObservationStereoImages::cameraPose
mrpt::poses::CPose3DQuat cameraPose
The pose of the LEFT camera, relative to the robot.
Definition: CObservationStereoImages.h:96
mrpt::obs::CObservation
Declares a class that represents any robot's observation.
Definition: CObservation.h:43
mrpt::obs::CObservation::swap
void swap(CObservation &o)
Swap with another observation, ONLY the data defined here in the base class CObservation.
Definition: CObservation.cpp:38
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
IMPLEMENTS_MEXPLUS_FROM
#define IMPLEMENTS_MEXPLUS_FROM(complete_type)
Definition: CSerializable.h:149
mrpt::img::UNINITIALIZED_IMAGE
@ UNINITIALIZED_IMAGE
Definition: img/CImage.h:50
in
GLuint in
Definition: glext.h:7274
mrpt::serialization::CSerializable::writeToMatlab
virtual mxArray * writeToMatlab() const
Introduces a pure virtual method responsible for writing to a mxArray Matlab object,...
Definition: CSerializable.h:70
mrpt::img::TStereoCamera::leftCamera
TCamera leftCamera
Intrinsic and distortion parameters of the left and right cameras.
Definition: TStereoCamera.h:30
mrpt::obs::CObservationStereoImages::hasImageRight
bool hasImageRight
Whether imageRight actually contains data (Default upon construction: true)
Definition: CObservationStereoImages.h:85
CArchive.h
mrpt::img::TStereoCamera::dumpAsText
std::string dumpAsText() const
Dumps all the parameters as a multi-line string, with the same format than saveToConfigFile.
Definition: TStereoCamera.cpp:98
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
mxArray
struct mxArray_tag mxArray
Forward declaration for mxArray (avoid #including as much as possible to speed up compiling)
Definition: CSerializable.h:18
mrpt::obs::CObservationStereoImages::hasImageDisparity
bool hasImageDisparity
Whether imageDisparity actually contains data (Default upon construction: false)
Definition: CObservationStereoImages.h:82
mrpt::obs::CObservationStereoImages::rightCameraPose
mrpt::poses::CPose3DQuat rightCameraPose
The pose of the right camera, relative to the left one: Note that using the conventional reference co...
Definition: CObservationStereoImages.h:104
mrpt::obs::CObservationStereoImages
Observation class for either a pair of left+right or left+disparity images from a stereo camera.
Definition: CObservationStereoImages.h:41



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