Main MRPT website > C++ reference for MRPT 1.9.9
CObservation3DRangeScan.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-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 #ifndef CObservation3DRangeScan_H
10 #define CObservation3DRangeScan_H
11 
13 #include <mrpt/utils/CImage.h>
14 #include <mrpt/obs/CObservation.h>
17 #include <mrpt/poses/CPose3D.h>
18 #include <mrpt/poses/CPose2D.h>
19 #include <mrpt/math/CPolygon.h>
20 #include <mrpt/math/CMatrix.h>
21 #include <mrpt/utils/TEnumType.h>
22 #include <mrpt/utils/adapters.h>
25 
26 namespace mrpt
27 {
28 namespace obs
29 {
30 /** Used in CObservation3DRangeScan::project3DPointsFromDepthImageInto() */
32 {
33  /** (Default: false) If false, local (sensor-centric) coordinates of points
34  * are generated. Otherwise, points are transformed with \a sensorPose.
35  * Furthermore, if provided, those coordinates are transformed with \a
36  * robotPoseInTheWorld */
38  /** (Default: nullptr) Read takeIntoAccountSensorPoseOnRobot */
40  /** (Default:true) [Only used when `range_is_depth`=true] Whether to use a
41  * Look-up-table (LUT) to speed up the conversion. It's thread safe in all
42  * situations <b>except</b> when you call this method from different threads
43  * <b>and</b> with different camera parameter matrices. In all other cases,
44  * it is a good idea to left it enabled. */
46  /** (Default:true) If possible, use SSE2 optimized code. */
47  bool USE_SSE2;
48  /** (Default:true) set to false if you want to preserve the organization of
49  * the point cloud */
50  bool MAKE_DENSE;
53  robotPoseInTheWorld(nullptr),
54  PROJ3D_USE_LUT(true),
55  USE_SSE2(true),
56  MAKE_DENSE(true)
57  {
58  }
59 };
60 /** Used in CObservation3DRangeScan::convertTo2DScan() */
62 {
63  /** The sensor label that will have the newly created observation. */
65  /** (Default=5 degrees) [Only if use_origin_sensor_pose=false] The upper &
66  * lower half-FOV angle (in radians). */
68  /** (Default:-inf, +inf) [Only if use_origin_sensor_pose=true] Only obstacle
69  * points with Z coordinates within the range [z_min,z_max] will be taken
70  * into account. */
71  double z_min, z_max;
72  /** (Default=1.2=120%) How many more laser scans rays to create (read docs
73  * for CObservation3DRangeScan::convertTo2DScan()). */
75 
76  /** (Default:false) If `false`, the conversion will be such that the 2D
77  * observation pose on the robot coincides with that in the original 3D
78  * range scan.
79  * If `true`, the sensed points will be "reprojected" as seen from a sensor
80  * pose at the robot/vehicle frame origin (and angle_sup, angle_inf will be
81  * ignored) */
83 
85 };
86 
87 namespace detail
88 {
89 // Implemented in CObservation3DRangeScan_project3D_impl.h
90 template <class POINTMAP>
92  mrpt::obs::CObservation3DRangeScan& src_obs, POINTMAP& dest_pointcloud,
93  const mrpt::obs::T3DPointsProjectionParams& projectParams,
94  const mrpt::obs::TRangeImageFilterParams& filterParams);
95 }
96 
97 /** Declares a class derived from "CObservation" that encapsules a 3D range scan
98  *measurement, as from a time-of-flight range camera or any other RGBD sensor.
99  *
100  * This kind of observations can carry one or more of these data fields:
101  * - 3D point cloud (as float's).
102  * - Each 3D point has its associated (u,v) pixel coordinates in \a
103  *points3D_idxs_x & \a points3D_idxs_y (New in MRPT 1.4.0)
104  * - 2D range image (as a matrix): Each entry in the matrix
105  *"rangeImage(ROW,COLUMN)" contains a distance or a depth (in meters), depending
106  *on \a range_is_depth.
107  * - 2D intensity (grayscale or RGB) image (as a mrpt::utils::CImage): For
108  *SwissRanger cameras, a logarithmic A-law compression is used to convert the
109  *original 16bit intensity to a more standard 8bit graylevel.
110  * - 2D confidence image (as a mrpt::utils::CImage): For each pixel, a 0x00
111  *and a 0xFF mean the lowest and highest confidence levels, respectively.
112  * - Semantic labels: Stored as a matrix of bitfields, each bit having a
113  *user-defined meaning.
114  *
115  * The coordinates of the 3D point cloud are in meters with respect to the
116  *depth camera origin of coordinates
117  * (in SwissRanger, the front face of the camera: a small offset ~1cm in
118  *front of the physical focal point),
119  * with the +X axis pointing forward, +Y pointing left-hand and +Z pointing
120  *up. By convention, a 3D point with its coordinates set to (0,0,0), will be
121  *considered as invalid.
122  * The field CObservation3DRangeScan::relativePoseIntensityWRTDepth describes
123  *the change of coordinates from
124  * the depth camera to the intensity (RGB or grayscale) camera. In a
125  *SwissRanger camera both cameras coincide,
126  * so this pose is just a rotation (0,0,0,-90deg,0,-90deg). But in
127  * Microsoft Kinect there is also an offset, as shown in this figure:
128  *
129  * <div align=center>
130  * <img src="CObservation3DRangeScan_figRefSystem.png">
131  * </div>
132  *
133  * In any case, check the field \a relativePoseIntensityWRTDepth, or the method
134  *\a doDepthAndIntensityCamerasCoincide()
135  * to determine if both frames of reference coincide, since even for Kinect
136  *cameras both can coincide if the images
137  * have been rectified.
138  *
139  * The 2D images and matrices are stored as common images, with an up->down
140  *rows order and left->right, as usual.
141  * Optionally, the intensity and confidence channels can be set to
142  *delayed-load images for off-rawlog storage so it saves
143  * memory by having loaded in memory just the needed images. See the methods
144  *load() and unload().
145  * Due to the intensive storage requirements of this kind of observations, this
146  *observation is the only one in MRPT
147  * for which it's recommended to always call "load()" and "unload()" before
148  *and after using the observation, *ONLY* when
149  * the observation was read from a rawlog dataset, in order to make sure that
150  *all the externally stored data fields are
151  * loaded and ready in memory.
152  *
153  * Classes that grab observations of this type are:
154  * - mrpt::hwdrivers::CSwissRanger3DCamera
155  * - mrpt::hwdrivers::CKinect
156  * - mrpt::hwdrivers::COpenNI2Sensor
157  *
158  * There are two sets of calibration parameters (see
159  *mrpt::vision::checkerBoardStereoCalibration() or the ready-to-use GUI program
160  *<a href="http://www.mrpt.org/Application:kinect-calibrate"
161  *>kinect-calibrate</a>):
162  * - cameraParams: Projection parameters of the depth camera.
163  * - cameraParamsIntensity: Projection parameters of the intensity
164  *(gray-level or RGB) camera.
165  *
166  * In some cameras, like SwissRanger, both are the same. It is possible in
167  *Kinect to rectify the range images such both cameras
168  * seem to coincide and then both sets of camera parameters will be identical.
169  *
170  * Range data can be interpreted in two different ways depending on the 3D
171  *camera (this field is already set to the
172  * correct setting when grabbing observations from an mrpt::hwdrivers
173  *sensor):
174  * - range_is_depth=true -> Kinect-like ranges: entries of \a rangeImage
175  *are
176  *distances along the +X axis
177  * - range_is_depth=false -> Ranges in \a rangeImage are actual distances
178  *in
179  *3D.
180  *
181  * The "intensity" channel may come from different channels in sesnsors as
182  *Kinect. Look at field \a intensityImageChannel to
183  * find out if the image was grabbed from the visible (RGB) or IR channels.
184  *
185  * 3D point clouds can be generated at any moment after grabbing with
186  *CObservation3DRangeScan::project3DPointsFromDepthImage() and
187  *CObservation3DRangeScan::project3DPointsFromDepthImageInto(), provided the
188  *correct
189  * calibration parameters. Note that project3DPointsFromDepthImage() will
190  *store the point cloud in sensor-centric local coordinates. Use
191  *project3DPointsFromDepthImageInto() to directly obtain vehicle or world
192  *coordinates.
193  *
194  * Example of how to assign labels to pixels (for object segmentation, semantic
195  *information, etc.):
196  *
197  * \code
198  * // Assume obs of type CObservation3DRangeScan::Ptr
199  * obs->pixelLabels = CObservation3DRangeScan::TPixelLabelInfo::Ptr( new
200  *CObservation3DRangeScan::TPixelLabelInfo<NUM_BYTES>() );
201  * obs->pixelLabels->setSize(ROWS,COLS);
202  * obs->pixelLabels->setLabel(col,row, label_idx); // label_idxs =
203  *[0,2^NUM_BYTES-1]
204  * //...
205  * \endcode
206  *
207  * \note Starting at serialization version 2 (MRPT 0.9.1+), the confidence
208  *channel is stored as an image instead of a matrix to optimize memory and disk
209  *space.
210  * \note Starting at serialization version 3 (MRPT 0.9.1+), the 3D point cloud
211  *and the rangeImage can both be stored externally to save rawlog space.
212  * \note Starting at serialization version 5 (MRPT 0.9.5+), the new field \a
213  *range_is_depth
214  * \note Starting at serialization version 6 (MRPT 0.9.5+), the new field \a
215  *intensityImageChannel
216  * \note Starting at serialization version 7 (MRPT 1.3.1+), new fields for
217  *semantic labeling
218  * \note Since MRPT 1.5.0, external files format can be selected at runtime
219  *with `CObservation3DRangeScan::EXTERNALS_AS_TEXT`
220  *
221  * \sa mrpt::hwdrivers::CSwissRanger3DCamera, mrpt::hwdrivers::CKinect,
222  *CObservation
223  * \ingroup mrpt_obs_grp
224  */
226 {
228 
229  protected:
230  /** If set to true, m_points3D_external_file is valid. */
232  /** 3D points are in CImage::getImagesPathBase()+<this_file_name> */
234 
235  /** If set to true, m_rangeImage_external_file is valid. */
237  /** rangeImage is in CImage::getImagesPathBase()+<this_file_name> */
239 
240  public:
241  /** Default constructor */
243  /** Destructor */
244  virtual ~CObservation3DRangeScan();
245 
246  /** @name Delayed-load manual control methods.
247  @{ */
248  /** Makes sure all images and other fields which may be externally stored
249  * are loaded in memory.
250  * Note that for all CImages, calling load() is not required since the
251  * images will be automatically loaded upon first access, so load()
252  * shouldn't be needed to be called in normal cases by the user.
253  * If all the data were alredy loaded or this object has no externally
254  * stored data fields, calling this method has no effects.
255  * \sa unload
256  */
257  virtual void load() const override;
258  /** Unload all images, for the case they being delayed-load images stored in
259  * external files (othewise, has no effect).
260  * \sa load
261  */
262  virtual void unload() override;
263  /** @} */
264 
265  /** Project the RGB+D images into a 3D point cloud (with color if the target
266  * map supports it) and optionally at a given 3D pose.
267  * The 3D point coordinates are computed from the depth image (\a
268  * rangeImage) and the depth camera camera parameters (\a cameraParams).
269  * There exist two set of formulas for projecting the i'th point,
270  * depending on the value of "range_is_depth".
271  * In all formulas below, "rangeImage" is the matrix of ranges and the
272  * pixel coordinates are (r,c).
273  *
274  * 1) [range_is_depth=true] With "range equals depth" or "Kinect-like
275  * depth mode": the range values
276  * are in fact distances along the "+X" axis, not real 3D ranges (this
277  * is the way Kinect reports ranges):
278  *
279  * \code
280  * x(i) = rangeImage(r,c)
281  * y(i) = (r_cx - c) * x(i) / r_fx
282  * z(i) = (r_cy - r) * x(i) / r_fy
283  * \endcode
284  *
285  *
286  * 2) [range_is_depth=false] With "normal ranges": range means distance in
287  * 3D. This must be set when
288  * processing data from the SwissRange 3D camera, among others.
289  *
290  * \code
291  * Ky = (r_cx - c)/r_fx
292  * Kz = (r_cy - r)/r_fy
293  *
294  * x(i) = rangeImage(r,c) / sqrt( 1 + Ky^2 + Kz^2 )
295  * y(i) = Ky * x(i)
296  * z(i) = Kz * x(i)
297  * \endcode
298  *
299  * The color of each point is determined by projecting the 3D local point
300  * into the RGB image using \a cameraParamsIntensity.
301  *
302  * By default the local (sensor-centric) coordinates of points are
303  * directly stored into the local map, but if indicated so in \a
304  * takeIntoAccountSensorPoseOnRobot
305  * the points are transformed with \a sensorPose. Furthermore, if
306  * provided, those coordinates are transformed with \a robotPoseInTheWorld
307  *
308  * \tparam POINTMAP Supported maps are all those covered by
309  * mrpt::utils::PointCloudAdapter (mrpt::maps::CPointsMap and derived,
310  * mrpt::opengl::CPointCloudColoured, PCL point clouds,...)
311  *
312  * \note In MRPT < 0.9.5, this method always assumes that ranges were in
313  * Kinect-like format.
314  */
315  template <class POINTMAP>
317  POINTMAP& dest_pointcloud,
318  const T3DPointsProjectionParams& projectParams,
319  const TRangeImageFilterParams& filterParams = TRangeImageFilterParams())
320  {
321  detail::project3DPointsFromDepthImageInto<POINTMAP>(
322  *this, dest_pointcloud, projectParams, filterParams);
323  }
324 
325  /** This method is equivalent to \c project3DPointsFromDepthImageInto()
326  * storing the projected 3D points (without color, in local sensor-centric
327  * coordinates) in this same class.
328  * For new code it's recommended to use instead \c
329  * project3DPointsFromDepthImageInto() which is much more versatile. */
330  inline void project3DPointsFromDepthImage(const bool PROJ3D_USE_LUT = true)
331  {
333  p.takeIntoAccountSensorPoseOnRobot = false;
334  p.PROJ3D_USE_LUT = PROJ3D_USE_LUT;
335  this->project3DPointsFromDepthImageInto(*this, p);
336  }
337 
338  /** Convert this 3D observation into an "equivalent 2D fake laser scan",
339  * with a configurable vertical FOV.
340  *
341  * The result is a 2D laser scan with more "rays" (N) than columns has the
342  * 3D observation (W), exactly: N = W * oversampling_ratio.
343  * This oversampling is required since laser scans sample the space at
344  * evenly-separated angles, while
345  * a range camera follows a tangent-like distribution. By oversampling we
346  * make sure we don't leave "gaps" unseen by the virtual "2D laser".
347  *
348  * All obstacles within a frustum are considered and the minimum distance
349  * is kept in each direction.
350  * The horizontal FOV of the frustum is automatically computed from the
351  * intrinsic parameters, but the
352  * vertical FOV must be provided by the user, and can be set to be
353  * assymetric which may be useful
354  * depending on the zone of interest where to look for obstacles.
355  *
356  * All spatial transformations are riguorosly taken into account in this
357  * class, using the depth camera
358  * intrinsic calibration parameters.
359  *
360  * The timestamp of the new object is copied from the 3D object.
361  * Obviously, a requisite for calling this method is the 3D observation
362  * having range data,
363  * i.e. hasRangeImage must be true. It's not needed to have RGB data nor
364  * the raw 3D point clouds
365  * for this method to work.
366  *
367  * If `scanParams.use_origin_sensor_pose` is `true`, the points will be
368  * projected to 3D and then reprojected
369  * as seen from a different sensorPose at the vehicle frame origin.
370  * Otherwise (the default), the output 2D observation will share the
371  * sensorPose of the input 3D scan
372  * (using a more efficient algorithm that avoids trigonometric functions).
373  *
374  * \param[out] out_scan2d The resulting 2D equivalent scan.
375  *
376  * \sa The example in
377  * http://www.mrpt.org/tutorials/mrpt-examples/example-kinect-to-2d-laser-demo/
378  */
379  void convertTo2DScan(
381  const T3DPointsTo2DScanParams& scanParams,
382  const TRangeImageFilterParams& filterParams =
384 
385  /** Whether external files (3D points, range and confidence) are to be
386  * saved as `.txt` text files (MATLAB compatible) or `*.bin` binary
387  *(faster).
388  * Loading always will determine the type by inspecting the file extension.
389  * \note Default=false
390  **/
391  static void EXTERNALS_AS_TEXT(bool value);
392  static bool EXTERNALS_AS_TEXT();
393 
394  /** \name Point cloud
395  * @{ */
396  /** true means the field points3D contains valid data. */
398  /** If hasPoints3D=true, the (X,Y,Z) coordinates of the 3D point cloud
399  * detected by the camera. \sa resizePoints3DVectors */
400  std::vector<float> points3D_x, points3D_y, points3D_z;
401  /** If hasPoints3D=true, the (x,y) pixel coordinates for each (X,Y,Z) point
402  * in \a points3D_x, points3D_y, points3D_z */
403  std::vector<uint16_t> points3D_idxs_x, points3D_idxs_y; //!<
404 
405  /** Use this method instead of resizing all three \a points3D_x, \a
406  * points3D_y & \a points3D_z to allow the usage of the internal memory
407  * pool. */
408  void resizePoints3DVectors(const size_t nPoints);
409 
410  /** Get the size of the scan pointcloud. \note Method is added for
411  * compatibility with its CObservation2DRangeScan counterpart */
412  size_t getScanSize() const;
413  /** @} */
414 
415  /** \name Point cloud external storage functions
416  * @{ */
417  inline bool points3D_isExternallyStored() const
418  {
420  }
422  {
424  }
426  std::string& out_path) const;
428  {
429  std::string tmp;
431  return tmp;
432  }
433  /** Users won't normally want to call this, it's only used from internal
434  * MRPT programs. \sa EXTERNALS_AS_TEXT */
436  const std::string& fileName, const std::string& use_this_base_dir);
437  /** @} */
438 
439  /** \name Range (depth) image
440  * @{ */
441  /** true means the field rangeImage contains valid data */
443  /** If hasRangeImage=true, a matrix of floats with the range data as
444  * captured by the camera (in meters) \sa range_is_depth */
446  /** true: Kinect-like ranges: entries of \a rangeImage are distances along
447  * the +X axis; false: Ranges in \a rangeImage are actual distances in 3D.
448  */
450 
451  /** Similar to calling "rangeImage.setSize(H,W)" but this method provides
452  * memory pooling to speed-up the memory allocation. */
453  void rangeImage_setSize(const int HEIGHT, const int WIDTH);
454  /** @} */
455 
456  /** \name Range Matrix external storage functions
457  * @{ */
458  inline bool rangeImage_isExternallyStored() const
459  {
461  }
463  {
465  }
467  std::string& out_path) const;
469  {
470  std::string tmp;
472  return tmp;
473  }
474  /** Users won't normally want to call this, it's only used from internal
475  * MRPT programs. \sa EXTERNALS_AS_TEXT */
477  const std::string& fileName, const std::string& use_this_base_dir);
478  /** Forces marking this observation as non-externally stored - it doesn't
479  * anything else apart from reseting the corresponding flag (Users won't
480  * normally want to call this, it's only used from internal MRPT programs)
481  */
483  {
485  }
486  /** @} */
487 
488  /** \name Intensity (RGB) channels
489  * @{ */
490  /** Enum type for intensityImageChannel */
492  {
493  /** Grayscale or RGB visible channel of the camera sensor. */
495  /** Infrarred (IR) channel */
496  CH_IR = 1
497  };
498 
499  /** true means the field intensityImage contains valid data */
501  /** If hasIntensityImage=true, a color or gray-level intensity image of the
502  * same size than "rangeImage" */
504  /** The source of the intensityImage; typically the visible channel \sa
505  * TIntensityChannelID */
507  /** @} */
508 
509  /** \name Confidence "channel"
510  * @{ */
511  /** true means the field confidenceImage contains valid data */
513  /** If hasConfidenceImage=true, an image with the "confidence" value [range
514  * 0-255] as estimated by the capture drivers. */
516  /** @} */
517 
518  /** \name Pixel-wise classification labels (for semantic labeling, etc.)
519  * @{ */
520  /** Returns true if the field CObservation3DRangeScan::pixelLabels contains
521  * a non-NULL smart pointer.
522  * To enhance a 3D point cloud with labeling info, just assign an
523  * appropiate object to \a pixelLabels
524  */
525  bool hasPixelLabels() const { return pixelLabels ? true : false; }
526  /** Virtual interface to all pixel-label information structs. See
527  * CObservation3DRangeScan::pixelLabels */
529  {
530  /** Used in CObservation3DRangeScan::pixelLabels */
531  using Ptr = std::shared_ptr<TPixelLabelInfoBase>;
532  typedef std::map<uint32_t, std::string> TMapLabelID2Name;
533 
534  /** The 'semantic' or human-friendly name of the i'th bit in
535  * pixelLabels(r,c) can be found in pixelLabelNames[i] as a std::string
536  */
538 
539  const std::string& getLabelName(unsigned int label_idx) const
540  {
542  pixelLabelNames.find(label_idx);
543  if (it == pixelLabelNames.end())
544  throw std::runtime_error(
545  "Error: label index has no defined name");
546  return it->second;
547  }
548  void setLabelName(unsigned int label_idx, const std::string& name)
549  {
550  pixelLabelNames[label_idx] = name;
551  }
552  /** Check the existence of a label by returning its associated index.
553  * -1 if it does not exist. */
555  {
557  for (it = pixelLabelNames.begin(); it != pixelLabelNames.end();
558  it++)
559  if (it->second == name) return it->first;
560  return -1;
561  }
562 
563  /** Resizes the matrix pixelLabels to the given size, setting all
564  * bitfields to zero (that is, all pixels are assigned NONE category).
565  */
566  virtual void setSize(const int NROWS, const int NCOLS) = 0;
567  /** Mark the pixel(row,col) as classified in the category \a label_idx,
568  * which may be in the range 0 to MAX_NUM_LABELS-1
569  * Note that 0 is a valid label index, it does not mean "no label" \sa
570  * unsetLabel, unsetAll */
571  virtual void setLabel(
572  const int row, const int col, uint8_t label_idx) = 0;
573  virtual void getLabels(
574  const int row, const int col, uint8_t& labels) = 0;
575  /** For the pixel(row,col), removes its classification into the category
576  * \a label_idx, which may be in the range 0 to 7
577  * Note that 0 is a valid label index, it does not mean "no label" \sa
578  * setLabel, unsetAll */
579  virtual void unsetLabel(
580  const int row, const int col, uint8_t label_idx) = 0;
581  /** Removes all categories for pixel(row,col) \sa setLabel, unsetLabel
582  */
583  virtual void unsetAll(
584  const int row, const int col, uint8_t label_idx) = 0;
585  /** Checks whether pixel(row,col) has been clasified into category \a
586  * label_idx, which may be in the range 0 to 7
587  * \sa unsetLabel, unsetAll */
588  virtual bool checkLabel(
589  const int row, const int col, uint8_t label_idx) const = 0;
590 
591  void writeToStream(mrpt::utils::CStream& out) const;
594 
595  /// std stream interface
596  friend std::ostream& operator<<(
597  std::ostream& out, const TPixelLabelInfoBase& obj)
598  {
599  obj.Print(out);
600  return out;
601  }
602 
603  TPixelLabelInfoBase(unsigned int BITFIELD_BYTES_)
604  : BITFIELD_BYTES(BITFIELD_BYTES_)
605  {
606  }
607 
608  virtual ~TPixelLabelInfoBase() {}
609  /** Minimum number of bytes required to hold MAX_NUM_DIFFERENT_LABELS
610  * bits. */
612 
613  protected:
615  virtual void internal_writeToStream(
616  mrpt::utils::CStream& out) const = 0;
617  virtual void Print(std::ostream&) const = 0;
618  };
619 
620  template <unsigned int BYTES_REQUIRED_>
622  {
623  using Ptr = std::shared_ptr<TPixelLabelInfo>;
624  enum
625  {
626  BYTES_REQUIRED = BYTES_REQUIRED_ // ((MAX_LABELS-1)/8)+1
627  };
628 
629  /** Automatically-determined integer type of the proper size such that
630  * all labels fit as one bit (max: 64) */
631  typedef
634 
635  /** Each pixel may be assigned between 0 and MAX_NUM_LABELS-1 'labels'
636  * by
637  * setting to 1 the corresponding i'th bit [0,MAX_NUM_LABELS-1] in the
638  * byte in pixelLabels(r,c).
639  * That is, each pixel is assigned an 8*BITFIELD_BYTES bit-wide
640  * bitfield of possible categories.
641  * \sa hasPixelLabels
642  */
643  typedef Eigen::Matrix<bitmask_t, Eigen::Dynamic, Eigen::Dynamic>
646 
647  void setSize(const int NROWS, const int NCOLS) override
648  {
649  pixelLabels = TPixelLabelMatrix::Zero(NROWS, NCOLS);
650  }
651  void setLabel(const int row, const int col, uint8_t label_idx) override
652  {
653  pixelLabels(row, col) |= static_cast<bitmask_t>(1) << label_idx;
654  }
655  void getLabels(const int row, const int col, uint8_t& labels) override
656  {
657  labels = pixelLabels(row, col);
658  }
659 
661  const int row, const int col, uint8_t label_idx) override
662  {
663  pixelLabels(row, col) &= ~(static_cast<bitmask_t>(1) << label_idx);
664  }
665  void unsetAll(const int row, const int col, uint8_t label_idx) override
666  {
667  pixelLabels(row, col) = 0;
668  }
670  const int row, const int col, uint8_t label_idx) const override
671  {
672  return (pixelLabels(row, col) &
673  (static_cast<bitmask_t>(1) << label_idx)) != 0;
674  }
675 
676  // Ctor: pass identification to parent for deserialization
677  TPixelLabelInfo() : TPixelLabelInfoBase(BYTES_REQUIRED_) {}
678  protected:
680  {
681  {
682  uint32_t nR, nC;
683  in >> nR >> nC;
684  pixelLabels.resize(nR, nC);
685  for (uint32_t c = 0; c < nC; c++)
686  for (uint32_t r = 0; r < nR; r++)
687  in >> pixelLabels.coeffRef(r, c);
688  }
689  in >> pixelLabelNames;
690  }
692  {
693  {
694  const uint32_t nR = static_cast<uint32_t>(pixelLabels.rows());
695  const uint32_t nC = static_cast<uint32_t>(pixelLabels.cols());
696  out << nR << nC;
697  for (uint32_t c = 0; c < nC; c++)
698  for (uint32_t r = 0; r < nR; r++)
699  out << pixelLabels.coeff(r, c);
700  }
701  out << pixelLabelNames;
702  }
703  void Print(std::ostream& out) const override
704  {
705  {
706  const uint32_t nR = static_cast<uint32_t>(pixelLabels.rows());
707  const uint32_t nC = static_cast<uint32_t>(pixelLabels.cols());
708  out << "Number of rows: " << nR << std::endl;
709  out << "Number of cols: " << nC << std::endl;
710  out << "Matrix of labels: " << std::endl;
711  for (uint32_t c = 0; c < nC; c++)
712  {
713  for (uint32_t r = 0; r < nR; r++)
714  out << pixelLabels.coeff(r, c) << " ";
715 
716  out << std::endl;
717  }
718  }
719  out << std::endl;
720  out << "Label indices and names: " << std::endl;
722  for (it = pixelLabelNames.begin(); it != pixelLabelNames.end();
723  it++)
724  out << it->first << " " << it->second << std::endl;
725  }
726  }; // end TPixelLabelInfo
727 
728  /** All information about pixel labeling is stored in this (smart pointer
729  * to) structure; refer to TPixelLabelInfo for details on the contents
730  * User is responsible of creating a new object of the desired data type.
731  * It will be automatically (de)serialized no matter its specific type. */
733 
734  /** @} */
735 
736  /** \name Sensor parameters
737  * @{ */
738  /** Projection parameters of the depth camera. */
740  /** Projection parameters of the intensity (graylevel or RGB) camera. */
742 
743  /** Relative pose of the intensity camera wrt the depth camera (which is the
744  * coordinates origin for this observation).
745  * In a SwissRanger camera, this will be (0,0,0,-90deg,0,-90deg) since
746  * both cameras coincide.
747  * In a Kinect, this will include a small lateral displacement and a
748  * rotation, according to the drawing on the top of this page.
749  * \sa doDepthAndIntensityCamerasCoincide
750  */
752 
753  /** Return true if \a relativePoseIntensityWRTDepth equals the pure rotation
754  * (0,0,0,-90deg,0,-90deg) (with a small comparison epsilon)
755  * \sa relativePoseIntensityWRTDepth
756  */
758 
759  /** The maximum range allowed by the device, in meters (e.g. 8.0m, 5.0m,...)
760  */
761  float maxRange;
762  /** The 6D pose of the sensor on the robot. */
764  /** The "sigma" error of the device in meters, used while inserting the scan
765  * in an occupancy grid. */
766  float stdError;
767 
768  // See base class docs
769  void getSensorPose(mrpt::poses::CPose3D& out_sensorPose) const override
770  {
771  out_sensorPose = sensorPose;
772  }
773  // See base class docs
774  void setSensorPose(const mrpt::poses::CPose3D& newSensorPose) override
775  {
776  sensorPose = newSensorPose;
777  }
778 
779  /** @} */ // end sensor params
780 
781  // See base class docs
782  void getDescriptionAsText(std::ostream& o) const override;
783 
784  /** Very efficient method to swap the contents of two observations. */
785  void swap(CObservation3DRangeScan& o);
786  /** Extract a ROI of the 3D observation as a new one. \note PixelLabels are
787  * *not* copied to the output subimage. */
788  void getZoneAsObs(
789  CObservation3DRangeScan& obs, const unsigned int& r1,
790  const unsigned int& r2, const unsigned int& c1, const unsigned int& c2);
791 
792  /** A Levenberg-Marquart-based optimizer to recover the calibration
793  * parameters of a 3D camera given a range (depth) image and the
794  * corresponding 3D point cloud.
795  * \param camera_offset The offset (in meters) in the +X direction of the
796  * point cloud. It's 1cm for SwissRanger SR4000.
797  * \return The final average reprojection error per pixel (typ <0.05 px)
798  */
800  const CObservation3DRangeScan& in_obs,
801  mrpt::utils::TCamera& out_camParams, const double camera_offset = 0.01);
802 
803  /** Look-up-table struct for project3DPointsFromDepthImageInto() */
805  {
808  };
809  /** 3D point cloud projection look-up-table \sa
810  * project3DPointsFromDepthImage */
812 
813 }; // End of class def.
814 
815 } // End of namespace
816 
817 namespace utils
818 {
819 // Specialization must occur in the same namespace
820 MRPT_DECLARE_TTYPENAME_PTR_NAMESPACE(CObservation3DRangeScan, mrpt::obs)
821 
822 // Enum <-> string converter:
823 template <>
825 {
827  static void fill(bimap<enum_t, std::string>& m_map)
828  {
829  m_map.insert(
832  }
833 };
834 }
835 
836 namespace utils
837 {
838 /** Specialization mrpt::utils::PointCloudAdapter<CObservation3DRangeScan>
839  * \ingroup mrpt_adapters_grp */
840 template <>
843  mrpt::obs::CObservation3DRangeScan, float>
844 {
845  private:
847 
848  public:
849  /** The type of each point XYZ coordinates */
850  typedef float coords_t;
851  /** Has any color RGB info? */
852  static const int HAS_RGB = 0;
853  /** Has native RGB info (as floats)? */
854  static const int HAS_RGBf = 0;
855  /** Has native RGB info (as uint8_t)? */
856  static const int HAS_RGBu8 = 0;
857 
858  /** Constructor (accept a const ref for convenience) */
860  : m_obj(*const_cast<mrpt::obs::CObservation3DRangeScan*>(&obj))
861  {
862  }
863  /** Get number of points */
864  inline size_t size() const { return m_obj.points3D_x.size(); }
865  /** Set number of points (to uninitialized values) */
866  inline void resize(const size_t N)
867  {
868  if (N) m_obj.hasPoints3D = true;
869  m_obj.resizePoints3DVectors(N);
870  }
871 
872  /** Get XYZ coordinates of i'th point */
873  template <typename T>
874  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
875  {
876  x = m_obj.points3D_x[idx];
877  y = m_obj.points3D_y[idx];
878  z = m_obj.points3D_z[idx];
879  }
880  /** Set XYZ coordinates of i'th point */
881  inline void setPointXYZ(
882  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
883  {
884  m_obj.points3D_x[idx] = x;
885  m_obj.points3D_y[idx] = y;
886  m_obj.points3D_z[idx] = z;
887  }
888  /** Set XYZ coordinates of i'th point */
889  inline void setInvalidPoint(const size_t idx)
890  {
892  "mrpt::obs::CObservation3DRangeScan requires needs to be dense");
893  }
894 
895 }; // end of PointCloudAdapter<CObservation3DRangeScan>
896 }
897 } // End of namespace
898 
900 
901 #endif
bool m_points3D_external_stored
If set to true, m_points3D_external_file is valid.
virtual void internal_writeToStream(mrpt::utils::CStream &out) const =0
std::string m_rangeImage_external_file
rangeImage is in CImage::getImagesPathBase()+<this_file_name>
void internal_writeToStream(mrpt::utils::CStream &out) const override
void setInvalidPoint(const size_t idx)
Set XYZ coordinates of i&#39;th point.
const std::string & getLabelName(unsigned int label_idx) const
GLdouble GLdouble z
Definition: glext.h:3872
void getZoneAsObs(CObservation3DRangeScan &obs, const unsigned int &r1, const unsigned int &r2, const unsigned int &c1, const unsigned int &c2)
Extract a ROI of the 3D observation as a new one.
TIntensityChannelID
Enum type for intensityImageChannel.
void resizePoints3DVectors(const size_t nPoints)
Use this method instead of resizing all three points3D_x, points3D_y & points3D_z to allow the usage ...
virtual void setSize(const int NROWS, const int NCOLS)=0
Resizes the matrix pixelLabels to the given size, setting all bitfields to zero (that is...
const mrpt::poses::CPose3D * robotPoseInTheWorld
(Default: nullptr) Read takeIntoAccountSensorPoseOnRobot
void resize(const size_t N)
Set number of points (to uninitialized values)
void setPointXYZ(const size_t idx, const coords_t x, const coords_t y, const coords_t z)
Set XYZ coordinates of i&#39;th point.
mrpt::utils::TCamera cameraParamsIntensity
Projection parameters of the intensity (graylevel or RGB) camera.
A helper base class for those PointCloudAdapter<> which do not handle RGB data; it declares needed in...
Definition: adapters.h:51
void setLabelName(unsigned int label_idx, const std::string &name)
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
#define THROW_EXCEPTION(msg)
std::string sensorLabel
The sensor label that will have the newly created observation.
void project3DPointsFromDepthImage(const bool PROJ3D_USE_LUT=true)
This method is equivalent to project3DPointsFromDepthImageInto() storing the projected 3D points (wit...
std::vector< uint16_t > points3D_idxs_x
If hasPoints3D=true, the (x,y) pixel coordinates for each (X,Y,Z) point in points3D_x, points3D_y, points3D_z.
double oversampling_ratio
(Default=1.2=120%) How many more laser scans rays to create (read docs for CObservation3DRangeScan::c...
double angle_sup
(Default=5 degrees) [Only if use_origin_sensor_pose=false] The upper & lower half-FOV angle (in radia...
bool m_rangeImage_external_stored
If set to true, m_rangeImage_external_file is valid.
void getDescriptionAsText(std::ostream &o) const override
Build a detailed, multi-line textual description of the observation contents and dump it to the outpu...
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement, as from a time-of-flight range camera or any other RGBD sensor.
Look-up-table struct for project3DPointsFromDepthImageInto()
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
Usage: uint_select_by_bytecount<N>type var; allows defining var as a unsigned integer with...
PointCloudAdapter(const mrpt::obs::CObservation3DRangeScan &obj)
Constructor (accept a const ref for convenience)
Only specializations of this class are defined for each enum type of interest.
Definition: TEnumType.h:24
friend std::ostream & operator<<(std::ostream &out, const TPixelLabelInfoBase &obj)
std stream interface
const Scalar * const_iterator
Definition: eigen_plugins.h:27
double z_min
(Default:-inf, +inf) [Only if use_origin_sensor_pose=true] Only obstacle points with Z coordinates wi...
virtual void unsetLabel(const int row, const int col, uint8_t label_idx)=0
For the pixel(row,col), removes its classification into the category label_idx, which may be in the r...
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
virtual void setLabel(const int row, const int col, uint8_t label_idx)=0
Mark the pixel(row,col) as classified in the category label_idx, which may be in the range 0 to MAX_N...
Used in CObservation3DRangeScan::project3DPointsFromDepthImageInto()
void setSensorPose(const mrpt::poses::CPose3D &newSensorPose) override
A general method to change the sensor pose on the robot.
void rangeImage_convertToExternalStorage(const std::string &fileName, const std::string &use_this_base_dir)
Users won&#39;t normally want to call this, it&#39;s only used from internal MRPT programs.
mrpt::math::CMatrix rangeImage
If hasRangeImage=true, a matrix of floats with the range data as captured by the camera (in meters) ...
unsigned char uint8_t
Definition: rptypes.h:41
mrpt::utils::TCamera cameraParams
Projection parameters of the depth camera.
const uint8_t BITFIELD_BYTES
Minimum number of bytes required to hold MAX_NUM_DIFFERENT_LABELS bits.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
std::string rangeImage_getExternalStorageFileAbsolutePath() const
mrpt::poses::CPose3D relativePoseIntensityWRTDepth
Relative pose of the intensity camera wrt the depth camera (which is the coordinates origin for this ...
virtual bool checkLabel(const int row, const int col, uint8_t label_idx) const =0
Checks whether pixel(row,col) has been clasified into category label_idx, which may be in the range 0...
TMapLabelID2Name pixelLabelNames
The &#39;semantic&#39; or human-friendly name of the i&#39;th bit in pixelLabels(r,c) can be found in pixelLabelN...
const GLubyte * c
Definition: glext.h:6313
virtual void unload() override
Unload all images, for the case they being delayed-load images stored in external files (othewise...
void swap(CObservation3DRangeScan &o)
Very efficient method to swap the contents of two observations.
void project3DPointsFromDepthImageInto(mrpt::obs::CObservation3DRangeScan &src_obs, POINTMAP &dest_pointcloud, const mrpt::obs::T3DPointsProjectionParams &projectParams, const mrpt::obs::TRangeImageFilterParams &filterParams)
Used in CObservation3DRangeScan::convertTo2DScan()
int checkLabelNameExistence(const std::string &name) const
Check the existence of a label by returning its associated index.
A bidirectional version of std::map, declared as bimap<KEY,VALUE> and which actually contains two std...
Definition: bimap.h:34
std::string m_points3D_external_file
3D points are in CImage::getImagesPathBase()+<this_file_name>
This namespace contains representation of robot actions and observations.
bool hasRangeImage
true means the field rangeImage contains valid data
void convertTo2DScan(mrpt::obs::CObservation2DRangeScan &out_scan2d, const T3DPointsTo2DScanParams &scanParams, const TRangeImageFilterParams &filterParams=TRangeImageFilterParams())
Convert this 3D observation into an "equivalent 2D fake laser scan", with a configurable vertical FOV...
TIntensityChannelID intensityImageChannel
The source of the intensityImage; typically the visible channel.
void rangeImage_forceResetExternalStorage()
Forces marking this observation as non-externally stored - it doesn&#39;t anything else apart from reseti...
std::string points3D_getExternalStorageFileAbsolutePath() const
bool hasPoints3D
true means the field points3D contains valid data.
GLsizei const GLchar ** string
Definition: glext.h:4101
Eigen::Matrix< bitmask_t, Eigen::Dynamic, Eigen::Dynamic > TPixelLabelMatrix
Each pixel may be assigned between 0 and MAX_NUM_LABELS-1 &#39;labels&#39; by setting to 1 the corresponding ...
float stdError
The "sigma" error of the device in meters, used while inserting the scan in an occupancy grid...
virtual void internal_readFromStream(mrpt::utils::CStream &in)=0
#define MRPT_DECLARE_TTYPENAME_PTR_NAMESPACE(_TYPE, __NS)
Definition: TTypeName.h:93
bool USE_SSE2
(Default:true) If possible, use SSE2 optimized code.
void getSensorPose(mrpt::poses::CPose3D &out_sensorPose) const override
A general method to retrieve the sensor pose on the robot.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void setSize(const int NROWS, const int NCOLS) override
Resizes the matrix pixelLabels to the given size, setting all bitfields to zero (that is...
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
mrpt::poses::CPose3D sensorPose
The 6D pose of the sensor on the robot.
static TPixelLabelInfoBase * readAndBuildFromStream(mrpt::utils::CStream &in)
void getLabels(const int row, const int col, uint8_t &labels) override
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
void internal_readFromStream(mrpt::utils::CStream &in) override
TPixelLabelInfoBase::Ptr pixelLabels
All information about pixel labeling is stored in this (smart pointer to) structure; refer to TPixelL...
static TCached3DProjTables & get_3dproj_lut()
3D point cloud projection look-up-table
GLenum GLenum GLvoid * row
Definition: glext.h:3576
Used in CObservation3DRangeScan::project3DPointsFromDepthImageInto()
bool hasIntensityImage
true means the field intensityImage contains valid data
bool checkLabel(const int row, const int col, uint8_t label_idx) const override
Checks whether pixel(row,col) has been clasified into category label_idx, which may be in the range 0...
GLuint in
Definition: glext.h:7274
std::vector< float > points3D_x
If hasPoints3D=true, the (X,Y,Z) coordinates of the 3D point cloud detected by the camera...
GLuint const GLchar * name
Definition: glext.h:4054
void rangeImage_setSize(const int HEIGHT, const int WIDTH)
Similar to calling "rangeImage.setSize(H,W)" but this method provides memory pooling to speed-up the ...
bool hasPixelLabels() const
Returns true if the field CObservation3DRangeScan::pixelLabels contains a non-NULL smart pointer...
static double recoverCameraCalibrationParameters(const CObservation3DRangeScan &in_obs, mrpt::utils::TCamera &out_camParams, const double camera_offset=0.01)
A Levenberg-Marquart-based optimizer to recover the calibration parameters of a 3D camera given a ran...
void project3DPointsFromDepthImageInto(POINTMAP &dest_pointcloud, const T3DPointsProjectionParams &projectParams, const TRangeImageFilterParams &filterParams=TRangeImageFilterParams())
Project the RGB+D images into a 3D point cloud (with color if the target map supports it) and optiona...
virtual void Print(std::ostream &) const =0
GLenum GLint GLint y
Definition: glext.h:3538
void points3D_convertToExternalStorage(const std::string &fileName, const std::string &use_this_base_dir)
Users won&#39;t normally want to call this, it&#39;s only used from internal MRPT programs.
mrpt::utils::uint_select_by_bytecount< BYTES_REQUIRED >::type bitmask_t
Automatically-determined integer type of the proper size such that all labels fit as one bit (max: 64...
bool PROJ3D_USE_LUT
(Default:true) [Only used when range_is_depth=true] Whether to use a Look-up-table (LUT) to speed up ...
bool doDepthAndIntensityCamerasCoincide() const
Return true if relativePoseIntensityWRTDepth equals the pure rotation (0,0,0,-90deg,0,-90deg) (with a small comparison epsilon)
mrpt::utils::CImage confidenceImage
If hasConfidenceImage=true, an image with the "confidence" value [range 0-255] as estimated by the ca...
void unsetLabel(const int row, const int col, uint8_t label_idx) override
For the pixel(row,col), removes its classification into the category label_idx, which may be in the r...
GLsizei const GLfloat * value
Definition: glext.h:4117
virtual void unsetAll(const int row, const int col, uint8_t label_idx)=0
Removes all categories for pixel(row,col)
std::shared_ptr< TPixelLabelInfoBase > Ptr
Used in CObservation3DRangeScan::pixelLabels.
An adapter to different kinds of point cloud object.
Definition: adapters.h:41
void insert(const KEY &k, const VALUE &v)
Insert a new pair KEY<->VALUE in the bi-map.
Definition: bimap.h:75
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i&#39;th point.
GLenum GLint x
Definition: glext.h:3538
bool MAKE_DENSE
(Default:true) set to false if you want to preserve the organization of the point cloud ...
mrpt::utils::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage"...
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:25
size_t getScanSize() const
Get the size of the scan pointcloud.
unsigned __int32 uint32_t
Definition: rptypes.h:47
bool range_is_depth
true: Kinect-like ranges: entries of rangeImage are distances along the +X axis; false: Ranges in ran...
void setLabel(const int row, const int col, uint8_t label_idx) override
Mark the pixel(row,col) as classified in the category label_idx, which may be in the range 0 to MAX_N...
GLfloat GLfloat p
Definition: glext.h:6305
Virtual interface to all pixel-label information structs.
void unsetAll(const int row, const int col, uint8_t label_idx) override
Removes all categories for pixel(row,col)
Grayscale or RGB visible channel of the camera sensor.
bool takeIntoAccountSensorPoseOnRobot
(Default: false) If false, local (sensor-centric) coordinates of points are generated.
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:32
virtual void load() const override
Makes sure all images and other fields which may be externally stored are loaded in memory...
virtual void getLabels(const int row, const int col, uint8_t &labels)=0
bool use_origin_sensor_pose
(Default:false) If false, the conversion will be such that the 2D observation pose on the robot coinc...
float maxRange
The maximum range allowed by the device, in meters (e.g.
bool hasConfidenceImage
true means the field confidenceImage contains valid data



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