Point Cloud Library (PCL)  1.7.1
feature_estimation.h
1 #ifndef FEATURE_ESTIMATION_H
2 #define FEATURE_ESTIMATION_H
3 
4 #include "typedefs.h"
5 
6 #include <pcl/io/io.h>
7 #include <pcl/features/normal_3d.h>
8 #include <pcl/keypoints/sift_keypoint.h>
9 #include <pcl/features/fpfh.h>
10 #include <pcl/features/vfh.h>
11 #include <pcl/search/kdtree.h>
12 
13 /* Use NormalEstimation to estimate a cloud's surface normals
14  * Inputs:
15  * input
16  * The input point cloud
17  * radius
18  * The size of the local neighborhood used to estimate the surface
19  * Return: A pointer to a SurfaceNormals point cloud
20  */
21 SurfaceNormalsPtr
22 estimateSurfaceNormals (const PointCloudPtr & input, float radius)
23 {
26  normal_estimation.setRadiusSearch (radius);
27  normal_estimation.setInputCloud (input);
28  SurfaceNormalsPtr normals (new SurfaceNormals);
29  normal_estimation.compute (*normals);
30 
31  return (normals);
32 }
33 
34 /* Use SIFTKeypoint to detect a set of keypoints
35  * Inputs:
36  * points
37  * The input point cloud
38  * normals
39  * The input surface normals
40  * min_scale
41  * The smallest scale in the difference-of-Gaussians (DoG) scale-space
42  * nr_octaves
43  * The number of times the scale doubles in the DoG scale-space
44  * nr_scales_per_octave
45  * The number of scales computed for each doubling
46  * min_contrast
47  * The minimum local contrast that must be present for a keypoint to be detected
48  * Return: A pointer to a point cloud of keypoints
49  */
50 PointCloudPtr
51 detectKeypoints (const PointCloudPtr & points, const SurfaceNormalsPtr & normals,
52  float min_scale, int nr_octaves, int nr_scales_per_octave, float min_contrast)
53 {
56  sift_detect.setScales (min_scale, nr_octaves, nr_scales_per_octave);
57  sift_detect.setMinimumContrast (min_contrast);
58  sift_detect.setInputCloud (points);
60  sift_detect.compute (keypoints_temp);
61  PointCloudPtr keypoints (new PointCloud);
62  pcl::copyPointCloud (keypoints_temp, *keypoints);
63 
64  return (keypoints);
65 }
66 
67 /* Use FPFHEstimation to compute local feature descriptors around each keypoint
68  * Inputs:
69  * points
70  * The input point cloud
71  * normals
72  * The input surface normals
73  * keypoints
74  * A cloud of keypoints specifying the positions at which the descriptors should be computed
75  * feature_radius
76  * The size of the neighborhood from which the local descriptors will be computed
77  * Return: A pointer to a LocalDescriptors (a cloud of LocalDescriptorT points)
78  */
79 LocalDescriptorsPtr
80 computeLocalDescriptors (const PointCloudPtr & points, const SurfaceNormalsPtr & normals,
81  const PointCloudPtr & keypoints, float feature_radius)
82 {
85  fpfh_estimation.setRadiusSearch (feature_radius);
86  fpfh_estimation.setSearchSurface (points);
87  fpfh_estimation.setInputNormals (normals);
88  fpfh_estimation.setInputCloud (keypoints);
89  LocalDescriptorsPtr local_descriptors (new LocalDescriptors);
90  fpfh_estimation.compute (*local_descriptors);
91 
92  return (local_descriptors);
93 }
94 
95 /* Use VFHEstimation to compute a single global descriptor for the entire input cloud
96  * Inputs:
97  * points
98  * The input point cloud
99  * normals
100  * The input surface normals
101  * Return: A pointer to a GlobalDescriptors point cloud (a cloud containing a single GlobalDescriptorT point)
102  */
103 GlobalDescriptorsPtr
104 computeGlobalDescriptor (const PointCloudPtr & points, const SurfaceNormalsPtr & normals)
105 {
108  vfh_estimation.setInputCloud (points);
109  vfh_estimation.setInputNormals (normals);
110  GlobalDescriptorsPtr global_descriptor (new GlobalDescriptors);
111  vfh_estimation.compute (*global_descriptor);
112 
113  return (global_descriptor);
114 }
115 
116 /* A simple structure for storing all of a cloud's features */
117 struct ObjectFeatures
118 {
119  PointCloudPtr points;
120  SurfaceNormalsPtr normals;
121  PointCloudPtr keypoints;
122  LocalDescriptorsPtr local_descriptors;
123  GlobalDescriptorsPtr global_descriptor;
124 };
125 
126 /* Estimate normals, detect keypoints, and compute local and global descriptors
127  * Return: An ObjectFeatures struct containing all the features
128  */
130 computeFeatures (const PointCloudPtr & input)
131 {
132  ObjectFeatures features;
133  features.points = input;
134  features.normals = estimateSurfaceNormals (input, 0.05);
135  features.keypoints = detectKeypoints (input, features.normals, 0.005, 10, 8, 1.5);
136  features.local_descriptors = computeLocalDescriptors (input, features.normals, features.keypoints, 0.1);
137  features.global_descriptor = computeGlobalDescriptor (input, features.normals);
138 
139  return (features);
140 }
141 
142 #endif