38 #ifndef PCL_SIFT_KEYPOINT_IMPL_H_
39 #define PCL_SIFT_KEYPOINT_IMPL_H_
41 #include <pcl/keypoints/sift_keypoint.h>
42 #include <pcl/common/io.h>
43 #include <pcl/filters/voxel_grid.h>
44 #include <pcl/search/kdtree.h>
47 template <
typename Po
intInT,
typename Po
intOutT>
void
50 min_scale_ = min_scale;
51 nr_octaves_ = nr_octaves;
52 nr_scales_per_octave_ = nr_scales_per_octave;
57 template <
typename Po
intInT,
typename Po
intOutT>
void
60 min_contrast_ = min_contrast;
64 template <
typename Po
intInT,
typename Po
intOutT>
bool
69 PCL_ERROR (
"[pcl::%s::initCompute] : Minimum scale (%f) must be strict positive!\n",
70 name_.c_str (), min_scale_);
75 PCL_ERROR (
"[pcl::%s::initCompute] : Number of octaves (%d) must be at least 1!\n",
76 name_.c_str (), nr_octaves_);
79 if (nr_scales_per_octave_ < 1)
81 PCL_ERROR (
"[pcl::%s::initCompute] : Number of scales per octave (%d) must be at least 1!\n",
82 name_.c_str (), nr_scales_per_octave_);
85 if (min_contrast_ < 0)
87 PCL_ERROR (
"[pcl::%s::initCompute] : Minimum contrast (%f) must be non-negative!\n",
88 name_.c_str (), min_contrast_);
98 template <
typename Po
intInT,
typename Po
intOutT>
void
101 if (surface_ && surface_ != input_)
103 PCL_WARN (
"[pcl::%s::detectKeypoints] : ", name_.c_str ());
104 PCL_WARN (
"A search surface has been set by setSearchSurface, but this SIFT keypoint detection algorithm does ");
105 PCL_WARN (
"not support search surfaces other than the input cloud. ");
106 PCL_WARN (
"The cloud provided in setInputCloud is being used instead.\n");
110 scale_idx_ = pcl::getFieldIndex<PointOutT> (
"scale", out_fields_);
120 float scale = min_scale_;
121 for (
int i_octave = 0; i_octave < nr_octaves_; ++i_octave)
124 const float s = 1.0f * scale;
128 voxel_grid.
filter (*temp);
132 constexpr std::size_t min_nr_points = 25;
133 if (cloud->
size () < min_nr_points)
137 tree_->setInputCloud (cloud);
140 detectKeypointsForOctave (*cloud, *tree_, scale, nr_scales_per_octave_, output);
148 output.width = output.size ();
149 output.header = input_->header;
150 output.sensor_origin_ = input_->sensor_origin_;
151 output.sensor_orientation_ = input_->sensor_orientation_;
156 template <
typename Po
intInT,
typename Po
intOutT>
void
158 const PointCloudIn &input,
KdTree &tree,
float base_scale,
int nr_scales_per_octave,
159 PointCloudOut &output)
162 std::vector<float> scales (nr_scales_per_octave + 3);
163 for (
int i_scale = 0; i_scale <= nr_scales_per_octave + 2; ++i_scale)
165 scales[i_scale] = base_scale * powf (2.0f, (1.0f *
static_cast<float> (i_scale) - 1.0f) /
static_cast<float> (nr_scales_per_octave));
167 Eigen::MatrixXf diff_of_gauss;
168 computeScaleSpace (input, tree, scales, diff_of_gauss);
172 std::vector<int> extrema_scales;
173 findScaleSpaceExtrema (input, tree, diff_of_gauss, extrema_indices, extrema_scales);
175 output.reserve (output.size () + extrema_indices.size ());
177 if (scale_idx_ != -1)
180 for (std::size_t i_keypoint = 0; i_keypoint < extrema_indices.size (); ++i_keypoint)
183 const auto &keypoint_index = extrema_indices[i_keypoint];
185 keypoint.x = input[keypoint_index].x;
186 keypoint.y = input[keypoint_index].y;
187 keypoint.z = input[keypoint_index].z;
188 memcpy (
reinterpret_cast<char*
> (&keypoint) + out_fields_[scale_idx_].offset,
189 &scales[extrema_scales[i_keypoint]],
sizeof (
float));
190 output.push_back (keypoint);
196 for (
const auto &keypoint_index : extrema_indices)
199 keypoint.x = input[keypoint_index].x;
200 keypoint.y = input[keypoint_index].y;
201 keypoint.z = input[keypoint_index].z;
203 output.push_back (keypoint);
210 template <
typename Po
intInT,
typename Po
intOutT>
212 const PointCloudIn &input, KdTree &tree,
const std::vector<float> &scales,
213 Eigen::MatrixXf &diff_of_gauss)
215 diff_of_gauss.resize (input.size (), scales.size () - 1);
218 const float max_radius = 3.0f * scales.back ();
220 for (
int i_point = 0; i_point < static_cast<int> (input.size ()); ++i_point)
223 std::vector<float> nn_dist;
224 tree.radiusSearch (i_point, max_radius, nn_indices, nn_dist);
230 float filter_response = 0.0f;
231 for (std::size_t i_scale = 0; i_scale < scales.size (); ++i_scale)
233 float sigma_sqr = powf (scales[i_scale], 2.0f);
235 float numerator = 0.0f;
236 float denominator = 0.0f;
237 for (std::size_t i_neighbor = 0; i_neighbor < nn_indices.size (); ++i_neighbor)
239 const float &value = getFieldValue_ (input[nn_indices[i_neighbor]]);
240 const float &dist_sqr = nn_dist[i_neighbor];
241 if (dist_sqr <= 9*sigma_sqr)
243 float w = std::exp (-0.5f * dist_sqr / sigma_sqr);
244 numerator += value * w;
249 float previous_filter_response = filter_response;
250 filter_response = numerator / denominator;
254 diff_of_gauss (i_point, i_scale - 1) = filter_response - previous_filter_response;
260 template <
typename Po
intInT,
typename Po
intOutT>
void
262 const PointCloudIn &input, KdTree &tree,
const Eigen::MatrixXf &diff_of_gauss,
263 pcl::Indices &extrema_indices, std::vector<int> &extrema_scales)
265 constexpr
int k = 25;
267 std::vector<float> nn_dist (k);
269 const int nr_scales =
static_cast<int> (diff_of_gauss.cols ());
270 std::vector<float> min_val (nr_scales), max_val (nr_scales);
272 for (
int i_point = 0; i_point < static_cast<int> (input.size ()); ++i_point)
275 const std::size_t nr_nn = tree.nearestKSearch (i_point, k, nn_indices, nn_dist);
281 for (
int i_scale = 0; i_scale < nr_scales; ++i_scale)
283 min_val[i_scale] = std::numeric_limits<float>::max ();
284 max_val[i_scale] = -std::numeric_limits<float>::max ();
286 for (std::size_t i_neighbor = 0; i_neighbor < nr_nn; ++i_neighbor)
288 const float &d = diff_of_gauss (nn_indices[i_neighbor], i_scale);
290 min_val[i_scale] = (std::min) (min_val[i_scale], d);
291 max_val[i_scale] = (std::max) (max_val[i_scale], d);
296 for (
int i_scale = 1; i_scale < nr_scales - 1; ++i_scale)
298 const float &val = diff_of_gauss (i_point, i_scale);
301 if (std::abs (val) >= min_contrast_)
304 if ((val == min_val[i_scale]) &&
305 (val < min_val[i_scale - 1]) &&
306 (val < min_val[i_scale + 1]))
308 extrema_indices.push_back (i_point);
309 extrema_scales.push_back (i_scale);
312 else if ((val == max_val[i_scale]) &&
313 (val > max_val[i_scale - 1]) &&
314 (val > max_val[i_scale + 1]))
316 extrema_indices.push_back (i_point);
317 extrema_scales.push_back (i_scale);
324 #define PCL_INSTANTIATE_SIFTKeypoint(T,U) template class PCL_EXPORTS pcl::SIFTKeypoint<T,U>;
void filter(PointCloud &output)
Calls the filtering method and returns the filtered dataset in output.
KdTree represents the base spatial locator class for kd-tree implementations.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
shared_ptr< PointCloud< PointT > > Ptr
SIFTKeypoint detects the Scale Invariant Feature Transform keypoints for a given point cloud dataset ...
bool initCompute() override
void setMinimumContrast(float min_contrast)
Provide a threshold to limit detection of keypoints without sufficient contrast.
void setScales(float min_scale, int nr_octaves, int nr_scales_per_octave)
Specify the range of scales over which to search for keypoints.
void detectKeypoints(PointCloudOut &output) override
Detect the SIFT keypoints for a set of points given in setInputCloud () using the spatial locator in ...
typename Keypoint< PointInT, PointOutT >::PointCloudOut PointCloudOut
VoxelGrid assembles a local 3D grid over a given PointCloud, and downsamples + filters the data.
void setLeafSize(const Eigen::Vector4f &leaf_size)
Set the voxel grid leaf size.
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
IndicesAllocator<> Indices
Type used for indices in PCL.