| 1 | #include <cvd/image_ref.h> |
| 2 | #include <cvd/nonmax_suppression.h> |
| 3 | #include <vector> |
| 4 | |
| 5 | using namespace std; |
| 6 | |
| 7 | namespace CVD |
| 8 | { |
| 9 | |
| 10 | //This function has been moved from fast_corner.cxx. Look there |
| 11 | //for the old ChangeLog. |
| 12 | // fast_nonmax_t is templated so you can have either of: |
| 13 | // 1: A vector of ImageRefs of the nonmax corners |
| 14 | // 2: A vector of <ImageRef, int> pairs of the corners and their scores. |
| 15 | // And |
| 16 | // 1: Non-strict (neighbours of the same score allowed) |
| 17 | // 2: Strict (must be larger than the neighbours) |
| 18 | // It's internal, the user-visible functions instantiate it below.. |
| 19 | template <class Score, class ReturnType, class Collector, class Test> |
| 20 | inline void nonmax_suppression_t(const vector<ImageRef>& corners, const vector<Score>& scores, vector<ReturnType>& nonmax_corners) |
| 21 | { |
| 22 | nonmax_corners.clear(); |
| 23 | nonmax_corners.reserve(corners.size()); |
| 24 | |
| 25 | if(corners.size() < 1) |
| 26 | return; |
| 27 | |
| 28 | // Find where each row begins |
| 29 | // (the corners are output in raster scan order). A beginning of -1 signifies |
| 30 | // that there are no corners on that row. |
| 31 | int last_row = corners.back().y; |
| 32 | vector<int> row_start(last_row + 1, -1); |
| 33 | |
| 34 | int prev_row = -1; |
| 35 | for(unsigned int i = 0; i < corners.size(); i++) |
| 36 | if(corners[i].y != prev_row) |
| 37 | { |
| 38 | row_start[corners[i].y] = i; |
| 39 | prev_row = corners[i].y; |
| 40 | } |
| 41 | |
| 42 | //Point above points (roughly) to the pixel above the one of interest, if there |
| 43 | //is a feature there. |
| 44 | int point_above = 0; |
| 45 | int point_below = 0; |
| 46 | |
| 47 | const int sz = (int)corners.size(); |
| 48 | |
| 49 | for(int i = 0; i < sz; i++) |
| 50 | { |
| 51 | Score score = scores[i]; |
| 52 | ImageRef pos = corners[i]; |
| 53 | |
| 54 | //Check left |
| 55 | if(i > 0) |
| 56 | if(corners[i - 1] == pos - ImageRef(1, 0) && Test::Compare(scores[i - 1], score)) |
| 57 | continue; |
| 58 | |
| 59 | //Check right |
| 60 | if(i < (sz - 1)) |
| 61 | if(corners[i + 1] == pos + ImageRef(1, 0) && Test::Compare(scores[i + 1], score)) |
| 62 | continue; |
| 63 | |
| 64 | //Check above (if there is a valid row above) |
| 65 | if(pos.y != 0 && row_start[pos.y - 1] != -1) |
| 66 | { |
| 67 | //Make sure that current point_above is one |
| 68 | //row above. |
| 69 | if(corners[point_above].y < pos.y - 1) |
| 70 | point_above = row_start[pos.y - 1]; |
| 71 | |
| 72 | //Make point_above point to the first of the pixels above the current point, |
| 73 | //if it exists. |
| 74 | for(; corners[point_above].y < pos.y && corners[point_above].x < pos.x - 1; point_above++) |
| 75 | { } |
| 76 | |
| 77 | for(int i = point_above; corners[i].y < pos.y && corners[i].x <= pos.x + 1; i++) |
| 78 | { |
| 79 | int x = corners[i].x; |
| 80 | if((x == pos.x - 1 || x == pos.x || x == pos.x + 1) && Test::Compare(scores[i], score)) |
| 81 | goto cont; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | //Check below (if there is anything below) |
| 86 | if(pos.y != last_row && row_start[pos.y + 1] != -1 && point_below < sz) //Nothing below |
| 87 | { |
| 88 | if(corners[point_below].y < pos.y + 1) |
| 89 | point_below = row_start[pos.y + 1]; |
| 90 | |
| 91 | // Make point below point to one of the pixels belowthe current point, if it |
| 92 | // exists. |
| 93 | for(; point_below < sz && corners[point_below].y == pos.y + 1 && corners[point_below].x < pos.x - 1; point_below++) |
| 94 | { } |
| 95 | |
| 96 | for(int i = point_below; i < sz && corners[i].y == pos.y + 1 && corners[i].x <= pos.x + 1; i++) |
| 97 | { |
| 98 | int x = corners[i].x; |
| 99 | if((x == pos.x - 1 || x == pos.x || x == pos.x + 1) && Test::Compare(scores[i], score)) |
| 100 | goto cont; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | nonmax_corners.push_back(Collector::collect(corners[i], scores[i])); |
| 105 | |
| 106 | cont:; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | struct Greater |
| 111 | { |
| 112 | static bool Compare(int a, int b) |
| 113 | { |
| 114 | return a > b; |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | struct GreaterEqual |
| 119 | { |
| 120 | static bool Compare(int a, int b) |
| 121 | { |
| 122 | return a >= b; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | // The two collectors which either return just the ImageRef or the <ImageRef,int> pair |
| 127 | struct collect_pos |
| 128 | { |
| 129 | static inline ImageRef collect(const ImageRef& pos, int) { return pos; } |
| 130 | }; |
| 131 | |
| 132 | struct collect_score |
| 133 | { |
| 134 | static inline pair<ImageRef, int> collect(const ImageRef& pos, int score) { return make_pair(pos, score); } |
| 135 | }; |
| 136 | |
| 137 | // The callable functions |
| 138 | void nonmax_suppression_strict(const vector<ImageRef>& corners, const vector<int>& scores, vector<ImageRef>& nonmax_corners) |
| 139 | { |
| 140 | nonmax_suppression_t<int, ImageRef, collect_pos, GreaterEqual>(corners, scores, nonmax_corners); |
| 141 | } |
| 142 | |
| 143 | void nonmax_suppression(const vector<ImageRef>& corners, const vector<int>& scores, vector<ImageRef>& nonmax_corners) |
| 144 | { |
| 145 | nonmax_suppression_t<int, ImageRef, collect_pos, Greater>(corners, scores, nonmax_corners); |
| 146 | } |
| 147 | |
| 148 | void nonmax_suppression_with_scores(const vector<ImageRef>& corners, const vector<int>& scores, vector<pair<ImageRef, int>>& nonmax_corners) |
| 149 | { |
| 150 | nonmax_suppression_t<int, pair<ImageRef, int>, collect_score, Greater>(corners, scores, nonmax_corners); |
| 151 | } |
| 152 | |
| 153 | } |