-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_repeatability.cc
246 lines (192 loc) · 7.45 KB
/
test_repeatability.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
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 test_repeatability.cc Main file for the test_repeatability executable.
\section wpUsage Usage
<code> test_repeatability [--var VAL] [--exec FILE] </code>
\section Description
This program \link gDataset loads a dataset\endlink and then
computes repeatability of the specified detector on the dataset. This
program accepts standard GVars3 commandline arguments and loads
<code>learn_detector.cfg</code> as a the default configuration:
\include test_repeatability.cfg
The available detectors are selected using the <code>detector</code> variable.
Options are given in ::get_detector.
*/
#include <iostream>
#include <sstream>
#include <cfloat>
#include <map>
#include <utility>
#include <random>
#include <cvd/image_io.h>
#include <cvd/image_interpolate.h>
#include "varprintf/varprintf.h"
#include "gvars_vector.h"
#include "load_data.h"
#include "detectors.h"
#include "utility.h"
using namespace std;
using namespace CVD;
using namespace varPrintf;
using namespace GVars3;
using namespace TooN;
double rand_g(){
static std::mt19937 eng;
static std::normal_distribution<> nd;
return nd(eng);
};
///Computes repeatability the slow way to avoid rounding errors, by comparing the warped
///corner position to every detected corner. A warp to x=-1, y=? is considered to be outside
///the image, so it is not counted.
///
/// @param warps Every warping where warps[i][j] specifies warp from image i to image j.
/// @param corners Detected corners
/// @param r A corner must be as close as this to be considered repeated
/// @return The repeatability. No corners means zero repeatability.
/// @ingroup gRepeatability
double compute_repeatability_exact(const vector<vector<Image<array<float,2> > > >& warps, const vector<vector<ImageRef> >& corners, double r)
{
unsigned int n = corners.size();
int repeatable_corners = 0;
int repeated_corners = 0;
r *= r;
for(unsigned int i=0; i < n; i++)
for(unsigned int j=0; j < n; j++)
{
if(i==j)
continue;
for(unsigned int k=0; k < corners[i].size(); k++)
{
const array<float, 2>& p = warps[i][j][corners[i][k]];
if(p[0] != -1) //pixel does not warp to inside image j
{
repeatable_corners++;
for(unsigned int l=0; l < corners[j].size(); l++)
{
Vector<2> d = Vec(p) - vec(corners[j][l]);
if(d*d < r)
{
repeated_corners++;
break;
}
}
}
}
}
return 1.0 * (repeated_corners) / (repeatable_corners + DBL_EPSILON);
}
///This wrapper function computed the repeatability for a given detector and a given
///container of corner densities. The result is printed to stdout.
///
/// @param images Images to test repeatability on
/// @param warps Every warping where warps[i][j] specifies warp from image i to image j.
/// @param detector Pointer to the corner detection function.
/// @param cpf The number of corners per frame to be tested.
/// @param fuzz A corner must be as close as this to be considered repeated
/// @ingroup gRepeatability
void compute_repeatability_all(const vector<Image<CVD::byte> >& images, const vector<vector<Image<array<float, 2> > > >& warps, const DetectN& detector, const vector<int>& cpf, double fuzz)
{
for(unsigned int i=0; i < cpf.size(); i++)
{
//Detect corners in each if the frames
vector<vector<ImageRef> > corners;
double num_corners = 0;
for(unsigned int j=0; j < images.size(); j++)
{
vector<ImageRef> c;
detector(images[j], c, cpf[i]);
corners.push_back(c);
num_corners += c.size();
}
//Compute and print the repeatability.
cout <<num_corners / images.size() << " " << compute_repeatability_exact(warps, corners, fuzz) << endl;
}
}
///This wrapper function computed the repeatability for a given detector and a given
///container of corner densities for variable levels of noise, from 0 to n in steps of 1
///The result is printed to stdout.
///
/// @param images Images to test repeatability on
/// @param warps Every warping where warps[i][j] specifies warp from image i to image j.
/// @param detector Pointer to the corner detection function.
/// @param cpf The number of corners per frame to be tested.
/// @param n The initial noise level
/// @param fuzz A corner must be as close as this to be considered repeated
/// @ingroup gRepeatability
void compute_repeatability_noise(const vector<Image<CVD::byte> >& images, const vector<vector<Image<array<float, 2> > > >& warps, const DetectN& detector, int cpf, float n, double fuzz)
{
for(float s=0; s <= n; s++)
{
//Detect corners in each if the frames
vector<vector<ImageRef> > corners;
double num_corners = 0;
for(unsigned int j=0; j < images.size(); j++)
{
Image<CVD::byte> ni = images[j];
//Add noise to the image
for(Image<CVD::byte>::iterator i=ni.begin(); i != ni.end(); i++)
*i = max(0, min(255, (int)floor(*i + rand_g() * s + .5)));
vector<ImageRef> c;
detector(ni, c, cpf);
corners.push_back(c);
num_corners += c.size();
}
//Compute and print the repeatability.
cout << s << " " << compute_repeatability_exact(warps, corners, fuzz) << " " << num_corners / images.size() << endl;
}
}
///This is the driver function. It reads the command line arguments and calls
///functions to load the data and compute the repeatability.
///
/// @param argc Number of command line argumentsd.
/// @param argv Pointer to command line arguments.
/// @ingroup gRepeatability
void mmain(int argc, char** argv)
{
GUI.LoadFile("test_repeatability.cfg");
GUI.parseArguments(argc, argv);
vector<Image<CVD::byte> > images;
vector<vector<Image<array<float, 2> > > > warps;
int n = GV3::get<int>("num", 2, 1);
string dir = GV3::get<string>("dir", "./", 1);
string format = GV3::get<string>("type", "cambridge", 1);
double fuzz = GV3::get<double>("r", 5, 1);
vector<int> cpf = GV3::get<vector<int> >("cpf", "0 10 20 30 40 50 60 70 80 90 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850 900 950 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2200", 1);
int ncpf = GV3::get<int>("ncpf", 500, 1);
float nmax = GV3::get<int>("nmax", 50, 1);
string test = GV3::get<string>("test", "normal", 1);
unique_ptr<DetectN> detector = get_detector();
tie(images, warps) = load_data(dir, n, format);
if(test == "noise")
compute_repeatability_noise(images, warps, *detector, ncpf, nmax, fuzz);
else
compute_repeatability_all(images, warps, *detector, cpf, fuzz);
}
///Driving function which catches exceptions
///@param argc Number of command line arguments
///@param argv Commandline argument list
int main(int argc, char** argv)
{
try
{
mmain(argc, argv);
}
catch(const Exceptions::All& e)
{
cerr << "Error: " << e.what() << endl;
}
}