Point Cloud Library (PCL)  1.7.0
sift_keypoint.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #ifndef PCL_SIFT_KEYPOINT_H_
37 #define PCL_SIFT_KEYPOINT_H_
38 
39 #include <pcl/keypoints/keypoint.h>
40 
41 namespace pcl
42 {
43  template<typename PointT>
45  {
46  inline float
47  operator () (const PointT & p) const
48  {
49  return p.intensity;
50  }
51  };
52  template<>
54  {
55  inline float
56  operator () (const PointNormal & p) const
57  {
58  return p.curvature;
59  }
60  };
61  template<>
63  {
64  inline float
65  operator () (const PointXYZRGB & p) const
66  {
67  return (static_cast<float> (299*p.r + 587*p.g + 114*p.b) / 1000.0f);
68  }
69  };
70  template<>
72  {
73  inline float
74  operator () (const PointXYZRGBA & p) const
75  {
76  return (static_cast<float> (299*p.r + 587*p.g + 114*p.b) / 1000.0f);
77  }
78  };
79 
80  /** \brief @b SIFTKeypoint detects the Scale Invariant Feature Transform
81  * keypoints for a given point cloud dataset containing points and intensity.
82  * This implementation adapts the original algorithm from images to point
83  * clouds.
84  *
85  * For more information about the image-based SIFT interest operator, see:
86  *
87  * David G. Lowe, "Distinctive image features from scale-invariant keypoints,"
88  * International Journal of Computer Vision, 60, 2 (2004), pp. 91-110.
89  *
90  * \author Michael Dixon
91  * \ingroup keypoints
92  */
93  template <typename PointInT, typename PointOutT>
94  class SIFTKeypoint : public Keypoint<PointInT, PointOutT>
95  {
96  public:
97  typedef boost::shared_ptr<SIFTKeypoint<PointInT, PointOutT> > Ptr;
98  typedef boost::shared_ptr<const SIFTKeypoint<PointInT, PointOutT> > ConstPtr;
99 
103 
110 
111  /** \brief Empty constructor. */
112  SIFTKeypoint () : min_scale_ (0.0), nr_octaves_ (0), nr_scales_per_octave_ (0),
113  min_contrast_ (-std::numeric_limits<float>::max ()), scale_idx_ (-1),
114  out_fields_ (), getFieldValue_ ()
115  {
116  name_ = "SIFTKeypoint";
117  }
118 
119  /** \brief Specify the range of scales over which to search for keypoints
120  * \param min_scale the standard deviation of the smallest scale in the scale space
121  * \param nr_octaves the number of octaves (i.e. doublings of scale) to compute
122  * \param nr_scales_per_octave the number of scales to compute within each octave
123  */
124  void
125  setScales (float min_scale, int nr_octaves, int nr_scales_per_octave);
126 
127  /** \brief Provide a threshold to limit detection of keypoints without sufficient contrast
128  * \param min_contrast the minimum contrast required for detection
129  */
130  void
131  setMinimumContrast (float min_contrast);
132 
133  protected:
134  bool
135  initCompute ();
136 
137  /** \brief Detect the SIFT keypoints for a set of points given in setInputCloud () using the spatial locator in
138  * setSearchMethod ().
139  * \param output the resultant cloud of keypoints
140  */
141  void
142  detectKeypoints (PointCloudOut &output);
143 
144  private:
145  /** \brief Detect the SIFT keypoints for a given point cloud for a single octave.
146  * \param input the point cloud to detect keypoints in
147  * \param tree a k-D tree of the points in \a input
148  * \param base_scale the first (smallest) scale in the octave
149  * \param nr_scales_per_octave the number of scales to to compute
150  * \param output the resultant point cloud containing the SIFT keypoints
151  */
152  void
153  detectKeypointsForOctave (const PointCloudIn &input, KdTree &tree,
154  float base_scale, int nr_scales_per_octave,
155  PointCloudOut &output);
156 
157  /** \brief Compute the difference-of-Gaussian (DoG) scale space for the given input and scales
158  * \param input the point cloud for which the DoG scale space will be computed
159  * \param tree a k-D tree of the points in \a input
160  * \param scales a vector containing the scales over which to compute the DoG scale space
161  * \param diff_of_gauss the resultant DoG scale space (in a number-of-points by number-of-scales matrix)
162  */
163  void
164  computeScaleSpace (const PointCloudIn &input, KdTree &tree,
165  const std::vector<float> &scales,
166  Eigen::MatrixXf &diff_of_gauss);
167 
168  /** \brief Find the local minima and maxima in the provided difference-of-Gaussian (DoG) scale space
169  * \param input the input point cloud
170  * \param tree a k-D tree of the points in \a input
171  * \param diff_of_gauss the DoG scale space (in a number-of-points by number-of-scales matrix)
172  * \param extrema_indices the resultant vector containing the point indices of each keypoint
173  * \param extrema_scales the resultant vector containing the scale indices of each keypoint
174  */
175  void
176  findScaleSpaceExtrema (const PointCloudIn &input, KdTree &tree,
177  const Eigen::MatrixXf &diff_of_gauss,
178  std::vector<int> &extrema_indices, std::vector<int> &extrema_scales);
179 
180 
181  /** \brief The standard deviation of the smallest scale in the scale space.*/
182  float min_scale_;
183 
184  /** \brief The number of octaves (i.e. doublings of scale) over which to search for keypoints.*/
185  int nr_octaves_;
186 
187  /** \brief The number of scales to be computed for each octave.*/
188  int nr_scales_per_octave_;
189 
190  /** \brief The minimum contrast required for detection.*/
191  float min_contrast_;
192 
193  /** \brief Set to a value different than -1 if the output cloud has a "scale" field and we have to save
194  * the keypoints scales. */
195  int scale_idx_;
196 
197  /** \brief The list of fields present in the output point cloud data. */
198  std::vector<pcl::PCLPointField> out_fields_;
199 
201  };
202 }
203 
204 #include <pcl/keypoints/impl/sift_keypoint.hpp>
205 
206 #endif // #ifndef PCL_SIFT_KEYPOINT_H_