-
Notifications
You must be signed in to change notification settings - Fork 20
/
fast_N_features.cc
208 lines (153 loc) · 4.66 KB
/
fast_N_features.cc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
\file fast_N_features.cc Main file for the fast_N_features executable.
\section wpUsage Usage
<code> fast_N_features [--R r] [--T threshold] IMAGE1 [IMAGE2 ...]</code>
\section Description
Extracts features so that an
accelerated tree can be learned. The output is is suitable for consumption
by \link learn_fast_tree.cc learn_fast_tree\endlink.
The program accpets standard GVars3 commandline arguments.
The images from which
features should be extracted are specified on the commandline.
This executable extracts pixel rings of radius r, and guarantees that all
feautres are persent at least once. It will go very slow if r is much larger than 3.
*/
#include <iostream>
#include <gvars3/instances.h>
#include <array>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <cvd/image.h>
#include <cvd/image_io.h>
#include <cvd/morphology.h>
using namespace std;
using CVD::Image;
using CVD::SubImage;
using CVD::ImageRef;
using namespace GVars3;
double angle(const ImageRef i){
return std::fmod(std::atan2(i.x, i.y) + M_PI*2*10, M_PI*2);
}
vector<ImageRef> circle(double r){
//Generate a 1 pixel wide, 8-connected circle.
int r_ = int(r+2);
ImageRef size(2*r_+1, 2*r_+1);
ImageRef centre = size/2;
Image<bool> im(size);
ImageRef p{0,0};
do
if((p-centre).mag_squared() <= r*r)
im[p]=1;
while(p.next(im.size()));
Image<bool> circle = CVD::morphology(im, {ImageRef{-1,0}, ImageRef{1,0}, ImageRef{0,-1}, ImageRef{0,1}}, CVD::Morphology::BinaryErode{});
vector<ImageRef> pixels;
do{
im[p]=im[p] && !circle[p];
if(im[p])
pixels.push_back(p-centre);
}
while(p.next(im.size()));
sort(pixels.begin(), pixels.end(), [](const auto& a, const auto& b){
return angle(a) < angle(b);
});
Image<string> viz(im.size(), "•");
for(const auto& p:pixels){
viz[p+centre] = 'A'+(&p - pixels.data());
}
for(int y=0; y < im.size().y; y++){
for(int x=0; x < im.size().x; x++)
clog << viz[y][x];
clog << "\n";
}
return pixels;
}
void extract(const SubImage<uint8_t>& im, int threshold, const vector<ImageRef>& ring, std::unordered_map<string, uint64_t>& counts){
int lo=100000000, hi=-100000000;
for(const auto& p: ring){
lo=std::min(lo, p.x);
lo=std::min(lo, p.y);
hi=std::max(hi, p.x);
hi=std::max(hi, p.y);
}
std::string thresholded;
thresholded.resize(ring.size());
for(int y=-lo; y < im.size().y - hi; y++)
for(int x=-lo; x < im.size().x - hi; x++){
for(size_t i=0; i < ring.size(); i++){
ImageRef c = ImageRef{x,y};
if(im[ring[i] + c] > im[c] + threshold)
thresholded[i]='b';
else if(im[ring[i] + c] < im[c] - threshold)
thresholded[i]='d';
else
thresholded[i]='s';
}
counts[thresholded]++;
}
}
int main(int argc, char ** argv){
int lastarg = GUI.parseArguments(argc, argv);
vector<ImageRef> ring = circle(GV3::get<double>("R", 3.3));
int N = ring.size();
//Twice the length necessary and contains two copies
//this lest a simple string search wrap across the end
std::string pixels_2(2*N, 'b');
std::string pattern_b(N/2+1, 'b');
std::string pattern_d(N/2+1, 'd');
clog << "FAST " << pattern_b.size() << "/" << N << endl;
unordered_map<string, uint64_t> features; //list of all features ever
unordered_map<string, bool> corners; //classification of all features
clog << "Generating...\n";
// Pre-generate one of each kind, and classify them
for(;;){
bool corner = 0;
if(pixels_2.find(pattern_b) != string::npos)
corner = 1;
else if(pixels_2.find(pattern_d) != string::npos)
corner = 1;
//const auto v = string_view(pixels_2).substr(N)
corners[pixels_2.substr(N)] = corner;
features[pixels_2.substr(N)]++;
for(int i=N-1; i >= 0; i--){
switch(pixels_2[i]){
case 'b':
pixels_2[i] = pixels_2[i+N] = 's';
goto loop;
case 's':
pixels_2[i] = pixels_2[i+N] = 'd';
goto loop;
case 'd':
pixels_2[i] = pixels_2[i+N] = 'b';
break;
}
}
goto overflow;
loop:;
if(corners.size() % 100000 == 0)
clog << " " << corners.size() * 100 / std::pow(3, N) << "%\r" << flush;
}
overflow:;
clog << endl;
for(int n=lastarg; n < argc; n++){
clog << " " << n*100/argc << "\r" << flush;
Image<uint8_t> im = CVD::img_load(argv[n]);
extract(im, GV3::get<int>("T", 30), ring, features);
}
clog << endl;
uint64_t total=0;
for(const auto& f:features)
total += f.second;
clog << "Features extracted = " << total << endl;
cout << N << endl;
for(const auto& p:ring)
cout << p << " ";
cout << endl;
vector<string> all;
for(const auto& c:corners)
all.emplace_back(c.first);
sort(all.begin(), all.end());
for(const auto& str: all){
cout << str << " " << corners[str] << " " << features[str] << endl;
}
}