Go to the documentation of this file.00001 #include <assert.h>
00002
00003 #include "grid_filter.hpp"
00004
00005 #include <cmath>
00006 #include <algorithm>
00007 #include <iterator>
00008 #include <iostream>
00009
00010 namespace fovis
00011 {
00012
00013 static bool
00014 keypoint_score_comparator(const KeyPoint& a,
00015 const KeyPoint& b) {
00016 return (a.score > b.score);
00017 }
00018
00019 void
00020 GridKeyPointFilter::filter(std::vector<KeyPoint>* keypoints_)
00021 {
00022
00023 for(int i=0, n=_buckets.size(); i<n; i++)
00024 _buckets[i].clear();
00025
00026 for (size_t i=0, num_keypoints=keypoints_->size(); i < num_keypoints; ++i) {
00027 KeyPoint& kp((*keypoints_)[i]);
00028 int grid_iy = kp.v / _bucket_height;
00029 int grid_ix = kp.u / _bucket_width;
00030 assert(grid_ix >= 0 && grid_ix < _grid_cols);
00031 assert(grid_iy >= 0 && grid_iy < _grid_rows);
00032 int bucket_index = grid_iy * _grid_cols + grid_ix;
00033 _buckets[bucket_index].push_back(kp);
00034 }
00035
00036 keypoints_->clear();
00037
00038 for (size_t i=0, num_buckets=_buckets.size(); i < num_buckets; ++i) {
00039 std::vector<KeyPoint>& bucket_kps(_buckets[i]);
00040 std::vector<KeyPoint>::iterator new_end_itr;
00041 if (bucket_kps.size() > static_cast<size_t>(_max_keypoints_per_bucket)) {
00042 new_end_itr = bucket_kps.begin() + _max_keypoints_per_bucket;
00043 std::nth_element(bucket_kps.begin(), new_end_itr, bucket_kps.end(), keypoint_score_comparator);
00044 } else {
00045 new_end_itr = bucket_kps.end();
00046 }
00047 std::copy(bucket_kps.begin(), new_end_itr, std::back_inserter(*keypoints_));
00048 }
00049 }
00050
00051 }