Point Cloud Library (PCL)  1.7.1
object_recognition.h
1 #ifndef OBJECT_RECOGNITION_H_
2 #define OBJECT_RECOGNITION_H_
3 
4 #include "typedefs.h"
5 
6 #include "solution/filters.h"
7 #include "solution/segmentation.h"
8 #include "solution/feature_estimation.h"
9 #include "solution/registration.h"
10 
11 #include <pcl/io/pcd_io.h>
12 #include <pcl/kdtree/kdtree_flann.h>
13 
14 
16 {
17  // Filter parameters
18  float min_depth;
19  float max_depth;
23 
24  // Segmentation parameters
27  float cluster_tolerance;
28  int min_cluster_size;
29  int max_cluster_size;
30 
31  // Feature estimation parameters
33  float keypoints_min_scale;
38 
39  // Registration parameters
47 };
48 
49 struct ObjectModel
50 {
51  PointCloudPtr points;
52  PointCloudPtr keypoints;
53  LocalDescriptorsPtr local_descriptors;
54  GlobalDescriptorsPtr global_descriptor;
55 };
56 
58 {
59 public:
61  {}
62 
63  void
64  populateDatabase (const std::vector<std::string> & filenames)
65  {
66  }
67 
68  const ObjectModel &
69  recognizeObject (const PointCloudPtr & query_cloud)
70  {
71  int best_match = 0;
72  return (models_[best_match]);
73  }
74 
75  PointCloudPtr
76  recognizeAndAlignPoints (const PointCloudPtr & query_cloud)
77  {
78  PointCloudPtr output;
79  return (output);
80  }
81 
82  /* Construct an object model by filtering, segmenting, and estimating feature descriptors */
83  void
84  constructObjectModel (const PointCloudPtr & points, ObjectModel & output) const
85  {
86  output.points = applyFiltersAndSegment (points, params_);
87 
88  SurfaceNormalsPtr normals;
89  estimateFeatures (output.points, params_, normals, output.keypoints,
90  output.local_descriptors, output.global_descriptor);
91  }
92 
93 protected:
94  /* Apply a series of filters (threshold depth, downsample, and remove outliers) */
95  PointCloudPtr
96  applyFiltersAndSegment (const PointCloudPtr & input, const ObjectRecognitionParameters & params) const
97  {
98  PointCloudPtr cloud;
99  cloud = thresholdDepth (input, params.min_depth, params.max_depth);
100  cloud = downsample (cloud, params.downsample_leaf_size);
101  cloud = removeOutliers (cloud, params.outlier_rejection_radius, params.outlier_rejection_min_neighbors);
102 
103  cloud = findAndSubtractPlane (cloud, params.plane_inlier_distance_threshold, params.max_ransac_iterations);
104  std::vector<pcl::PointIndices> cluster_indices;
105  clusterObjects (cloud, params.cluster_tolerance, params.min_cluster_size,
106  params.max_cluster_size, cluster_indices);
107 
108  PointCloudPtr largest_cluster (new PointCloud);
109  pcl::copyPointCloud (*cloud, cluster_indices[0], *largest_cluster);
110 
111  return (largest_cluster);
112  }
113 
114  /* Estimate surface normals, keypoints, and local/global feature descriptors */
115  void
116  estimateFeatures (const PointCloudPtr & points, const ObjectRecognitionParameters & params,
117  SurfaceNormalsPtr & normals_out, PointCloudPtr & keypoints_out,
118  LocalDescriptorsPtr & local_descriptors_out, GlobalDescriptorsPtr & global_descriptor_out) const
119  {
120  normals_out = estimateSurfaceNormals (points, params.surface_normal_radius);
121 
122  keypoints_out = detectKeypoints (points, normals_out, params.keypoints_min_scale, params.keypoints_nr_octaves,
124 
125  local_descriptors_out = computeLocalDescriptors (points, normals_out, keypoints_out,
126  params.local_descriptor_radius);
127 
128  global_descriptor_out = computeGlobalDescriptor (points, normals_out);
129  }
130 
131  /* Align the points in the source model to the points in the target model */
132  PointCloudPtr
133  alignModelPoints (const ObjectModel & source, const ObjectModel & target,
134  const ObjectRecognitionParameters & params) const
135  {
136  Eigen::Matrix4f tform;
137  tform = computeInitialAlignment (source.keypoints, source.local_descriptors,
138  target.keypoints, target.local_descriptors,
142 
143  tform = refineAlignment (source.points, target.points, tform,
146 
147  PointCloudPtr output (new PointCloud);
148  pcl::transformPointCloud (*(source.points), *output, tform);
149 
150  return (output);
151  }
152 
154  std::vector<ObjectModel> models_;
155  GlobalDescriptorsPtr descriptors_;
157 };
158 
159 #endif