Point Cloud Library (PCL)  1.11.1
harris_6d.hpp
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_HARRIS_KEYPOINT_6D_IMPL_H_
39 #define PCL_HARRIS_KEYPOINT_6D_IMPL_H_
40 
41 #include <pcl/keypoints/harris_6d.h>
42 #include <pcl/common/io.h>
43 #include <pcl/features/normal_3d.h>
44 //#include <pcl/features/fast_intensity_gradient.h>
45 #include <pcl/features/intensity_gradient.h>
46 #include <pcl/features/integral_image_normal.h>
47 
48 template <typename PointInT, typename PointOutT, typename NormalT> void
50 {
51  threshold_= threshold;
52 }
53 
54 template <typename PointInT, typename PointOutT, typename NormalT> void
56 {
57  search_radius_ = radius;
58 }
59 
60 template <typename PointInT, typename PointOutT, typename NormalT> void
62 {
63  refine_ = do_refine;
64 }
65 
66 template <typename PointInT, typename PointOutT, typename NormalT> void
68 {
69  nonmax_ = nonmax;
70 }
71 
72 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73 template <typename PointInT, typename PointOutT, typename NormalT> void
74 pcl::HarrisKeypoint6D<PointInT, PointOutT, NormalT>::calculateCombinedCovar (const std::vector<int>& neighbors, float* coefficients) const
75 {
76  memset (coefficients, 0, sizeof (float) * 21);
77  unsigned count = 0;
78  for (const int &neighbor : neighbors)
79  {
80  if (std::isfinite ((*normals_)[neighbor].normal_x) && std::isfinite ((*intensity_gradients_)[neighbor].gradient [0]))
81  {
82  coefficients[ 0] += (*normals_)[neighbor].normal_x * (*normals_)[neighbor].normal_x;
83  coefficients[ 1] += (*normals_)[neighbor].normal_x * (*normals_)[neighbor].normal_y;
84  coefficients[ 2] += (*normals_)[neighbor].normal_x * (*normals_)[neighbor].normal_z;
85  coefficients[ 3] += (*normals_)[neighbor].normal_x * (*intensity_gradients_)[neighbor].gradient [0];
86  coefficients[ 4] += (*normals_)[neighbor].normal_x * (*intensity_gradients_)[neighbor].gradient [1];
87  coefficients[ 5] += (*normals_)[neighbor].normal_x * (*intensity_gradients_)[neighbor].gradient [2];
88 
89  coefficients[ 6] += (*normals_)[neighbor].normal_y * (*normals_)[neighbor].normal_y;
90  coefficients[ 7] += (*normals_)[neighbor].normal_y * (*normals_)[neighbor].normal_z;
91  coefficients[ 8] += (*normals_)[neighbor].normal_y * (*intensity_gradients_)[neighbor].gradient [0];
92  coefficients[ 9] += (*normals_)[neighbor].normal_y * (*intensity_gradients_)[neighbor].gradient [1];
93  coefficients[10] += (*normals_)[neighbor].normal_y * (*intensity_gradients_)[neighbor].gradient [2];
94 
95  coefficients[11] += (*normals_)[neighbor].normal_z * (*normals_)[neighbor].normal_z;
96  coefficients[12] += (*normals_)[neighbor].normal_z * (*intensity_gradients_)[neighbor].gradient [0];
97  coefficients[13] += (*normals_)[neighbor].normal_z * (*intensity_gradients_)[neighbor].gradient [1];
98  coefficients[14] += (*normals_)[neighbor].normal_z * (*intensity_gradients_)[neighbor].gradient [2];
99 
100  coefficients[15] += (*intensity_gradients_)[neighbor].gradient [0] * (*intensity_gradients_)[neighbor].gradient [0];
101  coefficients[16] += (*intensity_gradients_)[neighbor].gradient [0] * (*intensity_gradients_)[neighbor].gradient [1];
102  coefficients[17] += (*intensity_gradients_)[neighbor].gradient [0] * (*intensity_gradients_)[neighbor].gradient [2];
103 
104  coefficients[18] += (*intensity_gradients_)[neighbor].gradient [1] * (*intensity_gradients_)[neighbor].gradient [1];
105  coefficients[19] += (*intensity_gradients_)[neighbor].gradient [1] * (*intensity_gradients_)[neighbor].gradient [2];
106 
107  coefficients[20] += (*intensity_gradients_)[neighbor].gradient [2] * (*intensity_gradients_)[neighbor].gradient [2];
108 
109  ++count;
110  }
111  }
112  if (count > 0)
113  {
114  float norm = 1.0 / float (count);
115  coefficients[ 0] *= norm;
116  coefficients[ 1] *= norm;
117  coefficients[ 2] *= norm;
118  coefficients[ 3] *= norm;
119  coefficients[ 4] *= norm;
120  coefficients[ 5] *= norm;
121  coefficients[ 6] *= norm;
122  coefficients[ 7] *= norm;
123  coefficients[ 8] *= norm;
124  coefficients[ 9] *= norm;
125  coefficients[10] *= norm;
126  coefficients[11] *= norm;
127  coefficients[12] *= norm;
128  coefficients[13] *= norm;
129  coefficients[14] *= norm;
130  coefficients[15] *= norm;
131  coefficients[16] *= norm;
132  coefficients[17] *= norm;
133  coefficients[18] *= norm;
134  coefficients[19] *= norm;
135  coefficients[20] *= norm;
136  }
137 }
138 
139 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140 template <typename PointInT, typename PointOutT, typename NormalT> void
142 {
143  if (normals_->empty ())
144  {
145  normals_->reserve (surface_->size ());
146  if (!surface_->isOrganized ())
147  {
149  normal_estimation.setInputCloud (surface_);
150  normal_estimation.setRadiusSearch (search_radius_);
151  normal_estimation.compute (*normals_);
152  }
153  else
154  {
157  normal_estimation.setInputCloud (surface_);
158  normal_estimation.setNormalSmoothingSize (5.0);
159  normal_estimation.compute (*normals_);
160  }
161  }
162 
164  cloud->resize (surface_->size ());
165 #pragma omp parallel for \
166  default(none) \
167  num_threads(threads_)
168  for (unsigned idx = 0; idx < surface_->size (); ++idx)
169  {
170  cloud->points [idx].x = surface_->points [idx].x;
171  cloud->points [idx].y = surface_->points [idx].y;
172  cloud->points [idx].z = surface_->points [idx].z;
173  //grayscale = 0.2989 * R + 0.5870 * G + 0.1140 * B
174 
175  cloud->points [idx].intensity = 0.00390625 * (0.114 * float(surface_->points [idx].b) + 0.5870 * float(surface_->points [idx].g) + 0.2989 * float(surface_->points [idx].r));
176  }
177  pcl::copyPointCloud (*surface_, *cloud);
178 
180  grad_est.setInputCloud (cloud);
181  grad_est.setInputNormals (normals_);
182  grad_est.setRadiusSearch (search_radius_);
183  grad_est.compute (*intensity_gradients_);
184 
185 #pragma omp parallel for \
186  default(none) \
187  num_threads(threads_)
188  for (std::size_t idx = 0; idx < intensity_gradients_->size (); ++idx)
189  {
190  float len = intensity_gradients_->points [idx].gradient_x * intensity_gradients_->points [idx].gradient_x +
191  intensity_gradients_->points [idx].gradient_y * intensity_gradients_->points [idx].gradient_y +
192  intensity_gradients_->points [idx].gradient_z * intensity_gradients_->points [idx].gradient_z ;
193 
194  // Suat: ToDo: remove this magic number or expose using set/get
195  if (len > 200.0)
196  {
197  len = 1.0 / sqrt (len);
198  intensity_gradients_->points [idx].gradient_x *= len;
199  intensity_gradients_->points [idx].gradient_y *= len;
200  intensity_gradients_->points [idx].gradient_z *= len;
201  }
202  else
203  {
204  intensity_gradients_->points [idx].gradient_x = 0;
205  intensity_gradients_->points [idx].gradient_y = 0;
206  intensity_gradients_->points [idx].gradient_z = 0;
207  }
208  }
209 
211  response->points.reserve (input_->size());
212  responseTomasi(*response);
213 
214  // just return the response
215  if (!nonmax_)
216  {
217  output = *response;
218  // we do not change the denseness in this case
219  output.is_dense = input_->is_dense;
220  for (std::size_t i = 0; i < response->size (); ++i)
221  keypoints_indices_->indices.push_back (i);
222  }
223  else
224  {
225  output.points.clear ();
226  output.points.reserve (response->size());
227 
228 #pragma omp parallel for \
229  default(none) \
230  num_threads(threads_)
231  for (std::size_t idx = 0; idx < response->size (); ++idx)
232  {
233  if (!isFinite ((*response)[idx]) || (*response)[idx].intensity < threshold_)
234  continue;
235 
236  std::vector<int> nn_indices;
237  std::vector<float> nn_dists;
238  tree_->radiusSearch (idx, search_radius_, nn_indices, nn_dists);
239  bool is_maxima = true;
240  for (std::vector<int>::const_iterator iIt = nn_indices.begin(); iIt != nn_indices.end(); ++iIt)
241  {
242  if ((*response)[idx].intensity < (*response)[*iIt].intensity)
243  {
244  is_maxima = false;
245  break;
246  }
247  }
248  if (is_maxima)
249  #pragma omp critical
250  {
251  output.points.push_back ((*response)[idx]);
252  keypoints_indices_->indices.push_back (idx);
253  }
254  }
255 
256  if (refine_)
257  refineCorners (output);
258 
259  output.height = 1;
260  output.width = output.size();
261  output.is_dense = true;
262  }
263 }
264 
265 template <typename PointInT, typename PointOutT, typename NormalT> void
267 {
268  // get the 6x6 covar-mat
269  PointOutT pointOut;
270  PCL_ALIGN (16) float covar [21];
271  Eigen::SelfAdjointEigenSolver <Eigen::Matrix<float, 6, 6> > solver;
272  Eigen::Matrix<float, 6, 6> covariance;
273 
274 #pragma omp parallel for \
275  default(none) \
276  firstprivate(pointOut, covar, covariance, solver) \
277  num_threads(threads_)
278  for (unsigned pIdx = 0; pIdx < input_->size (); ++pIdx)
279  {
280  const PointInT& pointIn = input_->points [pIdx];
281  pointOut.intensity = 0.0; //std::numeric_limits<float>::quiet_NaN ();
282  if (isFinite (pointIn))
283  {
284  std::vector<int> nn_indices;
285  std::vector<float> nn_dists;
286  tree_->radiusSearch (pointIn, search_radius_, nn_indices, nn_dists);
287  calculateCombinedCovar (nn_indices, covar);
288 
289  float trace = covar [0] + covar [6] + covar [11] + covar [15] + covar [18] + covar [20];
290  if (trace != 0)
291  {
292  covariance.coeffRef ( 0) = covar [ 0];
293  covariance.coeffRef ( 1) = covar [ 1];
294  covariance.coeffRef ( 2) = covar [ 2];
295  covariance.coeffRef ( 3) = covar [ 3];
296  covariance.coeffRef ( 4) = covar [ 4];
297  covariance.coeffRef ( 5) = covar [ 5];
298 
299  covariance.coeffRef ( 7) = covar [ 6];
300  covariance.coeffRef ( 8) = covar [ 7];
301  covariance.coeffRef ( 9) = covar [ 8];
302  covariance.coeffRef (10) = covar [ 9];
303  covariance.coeffRef (11) = covar [10];
304 
305  covariance.coeffRef (14) = covar [11];
306  covariance.coeffRef (15) = covar [12];
307  covariance.coeffRef (16) = covar [13];
308  covariance.coeffRef (17) = covar [14];
309 
310  covariance.coeffRef (21) = covar [15];
311  covariance.coeffRef (22) = covar [16];
312  covariance.coeffRef (23) = covar [17];
313 
314  covariance.coeffRef (28) = covar [18];
315  covariance.coeffRef (29) = covar [19];
316 
317  covariance.coeffRef (35) = covar [20];
318 
319  covariance.coeffRef ( 6) = covar [ 1];
320 
321  covariance.coeffRef (12) = covar [ 2];
322  covariance.coeffRef (13) = covar [ 7];
323 
324  covariance.coeffRef (18) = covar [ 3];
325  covariance.coeffRef (19) = covar [ 8];
326  covariance.coeffRef (20) = covar [12];
327 
328  covariance.coeffRef (24) = covar [ 4];
329  covariance.coeffRef (25) = covar [ 9];
330  covariance.coeffRef (26) = covar [13];
331  covariance.coeffRef (27) = covar [16];
332 
333  covariance.coeffRef (30) = covar [ 5];
334  covariance.coeffRef (31) = covar [10];
335  covariance.coeffRef (32) = covar [14];
336  covariance.coeffRef (33) = covar [17];
337  covariance.coeffRef (34) = covar [19];
338 
339  solver.compute (covariance);
340  pointOut.intensity = solver.eigenvalues () [3];
341  }
342  }
343 
344  pointOut.x = pointIn.x;
345  pointOut.y = pointIn.y;
346  pointOut.z = pointIn.z;
347 
348  #pragma omp critical
349  output.points.push_back(pointOut);
350  }
351  output.height = input_->height;
352  output.width = input_->width;
353 }
354 
355 template <typename PointInT, typename PointOutT, typename NormalT> void
357 {
359  search.setInputCloud(surface_);
360 
361  Eigen::Matrix3f nnT;
362  Eigen::Matrix3f NNT;
363  Eigen::Vector3f NNTp;
364  const Eigen::Vector3f* normal;
365  const Eigen::Vector3f* point;
366  float diff;
367  const unsigned max_iterations = 10;
368  for (typename PointCloudOut::iterator cornerIt = corners.begin(); cornerIt != corners.end(); ++cornerIt)
369  {
370  unsigned iterations = 0;
371  do {
372  NNT.setZero();
373  NNTp.setZero();
374  PointInT corner;
375  corner.x = cornerIt->x;
376  corner.y = cornerIt->y;
377  corner.z = cornerIt->z;
378  std::vector<int> nn_indices;
379  std::vector<float> nn_dists;
380  search.radiusSearch (corner, search_radius_, nn_indices, nn_dists);
381  for (std::vector<int>::const_iterator iIt = nn_indices.begin(); iIt != nn_indices.end(); ++iIt)
382  {
383  normal = reinterpret_cast<const Eigen::Vector3f*> (&((*normals_)[*iIt].normal_x));
384  point = reinterpret_cast<const Eigen::Vector3f*> (&((*surface_)[*iIt].x));
385  nnT = (*normal) * (normal->transpose());
386  NNT += nnT;
387  NNTp += nnT * (*point);
388  }
389  if (NNT.determinant() != 0)
390  *(reinterpret_cast<Eigen::Vector3f*>(&(cornerIt->x))) = NNT.inverse () * NNTp;
391 
392  diff = (cornerIt->x - corner.x) * (cornerIt->x - corner.x) +
393  (cornerIt->y - corner.y) * (cornerIt->y - corner.y) +
394  (cornerIt->z - corner.z) * (cornerIt->z - corner.z);
395 
396  } while (diff > 1e-6 && ++iterations < max_iterations);
397  }
398 }
399 
400 #define PCL_INSTANTIATE_HarrisKeypoint6D(T,U,N) template class PCL_EXPORTS pcl::HarrisKeypoint6D<T,U,N>;
401 #endif // #ifndef PCL_HARRIS_KEYPOINT_6D_IMPL_H_
402 
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:61
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:429
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:411
void setNonMaxSupression(bool=false)
whether non maxima suppression should be applied or the response for each point should be returned ...
Definition: harris_6d.hpp:67
int radiusSearch(const PointT &point, double radius, Indices &k_indices, std::vector< float > &k_sqr_distances, unsigned int max_nn=0) const override
Search for all the nearest neighbors of the query point in a given radius.
Definition: kdtree.hpp:96
std::size_t size() const
Definition: point_cloud.h:459
void setRadiusSearch(double radius)
Set the sphere radius that is to be used for determining the nearest neighbors used for the feature e...
Definition: feature.h:201
NormalEstimation estimates local surface properties (surface normals and curvatures)at each 3D point...
Definition: normal_3d.h:243
void detectKeypoints(PointCloudOut &output)
Definition: harris_6d.hpp:141
void copyPointCloud(const pcl::PointCloud< PointInT > &cloud_in, pcl::PointCloud< PointOutT > &cloud_out)
Copy all the fields from a given point cloud into a new point cloud.
Definition: io.hpp:121
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
Definition: kdtree.hpp:76
Surface normal estimation on organized data using integral images.
void setRadius(float radius)
set the radius for normal estimation and non maxima supression.
Definition: harris_6d.hpp:55
void refineCorners(PointCloudOut &corners) const
Definition: harris_6d.hpp:356
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition: point_cloud.h:478
void setRefine(bool do_refine)
whether the detected key points should be refined or not.
Definition: harris_6d.hpp:61
void setNormalEstimationMethod(NormalEstimationMethod normal_estimation_method)
Set the normal estimation method.
IntensityGradientEstimation estimates the intensity gradient for a point cloud that contains position...
void setThreshold(float threshold)
set the threshold value for detecting corners.
Definition: harris_6d.hpp:49
void setInputCloud(const typename PointCloudIn::ConstPtr &cloud) override
Provide a pointer to the input dataset (overwrites the PCLBase::setInputCloud method) ...
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:65
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:419
void compute(PointCloudOut &output)
Base method for feature estimation for all points given in <setInputCloud (), setIndices ()> using th...
Definition: feature.hpp:193
typename Keypoint< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: harris_6d.h:56
void setInputCloud(const PointCloudConstPtr &cloud) override
Provide a pointer to the input dataset.
Definition: normal_3d.h:332
void setInputNormals(const PointCloudNConstPtr &normals)
Provide a pointer to the input dataset that contains the point normals of the XYZ dataset...
Definition: feature.h:345
void calculateCombinedCovar(const std::vector< int > &neighbors, float *coefficients) const
Definition: harris_6d.hpp:74
void setNormalSmoothingSize(float normal_smoothing_size)
Set the normal smoothing size.
void responseTomasi(PointCloudOut &output) const
Definition: harris_6d.hpp:266