Point Cloud Library (PCL)  1.12.1-dev
line_rgbd.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/recognition/linemod.h>
41 #include <pcl/recognition/color_gradient_modality.h>
42 #include <pcl/recognition/surface_normal_modality.h>
43 #include <pcl/io/tar.h>
44 
45 namespace pcl
46 {
47 
49  {
50  /** \brief Constructor. */
51  BoundingBoxXYZ () : x (0.0f), y (0.0f), z (0.0f), width (0.0f), height (0.0f), depth (0.0f) {}
52 
53  /** \brief X-coordinate of the upper left front point */
54  float x;
55  /** \brief Y-coordinate of the upper left front point */
56  float y;
57  /** \brief Z-coordinate of the upper left front point */
58  float z;
59 
60  /** \brief Width of the bounding box */
61  float width;
62  /** \brief Height of the bounding box */
63  float height;
64  /** \brief Depth of the bounding box */
65  float depth;
66  };
67 
68  /** \brief High-level class for template matching using the LINEMOD approach based on RGB and Depth data.
69  * \author Stefan Holzer
70  */
71  template <typename PointXYZT, typename PointRGBT=PointXYZT>
73  {
74  public:
75 
76  /** \brief A LineRGBD detection. */
77  struct Detection
78  {
79  /** \brief Constructor. */
80  Detection () : template_id (0), object_id (0), detection_id (0), response (0.0f) {}
81 
82  /** \brief The ID of the template. */
83  std::size_t template_id;
84  /** \brief The ID of the object corresponding to the template. */
85  std::size_t object_id;
86  /** \brief The ID of this detection. This is only valid for the last call of the method detect (...). */
87  std::size_t detection_id;
88  /** \brief The response of this detection. Responses are between 0 and 1, where 1 is best. */
89  float response;
90  /** \brief The 3D bounding box of the detection. */
92  /** \brief The 2D template region of the detection. */
94  };
95 
96  /** \brief Constructor */
98  : intersection_volume_threshold_ (1.0f)
99  , color_gradient_mod_ ()
100  , surface_normal_mod_ ()
101  , cloud_xyz_ ()
102  , cloud_rgb_ ()
103  , detections_ ()
104  {
105  }
106 
107  /** \brief Destructor */
108  virtual ~LineRGBD () = default;
109 
110  /** \brief Loads templates from a LMT (LineMod Template) file. Overrides old templates.
111  *
112  * LineMod Template files are TAR files that store pairs of PCD datasets
113  * together with their LINEMOD signatures in \ref
114  * SparseQuantizedMultiModTemplate format.
115  *
116  * \param[in] file_name The name of the file that stores the templates.
117  * \param object_id
118  *
119  * \return true, if the operation was successful, false otherwise.
120  */
121  bool
122  loadTemplates (const std::string &file_name, std::size_t object_id = 0);
123 
124  bool
125  addTemplate (const SparseQuantizedMultiModTemplate & sqmmt, pcl::PointCloud<pcl::PointXYZRGBA> & cloud, std::size_t object_id = 0);
126 
127  /** \brief Sets the threshold for the detection responses. Responses are between 0 and 1, where 1 is a best.
128  * \param[in] threshold The threshold used to decide where a template is detected.
129  */
130  inline void
131  setDetectionThreshold (float threshold)
132  {
133  linemod_.setDetectionThreshold (threshold);
134  }
135 
136  /** \brief Sets the threshold on the magnitude of color gradients. Color gradients with a magnitude below
137  * this threshold are not considered in the detection process.
138  * \param[in] threshold The threshold on the magnitude of color gradients.
139  */
140  inline void
141  setGradientMagnitudeThreshold (const float threshold)
142  {
143  color_gradient_mod_.setGradientMagnitudeThreshold (threshold);
144  }
145 
146  /** \brief Sets the threshold for the decision whether two detections of the same template are merged or not.
147  * If ratio between the intersection of the bounding boxes of two detections and the original bounding
148  * boxes is larger than the specified threshold then they are merged. If detection A overlaps with
149  * detection B and B with C than A, B, and C are merged. Threshold has to be between 0 and 1.
150  * \param[in] threshold The threshold on the ratio between the intersection bounding box and the original
151  * bounding box.
152  */
153  inline void
154  setIntersectionVolumeThreshold (const float threshold = 1.0f)
155  {
156  intersection_volume_threshold_ = threshold;
157  }
158 
159  /** \brief Sets the input cloud with xyz point coordinates. The cloud has to be organized.
160  * \param[in] cloud The input cloud with xyz point coordinates.
161  */
162  inline void
164  {
165  cloud_xyz_ = cloud;
166 
167  surface_normal_mod_.setInputCloud (cloud);
168  surface_normal_mod_.processInputData ();
169  }
170 
171  /** \brief Sets the input cloud with rgb values. The cloud has to be organized.
172  * \param[in] cloud The input cloud with rgb values.
173  */
174  inline void
176  {
177  cloud_rgb_ = cloud;
178 
179  color_gradient_mod_.setInputCloud (cloud);
180  color_gradient_mod_.processInputData ();
181  }
182 
183  /** \brief Creates a template from the specified data and adds it to the matching queue.
184  * \param cloud
185  * \param object_id
186  * \param[in] mask_xyz the mask that determine which parts of the xyz-modality are used for creating the template.
187  * \param[in] mask_rgb the mask that determine which parts of the rgb-modality are used for creating the template.
188  * \param[in] region the region which will be associated with the template (can be larger than the actual modality-maps).
189  */
190  int
191  createAndAddTemplate (
193  const std::size_t object_id,
194  const MaskMap & mask_xyz,
195  const MaskMap & mask_rgb,
196  const RegionXY & region);
197 
198 
199  /** \brief Applies the detection process and fills the supplied vector with the detection instances.
200  * \param[out] detections The storage for the detection instances.
201  */
202  void
203  detect (std::vector<typename pcl::LineRGBD<PointXYZT, PointRGBT>::Detection> & detections);
204 
205  /** \brief Applies the detection process in a semi-scale-invariant manner. This is done by actually
206  * scaling the template to different sizes.
207  */
208  void
209  detectSemiScaleInvariant (std::vector<typename pcl::LineRGBD<PointXYZT, PointRGBT>::Detection> & detections,
210  float min_scale = 0.6944444f,
211  float max_scale = 1.44f,
212  float scale_multiplier = 1.2f);
213 
214  /** \brief Computes and returns the point cloud of the specified detection. This is the template point
215  * cloud transformed to the detection coordinates. The detection ID refers to the last call of
216  * the method detect (...).
217  * \param[in] detection_id The ID of the detection (according to the last call of the method detect (...)).
218  * \param[out] cloud The storage for the transformed points.
219  */
220  void
221  computeTransformedTemplatePoints (const std::size_t detection_id,
223 
224  /** \brief Finds the indices of the points in the input cloud which correspond to the specified detection.
225  * The detection ID refers to the last call of the method detect (...).
226  * \param[in] detection_id The ID of the detection (according to the last call of the method detect (...)).
227  */
228  inline std::vector<std::size_t>
229  findObjectPointIndices (const std::size_t detection_id)
230  {
231  if (detection_id >= detections_.size ())
232  PCL_ERROR ("ERROR pcl::LineRGBD::computeTransformedTemplatePoints - detection_id is out of bounds\n");
233 
234  // TODO: compute transform from detection
235  // TODO: transform template points
236  std::vector<std::size_t> vec;
237  return (vec);
238  }
239 
240 
241  protected:
242 
243  /** \brief Aligns the template points with the points found at the detection position of the specified detection.
244  * The detection ID refers to the last call of the method detect (...).
245  * \param[in] detection_id The ID of the detection (according to the last call of the method detect (...)).
246  */
247  inline std::vector<std::size_t>
248  alignTemplatePoints (const std::size_t detection_id)
249  {
250  if (detection_id >= detections_.size ())
251  PCL_ERROR ("ERROR pcl::LineRGBD::computeTransformedTemplatePoints - detection_id is out of bounds\n");
252 
253  // TODO: compute transform from detection
254  // TODO: transform template points
255  std::vector<std::size_t> vec;
256  return (vec);
257  }
258 
259  /** \brief Refines the found detections along the depth. */
260  void
261  refineDetectionsAlongDepth ();
262 
263  /** \brief Applies projective ICP on detections to find their correct position in depth. */
264  void
265  applyProjectiveDepthICPOnDetections ();
266 
267  /** \brief Checks for overlapping detections, removes them and keeps only the strongest one. */
268  void
269  removeOverlappingDetections ();
270 
271  /** \brief Computes the volume of the intersection between two bounding boxes.
272  * \param[in] box1 First bounding box.
273  * \param[in] box2 Second bounding box.
274  */
275  static float
276  computeBoundingBoxIntersectionVolume (const BoundingBoxXYZ &box1, const BoundingBoxXYZ &box2);
277 
278  private:
279  /** \brief Read another LTM header chunk. */
280  bool
281  readLTMHeader (int fd, pcl::io::TARHeader &header);
282 
283  /** \brief Intersection volume threshold. */
284  float intersection_volume_threshold_;
285 
286  /** \brief LINEMOD instance. */
288  /** \brief Color gradient modality. */
290  /** \brief Surface normal modality. */
292 
293  /** \brief XYZ point cloud. */
295  /** \brief RGB point cloud. */
297 
298  /** \brief Point clouds corresponding to the templates. */
300  /** \brief Bounding boxes corresponding to the templates. */
301  std::vector<pcl::BoundingBoxXYZ> bounding_boxes_;
302  /** \brief Object IDs corresponding to the templates. */
303  std::vector<std::size_t> object_ids_;
304 
305  /** \brief Detections from last call of method detect (...). */
306  std::vector<typename pcl::LineRGBD<PointXYZT, PointRGBT>::Detection> detections_;
307  };
308 
309 }
310 
311 #include <pcl/recognition/impl/linemod/line_rgbd.hpp>
Modality based on max-RGB gradients.
Template matching using the LINEMOD approach.
Definition: linemod.h:331
High-level class for template matching using the LINEMOD approach based on RGB and Depth data.
Definition: line_rgbd.h:73
void setDetectionThreshold(float threshold)
Sets the threshold for the detection responses.
Definition: line_rgbd.h:131
pcl::PointCloud< PointXYZT >::ConstPtr cloud_xyz_
XYZ point cloud.
Definition: line_rgbd.h:294
pcl::LINEMOD linemod_
LINEMOD instance.
Definition: line_rgbd.h:287
pcl::PointCloud< PointRGBT >::ConstPtr cloud_rgb_
RGB point cloud.
Definition: line_rgbd.h:296
std::vector< typename pcl::LineRGBD< PointXYZT, PointRGBT >::Detection > detections_
Detections from last call of method detect (...).
Definition: line_rgbd.h:306
void setGradientMagnitudeThreshold(const float threshold)
Sets the threshold on the magnitude of color gradients.
Definition: line_rgbd.h:141
std::vector< std::size_t > findObjectPointIndices(const std::size_t detection_id)
Finds the indices of the points in the input cloud which correspond to the specified detection.
Definition: line_rgbd.h:229
std::vector< pcl::BoundingBoxXYZ > bounding_boxes_
Bounding boxes corresponding to the templates.
Definition: line_rgbd.h:301
std::vector< std::size_t > object_ids_
Object IDs corresponding to the templates.
Definition: line_rgbd.h:303
pcl::ColorGradientModality< PointRGBT > color_gradient_mod_
Color gradient modality.
Definition: line_rgbd.h:289
pcl::SurfaceNormalModality< PointXYZT > surface_normal_mod_
Surface normal modality.
Definition: line_rgbd.h:291
std::vector< std::size_t > alignTemplatePoints(const std::size_t detection_id)
Aligns the template points with the points found at the detection position of the specified detection...
Definition: line_rgbd.h:248
virtual ~LineRGBD()=default
Destructor.
pcl::PointCloud< pcl::PointXYZRGBA >::CloudVectorType template_point_clouds_
Point clouds corresponding to the templates.
Definition: line_rgbd.h:299
void setIntersectionVolumeThreshold(const float threshold=1.0f)
Sets the threshold for the decision whether two detections of the same template are merged or not.
Definition: line_rgbd.h:154
void setInputColors(const typename pcl::PointCloud< PointRGBT >::ConstPtr &cloud)
Sets the input cloud with rgb values.
Definition: line_rgbd.h:175
void setInputCloud(const typename pcl::PointCloud< PointXYZT >::ConstPtr &cloud)
Sets the input cloud with xyz point coordinates.
Definition: line_rgbd.h:163
LineRGBD()
Constructor.
Definition: line_rgbd.h:97
std::vector< PointCloud< PointT >, Eigen::aligned_allocator< PointCloud< PointT > > > CloudVectorType
Definition: point_cloud.h:412
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
#define PCL_EXPORTS
Definition: pcl_macros.h:323
float width
Width of the bounding box.
Definition: line_rgbd.h:61
float x
X-coordinate of the upper left front point.
Definition: line_rgbd.h:54
float y
Y-coordinate of the upper left front point.
Definition: line_rgbd.h:56
float depth
Depth of the bounding box.
Definition: line_rgbd.h:65
BoundingBoxXYZ()
Constructor.
Definition: line_rgbd.h:51
float z
Z-coordinate of the upper left front point.
Definition: line_rgbd.h:58
float height
Height of the bounding box.
Definition: line_rgbd.h:63
A LineRGBD detection.
Definition: line_rgbd.h:78
std::size_t template_id
The ID of the template.
Definition: line_rgbd.h:83
BoundingBoxXYZ bounding_box
The 3D bounding box of the detection.
Definition: line_rgbd.h:91
std::size_t object_id
The ID of the object corresponding to the template.
Definition: line_rgbd.h:85
float response
The response of this detection.
Definition: line_rgbd.h:89
std::size_t detection_id
The ID of this detection.
Definition: line_rgbd.h:87
RegionXY region
The 2D template region of the detection.
Definition: line_rgbd.h:93
Detection()
Constructor.
Definition: line_rgbd.h:80
Defines a region in XY-space.
Definition: region_xy.h:82
A multi-modality template constructed from a set of quantized multi-modality features.
A TAR file's header, as described on http://en.wikipedia.org/wiki/Tar_%28file_format%29.
Definition: tar.h:50