-
Notifications
You must be signed in to change notification settings - Fork 20
/
detectors.cc
188 lines (166 loc) · 5.78 KB
/
detectors.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
/*
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.
*/
#include "detectors.h"
#include "harrislike.h"
#include "dog.h"
#ifdef USESUSAN
#include "susan.h"
#endif
#include "cvd_fast.h"
//#include "faster_block.h"
#include "faster_detector.h"
#include <memory>
#include <cstdlib>
#include <gvars3/instances.h>
using namespace std;
using namespace CVD;
using namespace GVars3;
/** This takes a detector which requires a threshold and uses binary search to get as
close as possible to the requested number of corners.
@param i The image in which to detect corners.
@param c The detected corners to be returned.
@param N The target number of corners.
@param detector The corner detector.
@ingroup gDetect
*/
int binary_search_threshold(const Image<CVD::byte>& i, vector<ImageRef>& c, unsigned int N, const DetectT& detector)
{
//Corners for high, low and midpoint thresholds.
vector<ImageRef> ch, cl, cm;
//The high and low thresholds.
unsigned int t_high = 256;
unsigned int t_low = 0;
detector(i, ch, t_high);
detector(i, cl, t_low);
while(t_high > t_low + 1)
{
cm.clear();
unsigned int t = (t_high + t_low ) / 2;
detector(i, cm, t);
if(cm.size() == N)
{
c = cm;
return t;
}
else if(cm.size() < N) //If we detected too few points, then the t is too high
{
t_high = t;
ch = cm;
}
else //We detected too many points to t is too low.
{
t_low = t;
cl = cm;
}
}
//Pick the closest
//If there is ambiguity, go with the lower threshold (more corners).
//The only reason for this is that the evaluation code in the FAST-ER
//system uses this rule.
if( N - ch.size() >= cl.size() - N)
{
c = cl;
return t_low;
}
else
{
c = ch;
return t_high;
}
}
///This class wraps a ::DetectT class with ::binary_search_threshold and presents
///is as a DetectN class.
///@ingroup gDetect
struct SearchThreshold:public DetectN
{
///@param d Detector to wrap. This will be managed by SearchThreshold
SearchThreshold(DetectT* d)
:detector(d)
{
}
///Detect corners
///@param im Image in which to detect corners
///@param corners Detected corners are inserted in to this array
///@param N number of corners to detect
virtual void operator()(const Image<CVD::byte>& im, vector<ImageRef>& corners, unsigned int N)const
{
int t = binary_search_threshold(im, corners, N, *detector);
}
private:
///Detector to wrap
unique_ptr<DetectT> detector;
};
///@ingroup gDetect
///Detector which randomly scatters corners around an image.
struct Random:public DetectN
{
///Detect corners by scattering points around at random
///@param im Image in which to detect corners
///@param corners Detected corners are inserted in to this array
///@param N number of corners to detect
virtual void operator()(const Image<CVD::byte>& im, vector<ImageRef>& corners, unsigned int N)const
{
for(unsigned int i=0; i < N; i++)
corners.push_back(ImageRef(rand() % im.size().x, rand() % im.size().y));
}
};
///Very simple factory function for getting detector objects.
///Paramaters (including the detector type) are drawn from
///the GVars database. The parameter "detector" determines the
///detector type. Valid options are:
/// - \link ::Random random \endlink Randomly scatter corners around the image.
/// - \link ::dog dog \endlink Difference of Gaussians detector
/// - \link ::harrisdog harrisdog \endlink Harris-Laplace (actually implemented as Harris-DoG) detector
/// - \link ::HarrisDetect harris\endlink Harris detector with Gaussian blur
/// - \link ::ShiTomasiDetect shitomasi\endlink Shi-Tomasi detector
/// - \link ::SUSAN susan\endlink Reference implementation of the SUSAN detector
/// - \link ::fast_9 fast9\endlink libCVD's builtin FAST-9 detector
/// - \link ::fast_9_old fast9old\endlink libCVD's builtin FAST-9 detector with the old
/// scoring algorithm, as seen in [Rosten, Drummond 2006].
/// - \link ::fast_12 fast12\endlink libCVD's builtin FAST-12 detector
/// - \link ::faster_learn faster2\endlink A FAST-ER detector loaded from a file containing the tree
///@ingroup gDetect
unique_ptr<DetectN> get_detector()
{
string d = GV3::get<string>("detector", "fast9", 1);
if(d == "random")
return unique_ptr<DetectN>(new Random);
else if(d == "dog")
return unique_ptr<DetectN>(new dog);
else if(d == "harrisdog")
return unique_ptr<DetectN>(new harrisdog);
else if(d == "shitomasi")
return unique_ptr<DetectN>(new ShiTomasiDetect);
else if(d == "harris")
return unique_ptr<DetectN>(new HarrisDetect);
#ifdef USESUSAN
else if(d == "susan")
return unique_ptr<DetectN>(new SearchThreshold(new SUSAN));
#endif
else if(d == "fast9")
return unique_ptr<DetectN>(new SearchThreshold(new fast_9));
else if(d == "fast9old")
return unique_ptr<DetectN>(new SearchThreshold(new fast_9_old));
else if(d == "fast12")
return unique_ptr<DetectN>(new SearchThreshold(new fast_12));
else if(d == "faster2")
return unique_ptr<DetectN>(new SearchThreshold(new faster_learn(GV3::get<string>("faster2"))));
else
{
cerr << "Unknown detector: " << d << endl;
exit(1);
}
}