| 1 | #include "cvd_src/fast/prototypes.h" |
| 2 | #include <cvd/fast_corner.h> |
| 3 | #include <cvd/nonmax_suppression.h> |
| 4 | |
| 5 | using namespace CVD; |
| 6 | using namespace std; |
| 7 | |
| 8 | namespace CVD |
| 9 | { |
| 10 | |
| 11 | const ImageRef fast_pixel_ring[16] = { |
| 12 | ImageRef(0, 3), |
| 13 | ImageRef(1, 3), |
| 14 | ImageRef(2, 2), |
| 15 | ImageRef(3, 1), |
| 16 | ImageRef(3, 0), |
| 17 | ImageRef(3, -1), |
| 18 | ImageRef(2, -2), |
| 19 | ImageRef(1, -3), |
| 20 | ImageRef(0, -3), |
| 21 | ImageRef(-1, -3), |
| 22 | ImageRef(-2, -2), |
| 23 | ImageRef(-3, -1), |
| 24 | ImageRef(-3, 0), |
| 25 | ImageRef(-3, 1), |
| 26 | ImageRef(-2, 2), |
| 27 | ImageRef(-1, 3), |
| 28 | }; |
| 29 | |
| 30 | struct CornerPositive |
| 31 | { |
| 32 | inline static int sub(int a, int b) { return a - b; } |
| 33 | }; |
| 34 | struct CornerNegative |
| 35 | { |
| 36 | inline static int sub(int a, int b) { return b - a; } |
| 37 | }; |
| 38 | |
| 39 | int old_style_corner_score(const BasicImage<byte>& im, ImageRef c, const int* pointer_dir, int barrier) |
| 40 | { |
| 41 | //The score for a positive feature is sum of the difference between the pixels |
| 42 | //and the barrier if the difference is positive. Negative is similar. |
| 43 | //The score is the max of those two. |
| 44 | // |
| 45 | // B = {x | x = points on the Bresenham circle around c} |
| 46 | // Sp = { I(x) - t | x E B , I(x) - t > 0 } |
| 47 | // Sn = { t - I(x) | x E B, t - I(x) > 0} |
| 48 | // |
| 49 | // Score = max sum(Sp), sum(Sn) |
| 50 | |
| 51 | const byte* imp = &im[c]; |
| 52 | |
| 53 | int cb = *imp + barrier; |
| 54 | int c_b = *imp - barrier; |
| 55 | int sp = 0, sn = 0; |
| 56 | |
| 57 | for(int i = 0; i < 16; i++) |
| 58 | { |
| 59 | int p = imp[pointer_dir[i]]; |
| 60 | |
| 61 | if(p > cb) |
| 62 | sp += p - cb; |
| 63 | else if(p < c_b) |
| 64 | sn += c_b - p; |
| 65 | } |
| 66 | |
| 67 | if(sp > sn) |
| 68 | return sp; |
| 69 | else |
| 70 | return sn; |
| 71 | } |
| 72 | |
| 73 | void compute_fast_score_old(const BasicImage<byte>& im, const vector<ImageRef>& corners, int barrier, vector<int>& scores) |
| 74 | { |
| 75 | int pointer_dir[16]; |
| 76 | for(int i = 0; i < 16; i++) |
| 77 | pointer_dir[i] = fast_pixel_ring[i].x + fast_pixel_ring[i].y * im.size().x; |
| 78 | |
| 79 | scores.resize(corners.size()); |
| 80 | |
| 81 | for(unsigned int i = 0; i < corners.size(); i++) |
| 82 | scores[i] = old_style_corner_score(im, corners[i], pointer_dir, barrier); |
| 83 | } |
| 84 | |
| 85 | void fast_nonmax(const BasicImage<byte>& im, const vector<ImageRef>& corners, int barrier, vector<ImageRef>& max_corners) |
| 86 | { |
| 87 | vector<int> scores; |
| 88 | compute_fast_score_old(im, corners, barrier, scores); |
| 89 | nonmax_suppression(corners, scores, max_corners); |
| 90 | } |
| 91 | |
| 92 | void fast_nonmax_with_scores(const BasicImage<byte>& im, const vector<ImageRef>& corners, int barrier, vector<pair<ImageRef, int>>& max_corners) |
| 93 | { |
| 94 | vector<int> scores; |
| 95 | compute_fast_score_old(im, corners, barrier, scores); |
| 96 | nonmax_suppression_with_scores(corners, scores, max_corners); |
| 97 | } |
| 98 | |
| 99 | } |