-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathextract_features.cc
194 lines (160 loc) · 5.78 KB
/
extract_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
/*
This file is part of the FAST-ER machine learning system.
Copyright (C) 2008 Edward Rosten and Los Alamos National Laboratory
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; either version 2 of the License, or
(at your option) any later version.
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
\file extract_features.cc Main file for the extract_features executable.
\section wpUsage Usage
<code> extract_features [--VAR VAL] [--exec FILE] IMAGE1 [IMAGE2 ...]</code>
\section Description
This program loads a learned FAST-ER tree and 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, and the default
parameters are contained in \p extract_features.cfg :
\include extract_features.cfg
The images from which
features should be extracted are specified on the commandline.
*/
#include <gvars3/instances.h>
#include <cvd/image_io.h>
#include <stdint.h>
#include <map>
#include <iterator>
#include <string>
#include "offsets.h"
#include "faster_tree.h"
using namespace std;
using namespace CVD;
using namespace GVars3;
static const char BrighterFlag = 'b'; ///< Character code for pixels significantly brighter than the centre
static const char DarkerFlag = 'd';///< Character code for pixels significantly darker than the centre
static const char SimilarFlag = 's';///< Character code for pixels similar to the centre
///Extracts a feature from an image.
///@param s String to extract feature in to
///@param im Image to extract feature from
///@param pos Location to extract feature from
///@param barrier Threshold used to compute feature
///@param o Index in to offsets (i.e.\ feature orientation) to use
///@param invert_sense Whether or not to invert the extracted feature
void extract_feature(string& s, const BasicImage<CVD::byte>& im, const ImageRef& pos, int barrier, int o, bool invert_sense)
{
int cb = im[pos] + barrier;
int c_b = im[pos] - barrier;
for(int i=0; i < num_offsets; i++)
{
int pix = im[pos + offsets[o][i]];
if(pix > cb)
if(invert_sense == false)
s[i] = BrighterFlag;
else
s[i] = DarkerFlag;
else if(pix < c_b)
if(invert_sense == false)
s[i] = DarkerFlag;
else
s[i] = BrighterFlag;
else
s[i] = SimilarFlag;
}
}
///Driving program
///@param argc Number of commandline arguments
///@param argv List of commandline arguments. Contains GVars3 arguments, and images to process.
int main(int argc, char** argv)
{
//The usual initialization.
GUI.LoadFile("extract_features.cfg");
int lastarg = GUI.parseArguments(argc, argv);
create_offsets();
//Store corners and noncorners by the string representing the feature.
map<string, uint64_t> corners, non_corners;
//Scratch string of the correct length for extracting features in to.
string scratch(num_offsets, '.');
//Don't bother examining points outside this border.
int border = max(max(offsets_bbox.first.x, offsets_bbox.first.y), max(offsets_bbox.second.x, offsets_bbox.second.y));
int threshold = GV3::get<int>("threshold", 30);
string fname=GV3::get<string>("detector", "best_faster.tree");
//Load a detector from a tree file
tree_element* faster_detector;
ifstream i;
i.open(fname.c_str());
if(!i.good())
{
cerr << "Error: " << fname << ": " << strerror(errno) << endl;
exit(1);
}
try{
faster_detector = load_a_tree(i);
}
catch(ParseError p)
{
cerr << "Parse error in " << fname << endl;
exit(1);
}
//Iterate over all images, extracting features
for(int i=lastarg; i < argc; i++)
{
try{
Image<CVD::byte> im = img_load(argv[i]);
for(int r=border; r < im.size().y - border; r++)
for(int c=border; c < im.size().x - border; c++)
{
ImageRef pos(c,r);
//Test for cornerness
bool is_corner = faster_detector->detect_corner(im, pos, threshold);
//Iterate over all feature orientations and inversions,
//extracting the reatures, and inserting them in to the
//correct bin
for(unsigned int k=0; k < offsets.size(); k++)
for(int l=0; l < 2; l++)
{
extract_feature(scratch, im, pos, threshold, k, l);
if(is_corner)
{
corners[scratch]++;
if(non_corners.count(scratch))
{
cerr << "Fatal error! extracted corner has an identical non-corner!\n";
cerr << "Are your offsets correct?\n";
exit(1);
}
}
else
{
non_corners[scratch]++;
if(corners.count(scratch))
{
cerr << "Fatal error! extracted non-corner has an identical corner!\n";
cerr << "Are your offsets correct?\n";
exit(1);
}
}
}
}
cerr << "Processed " << argv[i] << endl;
}
catch(const Exceptions::All& e)
{
cerr << "Failed to load " << argv[i] << ": " << e.what() << endl;
}
}
cout << num_offsets << endl;
copy(offsets[0].begin(), offsets[0].end(), ostream_iterator<ImageRef>(cout, " "));
cout << endl;
for(map<string, uint64_t>::iterator i=corners.begin(); i != corners.end(); i++)
cout << i->first << " " << i->second << " 1" << endl;
for(map<string, uint64_t>::iterator i=non_corners.begin(); i != non_corners.end(); i++)
cout << i->first << " " << i->second << " 0" << endl;
}