-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature.cpp
77 lines (65 loc) · 2.59 KB
/
feature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <pcl/features/normal_3d.h>
#include <pcl/keypoints/sift_keypoint.h>
#include <pcl/features/fpfh.h>
#include <pcl/kdtree/kdtree_flann.h>
#include "feature.h"
NormalCloudPtr compute_normals(const PointCloudConstPtr &cloud, float radius)
{
pcl::NormalEstimation<PointT, NormalT> ne;
pcl::search::KdTree<PointT>::Ptr tree(new pcl::search::KdTree<PointT>);
NormalCloudPtr cloud_normal(new NormalCloud);
ne.setInputCloud(cloud);
ne.setSearchMethod(tree);
ne.setRadiusSearch(radius);
ne.compute(*cloud_normal);
return cloud_normal;
}
PointCloudPtr compute_keypoints(const PointCloudPtr &cloud,
float min_scale, int nr_octaves, int nr_scales_per_octave, float min_contrast)
{
pcl::SIFTKeypoint<PointT, pcl::PointWithScale> sift_detect;
sift_detect.setSearchMethod(pcl::search::Search<PointT>::Ptr(new pcl::search::KdTree<PointT>));
sift_detect.setScales(min_scale, nr_octaves, nr_scales_per_octave);
sift_detect.setMinimumContrast(min_contrast);
sift_detect.setInputCloud(cloud);
pcl::PointCloud<pcl::PointWithScale> keypoints_temp;
sift_detect.compute(keypoints_temp);
PointCloudPtr keypoints(new PointCloud);
pcl::copyPointCloud(keypoints_temp, *keypoints);
return keypoints;
}
FeatureCloudPtr compute_feature_descriptors(const PointCloudConstPtr &cloud,
const NormalCloudConstPtr &normals, const PointCloudConstPtr &keypoints,
float radius)
{
pcl::FPFHEstimation<PointT, NormalT, FeatureT> fpfh;
pcl::search::KdTree<PointT>::Ptr tree(new pcl::search::KdTree<PointT>);
FeatureCloudPtr fpfhs(new FeatureCloud);
fpfh.setSearchMethod(tree);
fpfh.setRadiusSearch(radius);
fpfh.setSearchSurface(cloud);
fpfh.setInputNormals(normals);
fpfh.setInputCloud(keypoints);
fpfh.compute(*fpfhs);
return fpfhs;
}
void find_feature_correspondences(const FeatureCloudConstPtr &source_features,
const FeatureCloudConstPtr &target_features,
std::vector<int> &correspondences, std::vector<int> &correspondence_scores)
{
// Resize the output vector
correspondences.resize(source_features->size());
correspondence_scores.resize(source_features->size());
// Use a KdTree to search for the nearest matches in feature space
pcl::KdTreeFLANN<FeatureT> kdtree;
kdtree.setInputCloud(target_features);
// Find the index of the best match for each keypoint, and store it in "correspondences"
const int k = 1;
std::vector<int> k_indices(k);
std::vector<float> k_squared_distances(k);
for (size_t i = 0; i < source_features->size(); ++i) {
kdtree.nearestKSearch (*source_features, i, k, k_indices, k_squared_distances);
correspondences[i] = k_indices[0];
correspondence_scores[i] = k_squared_distances[0];
}
}