-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fast
163 lines (141 loc) · 8.22 KB
/
fast
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_VISION_FEATURE_DETECTOR_FAST_HPP
#define GTL_VISION_FEATURE_DETECTOR_FAST_HPP
// Summary: Implemetation of feature detection from the paper "Faster and better: A machine learning approach to corner detection". IEEE transactions on pattern analysis and machine intelligence (2008). [wip]
#include <vision/feature/feature>
namespace gtl {
int fast(
const unsigned char* __restrict const data,
const int width,
const int height,
const int stride,
const int threshold,
const int feature_point_buffer_size,
gtl::feature* __restrict feature_point_buffer
);
int fast(
const unsigned char* __restrict const data,
const int width,
const int height,
const int stride,
const int threshold,
const int feature_point_buffer_size,
gtl::feature* __restrict feature_point_buffer
) {
constexpr static const int patch_size = 7;
constexpr static const int patch_radius = 3;
constexpr static const int pattern_size = 16;
constexpr static const int pattern_size_half = pattern_size / 2;
static_cast<void>(patch_size);
// A FAST patch (radius 3):
//
// * * 9 8 7 * *
// * A X X X 6 *
// B X X X X X 5
// C X X O X X 4
// D X X X X X 3
// * E X X X 2 *
// * * F 0 1 * *
// The pattern loops from 0 to F and back to 7 so that every pixel can be compared against one 8 ahead of it.
const int pattern_offset[24] = {
patch_radius * stride + 0, patch_radius * stride + 1, +2 * stride + 2, patch_radius + stride + 0,
patch_radius, patch_radius - stride + 0, -2 * stride + 2, -patch_radius * stride + 1,
-patch_radius * stride + 0, -patch_radius * stride - 1, -2 * stride - 2, -patch_radius - stride + 0,
-patch_radius, -patch_radius + stride + 0, +2 * stride - 2, patch_radius * stride - 1,
patch_radius * stride + 0, patch_radius * stride + 1, +2 * stride + 2, patch_radius + stride + 0,
patch_radius, patch_radius - stride + 0, -2 * stride + 2, -patch_radius * stride + 1
};
// A threshold lookup table for pixel intensities.
unsigned char threshold_map[255 + 1 + 255];
for (int value = -255; value <= 255; ++value) {
threshold_map[value + 255] = (value < -threshold) + 2 * (value > threshold);
}
// Count the number of detected features.
int feature_count = 0;
// Process the entire data buffer, keeping half the patch size from every edge.
for (int y = patch_radius; y < height - patch_radius; ++y) {
const unsigned char* data_pointer = data + y * stride + patch_radius;
for (int x = patch_radius; x < width - patch_radius; ++x, ++data_pointer) {
// Offset into the threshold_map using the value of the centre pixel.
const unsigned char* threshold_pointer = &threshold_map[255] - data_pointer[0];
// Comapare pattern pixels against threshold using the offset threshold map, to determine if this is a potential corner.
unsigned char threshold_accumulator = 1 | 2;
threshold_accumulator &= threshold_pointer[data_pointer[pattern_offset[0]]] | threshold_pointer[data_pointer[pattern_offset[8]]];
threshold_accumulator &= threshold_pointer[data_pointer[pattern_offset[1]]] | threshold_pointer[data_pointer[pattern_offset[9]]];
threshold_accumulator &= threshold_pointer[data_pointer[pattern_offset[2]]] | threshold_pointer[data_pointer[pattern_offset[10]]];
threshold_accumulator &= threshold_pointer[data_pointer[pattern_offset[3]]] | threshold_pointer[data_pointer[pattern_offset[11]]];
threshold_accumulator &= threshold_pointer[data_pointer[pattern_offset[4]]] | threshold_pointer[data_pointer[pattern_offset[12]]];
threshold_accumulator &= threshold_pointer[data_pointer[pattern_offset[5]]] | threshold_pointer[data_pointer[pattern_offset[13]]];
threshold_accumulator &= threshold_pointer[data_pointer[pattern_offset[6]]] | threshold_pointer[data_pointer[pattern_offset[14]]];
threshold_accumulator &= threshold_pointer[data_pointer[pattern_offset[7]]] | threshold_pointer[data_pointer[pattern_offset[15]]];
// If this is a potential corner the accumulator will not be zero.
// If the pixels have been less than the threshold, it will contain a 1.
if (threshold_accumulator & 1) {
unsigned int valid_pixels = 0;
const int pixel_threshold = static_cast<int>(data_pointer[0]) - threshold;
// Check each pattern pixel against the thresholded centre pixel, need a continuous set of passes greater than half the pattern size.
for (unsigned int index = 0; index < pattern_size + pattern_size_half; ++index) {
const int pixel = data_pointer[pattern_offset[index]];
if (pixel < pixel_threshold) {
++valid_pixels;
if (valid_pixels > pattern_size_half) {
feature_point_buffer[feature_count++] = { x, y, 0, 0 };
if (feature_count == feature_point_buffer_size) {
return feature_count;
}
break;
}
}
else {
// No longer possible to have a large enough continuous set of passes.
if (index > pattern_size) {
break;
}
valid_pixels = 0;
}
}
}
// If the pixels have been greater than the threshold, it will contain a 2.
if (threshold_accumulator & 2) {
unsigned int valid_pixels = 0;
const int pixel_threshold = static_cast<int>(data_pointer[0]) + threshold;
// Check each pattern pixel against the thresholded centre pixel, need a continuous set of passes greater than half the pattern size.
for (unsigned int index = 0; index < pattern_size + pattern_size_half; ++index) {
const int pixel = data_pointer[pattern_offset[index]];
if (pixel > pixel_threshold) {
++valid_pixels;
if (valid_pixels > pattern_size_half) {
feature_point_buffer[feature_count++] = { x, y, 0, 0 };
if (feature_count == feature_point_buffer_size) {
return feature_count;
}
break;
}
}
else {
// No longer possible to have a large enough continuous set of passes.
if (index > pattern_size) {
break;
}
valid_pixels = 0;
}
}
}
}
}
return feature_count;
}
}
#endif // GTL_VISION_FEATURE_DETECTOR_FAST_HPP