Point Cloud Library (PCL)  1.7.2
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 #ifndef PCL_RECOGNITION_LINEMOD_LINE_RGBD
39 #define PCL_RECOGNITION_LINEMOD_LINE_RGBD
40 
41 #include <pcl/recognition/linemod.h>
42 #include <pcl/recognition/color_gradient_modality.h>
43 #include <pcl/recognition/surface_normal_modality.h>
44 #include <pcl/io/tar.h>
45 
46 namespace pcl
47 {
48 
50  {
51  /** \brief Constructor. */
52  BoundingBoxXYZ () : x (0.0f), y (0.0f), z (0.0f), width (0.0f), height (0.0f), depth (0.0f) {}
53 
54  /** \brief X-coordinate of the upper left front point */
55  float x;
56  /** \brief Y-coordinate of the upper left front point */
57  float y;
58  /** \brief Z-coordinate of the upper left front point */
59  float z;
60 
61  /** \brief Width of the bounding box */
62  float width;
63  /** \brief Height of the bounding box */
64  float height;
65  /** \brief Depth of the bounding box */
66  float depth;
67  };
68 
69  /** \brief High-level class for template matching using the LINEMOD approach based on RGB and Depth data.
70  * \author Stefan Holzer
71  */
72  template <typename PointXYZT, typename PointRGBT=PointXYZT>
73  class PCL_EXPORTS LineRGBD
74  {
75  public:
76 
77  /** \brief A LineRGBD detection. */
78  struct Detection
79  {
80  /** \brief Constructor. */
81  Detection () : template_id (0), object_id (0), detection_id (0), response (0.0f), bounding_box () {}
82 
83  /** \brief The ID of the template. */
84  size_t template_id;
85  /** \brief The ID of the object corresponding to the template. */
86  size_t object_id;
87  /** \brief The ID of this detection. This is only valid for the last call of the method detect (...). */
88  size_t detection_id;
89  /** \brief The response of this detection. Responses are between 0 and 1, where 1 is best. */
90  float response;
91  /** \brief The 3D bounding box of the detection. */
93  /** \brief The 2D template region of the detection. */
95  };
96 
97  /** \brief Constructor */
99  : intersection_volume_threshold_ (1.0f)
100  , linemod_ ()
101  , color_gradient_mod_ ()
102  , surface_normal_mod_ ()
103  , cloud_xyz_ ()
104  , cloud_rgb_ ()
105  , template_point_clouds_ ()
106  , bounding_boxes_ ()
107  , object_ids_ ()
108  , detections_ ()
109  {
110  }
111 
112  /** \brief Destructor */
113  virtual ~LineRGBD ()
114  {
115  }
116 
117  /** \brief Loads templates from a LMT (LineMod Template) file. Overrides old templates.
118  *
119  * LineMod Template files are TAR files that store pairs of PCD datasets
120  * together with their LINEMOD signatures in \ref
121  * SparseQuantizedMultiModTemplate format.
122  *
123  * \param[in] file_name The name of the file that stores the templates.
124  * \param object_id
125  *
126  * \return true, if the operation was successful, false otherwise.
127  */
128  bool
129  loadTemplates (const std::string &file_name, size_t object_id = 0);
130 
131  bool
132  addTemplate (const SparseQuantizedMultiModTemplate & sqmmt, pcl::PointCloud<pcl::PointXYZRGBA> & cloud, size_t object_id = 0);
133 
134  /** \brief Sets the threshold for the detection responses. Responses are between 0 and 1, where 1 is a best.
135  * \param[in] threshold The threshold used to decide where a template is detected.
136  */
137  inline void
138  setDetectionThreshold (float threshold)
139  {
140  linemod_.setDetectionThreshold (threshold);
141  }
142 
143  /** \brief Sets the threshold on the magnitude of color gradients. Color gradients with a magnitude below
144  * this threshold are not considered in the detection process.
145  * \param[in] threshold The threshold on the magnitude of color gradients.
146  */
147  inline void
148  setGradientMagnitudeThreshold (const float threshold)
149  {
150  color_gradient_mod_.setGradientMagnitudeThreshold (threshold);
151  }
152 
153  /** \brief Sets the threshold for the decision whether two detections of the same template are merged or not.
154  * If ratio between the intersection of the bounding boxes of two detections and the original bounding
155  * boxes is larger than the specified threshold then they are merged. If detection A overlaps with
156  * detection B and B with C than A, B, and C are merged. Threshold has to be between 0 and 1.
157  * \param[in] threshold The threshold on the ratio between the intersection bounding box and the original
158  * bounding box.
159  */
160  inline void
161  setIntersectionVolumeThreshold (const float threshold = 1.0f)
162  {
163  intersection_volume_threshold_ = threshold;
164  }
165 
166  /** \brief Sets the input cloud with xyz point coordinates. The cloud has to be organized.
167  * \param[in] cloud The input cloud with xyz point coordinates.
168  */
169  inline void
171  {
172  cloud_xyz_ = cloud;
173 
174  surface_normal_mod_.setInputCloud (cloud);
175  surface_normal_mod_.processInputData ();
176  }
177 
178  /** \brief Sets the input cloud with rgb values. The cloud has to be organized.
179  * \param[in] cloud The input cloud with rgb values.
180  */
181  inline void
183  {
184  cloud_rgb_ = cloud;
185 
186  color_gradient_mod_.setInputCloud (cloud);
187  color_gradient_mod_.processInputData ();
188  }
189 
190  /** \brief Creates a template from the specified data and adds it to the matching queue.
191  * \param cloud
192  * \param object_id
193  * \param[in] mask_xyz the mask that determine which parts of the xyz-modality are used for creating the template.
194  * \param[in] mask_rgb the mask that determine which parts of the rgb-modality are used for creating the template.
195  * \param[in] region the region which will be associated with the template (can be larger than the actual modality-maps).
196  */
197  int
198  createAndAddTemplate (
200  const size_t object_id,
201  const MaskMap & mask_xyz,
202  const MaskMap & mask_rgb,
203  const RegionXY & region);
204 
205 
206  /** \brief Applies the detection process and fills the supplied vector with the detection instances.
207  * \param[out] detections The storage for the detection instances.
208  */
209  void
210  detect (std::vector<typename pcl::LineRGBD<PointXYZT, PointRGBT>::Detection> & detections);
211 
212  /** \brief Applies the detection process in a semi-scale-invariant manner. This is done by acutally
213  * scaling the template to different sizes.
214  */
215  void
216  detectSemiScaleInvariant (std::vector<typename pcl::LineRGBD<PointXYZT, PointRGBT>::Detection> & detections,
217  float min_scale = 0.6944444f,
218  float max_scale = 1.44f,
219  float scale_multiplier = 1.2f);
220 
221  /** \brief Computes and returns the point cloud of the specified detection. This is the template point
222  * cloud transformed to the detection coordinates. The detection ID refers to the last call of
223  * the method detect (...).
224  * \param[in] detection_id The ID of the detection (according to the last call of the method detect (...)).
225  * \param[out] cloud The storage for the transformed points.
226  */
227  void
228  computeTransformedTemplatePoints (const size_t detection_id,
230 
231  /** \brief Finds the indices of the points in the input cloud which correspond to the specified detection.
232  * The detection ID refers to the last call of the method detect (...).
233  * \param[in] detection_id The ID of the detection (according to the last call of the method detect (...)).
234  */
235  inline std::vector<size_t>
236  findObjectPointIndices (const size_t detection_id)
237  {
238  if (detection_id >= detections_.size ())
239  PCL_ERROR ("ERROR pcl::LineRGBD::computeTransformedTemplatePoints - detection_id is out of bounds\n");
240 
241  // TODO: compute transform from detection
242  // TODO: transform template points
243  std::vector<size_t> vec;
244  return (vec);
245  }
246 
247 
248  protected:
249 
250  /** \brief Aligns the template points with the points found at the detection position of the specified detection.
251  * The detection ID refers to the last call of the method detect (...).
252  * \param[in] detection_id The ID of the detection (according to the last call of the method detect (...)).
253  */
254  inline std::vector<size_t>
255  alignTemplatePoints (const size_t detection_id)
256  {
257  if (detection_id >= detections_.size ())
258  PCL_ERROR ("ERROR pcl::LineRGBD::computeTransformedTemplatePoints - detection_id is out of bounds\n");
259 
260  // TODO: compute transform from detection
261  // TODO: transform template points
262  std::vector<size_t> vec;
263  return (vec);
264  }
265 
266  /** \brief Refines the found detections along the depth. */
267  void
268  refineDetectionsAlongDepth ();
269 
270  /** \brief Applies projective ICP on detections to find their correct position in depth. */
271  void
272  applyProjectiveDepthICPOnDetections ();
273 
274  /** \brief Checks for overlapping detections, removes them and keeps only the strongest one. */
275  void
276  removeOverlappingDetections ();
277 
278  /** \brief Computes the volume of the intersection between two bounding boxes.
279  * \param[in] box1 First bounding box.
280  * \param[in] box2 Second bounding box.
281  */
282  static float
283  computeBoundingBoxIntersectionVolume (const BoundingBoxXYZ &box1, const BoundingBoxXYZ &box2);
284 
285  private:
286  /** \brief Read another LTM header chunk. */
287  bool
288  readLTMHeader (int fd, pcl::io::TARHeader &header);
289 
290  /** \brief Intersection volume threshold. */
291  float intersection_volume_threshold_;
292 
293  /** \brief LINEMOD instance. */
295  /** \brief Color gradient modality. */
297  /** \brief Surface normal modality. */
299 
300  /** \brief XYZ point cloud. */
302  /** \brief RGB point cloud. */
304 
305  /** \brief Point clouds corresponding to the templates. */
307  /** \brief Bounding boxes corresonding to the templates. */
308  std::vector<pcl::BoundingBoxXYZ> bounding_boxes_;
309  /** \brief Object IDs corresponding to the templates. */
310  std::vector<size_t> object_ids_;
311 
312  /** \brief Detections from last call of method detect (...). */
313  std::vector<typename pcl::LineRGBD<PointXYZT, PointRGBT>::Detection> detections_;
314  };
315 
316 }
317 
318 #include <pcl/recognition/impl/linemod/line_rgbd.hpp>
319 
320 #endif
Detection()
Constructor.
Definition: line_rgbd.h:81
float z
Z-coordinate of the upper left front point.
Definition: line_rgbd.h:59
A TAR file&#39;s header, as described on http://en.wikipedia.org/wiki/Tar_%28file_format%29.
Definition: tar.h:50
std::vector< PointCloud< PointT >, Eigen::aligned_allocator< PointCloud< PointT > > > CloudVectorType
Definition: point_cloud.h:427
std::vector< pcl::BoundingBoxXYZ > bounding_boxes_
Bounding boxes corresonding to the templates.
Definition: line_rgbd.h:308
pcl::ColorGradientModality< PointRGBT > color_gradient_mod_
Color gradient modality.
Definition: line_rgbd.h:296
Template matching using the LINEMOD approach.
Definition: linemod.h:335
pcl::PointCloud< pcl::PointXYZRGBA >::CloudVectorType template_point_clouds_
Point clouds corresponding to the templates.
Definition: line_rgbd.h:306
A LineRGBD detection.
Definition: line_rgbd.h:78
void setGradientMagnitudeThreshold(const float threshold)
Sets the threshold on the magnitude of color gradients.
Definition: line_rgbd.h:148
void setDetectionThreshold(float threshold)
Sets the threshold for the detection responses.
Definition: line_rgbd.h:138
Defines a region in XY-space.
Definition: region_xy.h:82
float y
Y-coordinate of the upper left front point.
Definition: line_rgbd.h:57
A multi-modality template constructed from a set of quantized multi-modality features.
float x
X-coordinate of the upper left front point.
Definition: line_rgbd.h:55
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:161
pcl::LINEMOD linemod_
LINEMOD instance.
Definition: line_rgbd.h:294
void setInputColors(const typename pcl::PointCloud< PointRGBT >::ConstPtr &cloud)
Sets the input cloud with rgb values.
Definition: line_rgbd.h:182
float width
Width of the bounding box.
Definition: line_rgbd.h:62
size_t detection_id
The ID of this detection.
Definition: line_rgbd.h:88
BoundingBoxXYZ()
Constructor.
Definition: line_rgbd.h:52
RegionXY region
The 2D template region of the detection.
Definition: line_rgbd.h:94
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
float depth
Depth of the bounding box.
Definition: line_rgbd.h:66
LineRGBD()
Constructor.
Definition: line_rgbd.h:98
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void setInputCloud(const typename pcl::PointCloud< PointXYZT >::ConstPtr &cloud)
Sets the input cloud with xyz point coordinates.
Definition: line_rgbd.h:170
pcl::PointCloud< PointRGBT >::ConstPtr cloud_rgb_
RGB point cloud.
Definition: line_rgbd.h:303
virtual ~LineRGBD()
Destructor.
Definition: line_rgbd.h:113
size_t object_id
The ID of the object corresponding to the template.
Definition: line_rgbd.h:86
std::vector< typename pcl::LineRGBD< PointXYZT, PointRGBT >::Detection > detections_
Detections from last call of method detect (...).
Definition: line_rgbd.h:313
pcl::SurfaceNormalModality< PointXYZT > surface_normal_mod_
Surface normal modality.
Definition: line_rgbd.h:298
std::vector< size_t > findObjectPointIndices(const size_t detection_id)
Finds the indices of the points in the input cloud which correspond to the specified detection...
Definition: line_rgbd.h:236
High-level class for template matching using the LINEMOD approach based on RGB and Depth data...
Definition: line_rgbd.h:73
std::vector< size_t > alignTemplatePoints(const size_t detection_id)
Aligns the template points with the points found at the detection position of the specified detection...
Definition: line_rgbd.h:255
size_t template_id
The ID of the template.
Definition: line_rgbd.h:84
std::vector< size_t > object_ids_
Object IDs corresponding to the templates.
Definition: line_rgbd.h:310
pcl::PointCloud< PointXYZT >::ConstPtr cloud_xyz_
XYZ point cloud.
Definition: line_rgbd.h:301
float response
The response of this detection.
Definition: line_rgbd.h:90
float height
Height of the bounding box.
Definition: line_rgbd.h:64
BoundingBoxXYZ bounding_box
The 3D bounding box of the detection.
Definition: line_rgbd.h:92