Point Cloud Library (PCL)  1.7.0
harris_2d.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  * $Id$
37  *
38  */
39 
40 #ifndef PCL_HARRIS_KEYPOINT_2D_H_
41 #define PCL_HARRIS_KEYPOINT_2D_H_
42 
43 #include <pcl/keypoints/keypoint.h>
44 #include <pcl/common/intensity.h>
45 
46 namespace pcl
47 {
48  /** \brief HarrisKeypoint2D detects Harris corners family points
49  *
50  * \author Nizar Sallem
51  * \ingroup keypoints
52  */
53  template <typename PointInT, typename PointOutT, typename IntensityT = pcl::common::IntensityFieldAccessor<PointInT> >
54  class HarrisKeypoint2D : public Keypoint<PointInT, PointOutT>
55  {
56  public:
57  typedef boost::shared_ptr<HarrisKeypoint2D<PointInT, PointOutT, IntensityT> > Ptr;
58  typedef boost::shared_ptr<const HarrisKeypoint2D<PointInT, PointOutT, IntensityT> > ConstPtr;
59 
64 
68 
69  typedef enum {HARRIS = 1, NOBLE, LOWE, TOMASI} ResponseMethod;
70 
71  /** \brief Constructor
72  * \param[in] method the method to be used to determine the corner responses
73  * \param[in] threshold the threshold to filter out weak corners
74  */
75  HarrisKeypoint2D (ResponseMethod method = HARRIS, int window_width = 3, int window_height = 3, int min_distance = 5, float threshold = 0.0)
76  : threshold_ (threshold)
77  , refine_ (false)
78  , nonmax_ (true)
79  , method_ (method)
80  , threads_ (0)
81  , response_ (new pcl::PointCloud<PointOutT> ())
82  , window_width_ (window_width)
83  , window_height_ (window_height)
84  , skipped_pixels_ (0)
85  , min_distance_ (min_distance)
86  {
87  name_ = "HarrisKeypoint2D";
88  }
89 
90  /** \brief set the method of the response to be calculated.
91  * \param[in] type
92  */
93  void setMethod (ResponseMethod type);
94 
95  ///Set window width
96  void setWindowWidth (int window_width);
97 
98  ///Set window height
99  void setWindowHeight (int window_height);
100 
101  ///Set number of pixels to skip
102  void setSkippedPixels (int skipped_pixels);
103 
104  ///Set minimal distance between candidate keypoints
105  void setMinimalDistance (int min_distance);
106 
107  /** \brief set the threshold value for detecting corners. This is only evaluated if non maxima suppression is turned on.
108  * \brief note non maxima suppression needs to be activated in order to use this feature.
109  * \param[in] threshold
110  */
111  void setThreshold (float threshold);
112 
113  /** \brief whether non maxima suppression should be applied or the response for each point should be returned
114  * \note this value needs to be turned on in order to apply thresholding and refinement
115  * \param[in] nonmax default is false
116  */
117  void setNonMaxSupression (bool = false);
118 
119  /** \brief whether the detected key points should be refined or not. If turned of, the key points are a subset of
120  * the original point cloud. Otherwise the key points may be arbitrary.
121  * \brief note non maxima supression needs to be on in order to use this feature.
122  * \param[in] do_refine
123  */
124  void setRefine (bool do_refine);
125 
126  /** \brief Initialize the scheduler and set the number of threads to use.
127  * \param nr_threads the number of hardware threads to use (0 sets the value back to automatic)
128  */
129  inline void
130  setNumberOfThreads (unsigned int nr_threads = 0) { threads_ = nr_threads; }
131 
132  protected:
133  bool
134  initCompute ();
135  void
136  detectKeypoints (PointCloudOut &output);
137  /** \brief gets the corner response for valid input points*/
138  void
139  responseHarris (PointCloudOut &output, float& highest_response) const;
140  void
141  responseNoble (PointCloudOut &output, float& highest_response) const;
142  void
143  responseLowe (PointCloudOut &output, float& highest_response) const;
144  void
145  responseTomasi (PointCloudOut &output, float& highest_response) const;
146 // void refineCorners (PointCloudOut &corners) const;
147  /** \brief calculates the upper triangular part of unnormalized
148  * covariance matrix over intensities given by the 2D coordinates
149  * and window_width_ and window_height_
150  */
151  void
152  computeSecondMomentMatrix (std::size_t pos, float* coefficients) const;
153  /// threshold for non maxima suppression
154  float threshold_;
155  /// corner refinement
156  bool refine_;
157  /// non maximas suppression
158  bool nonmax_;
159  /// cornerness computation methode
161  /// number of threads to be used
162  unsigned int threads_;
163 
164  private:
165  Eigen::MatrixXf derivatives_rows_;
166  Eigen::MatrixXf derivatives_cols_;
167  /// intermediate holder for computed responses
168  boost::shared_ptr<pcl::PointCloud<PointOutT> > response_;
169  /// comparator for responses intensity
170  bool
171  greaterIntensityAtIndices (int a, int b) const
172  {
173  return (response_->at (a).intensity > response_->at (b).intensity);
174  }
175  /// Window width
176  int window_width_;
177  /// Window height
178  int window_height_;
179  /// half window width
180  int half_window_width_;
181  /// half window height
182  int half_window_height_;
183  /// number of pixels to skip within search window
184  int skipped_pixels_;
185  /// minimum distance between two keypoints
186  int min_distance_;
187  /// intensity field accessor
188  IntensityT intensity_;
189  };
190 }
191 
192 #include <pcl/keypoints/impl/harris_2d.hpp>
193 
194 #endif // #ifndef PCL_HARRIS_KEYPOINT_2D_H_