-
Notifications
You must be signed in to change notification settings - Fork 20
/
load_data.cc
398 lines (310 loc) · 9.51 KB
/
load_data.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
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 <cvd/image_io.h>
#include <cvd/vector_image_ref.h>
#include <array>
#include <TooN/TooN.h>
#include <TooN/LU.h>
#include <TooN/helpers.h>
#include <cstdlib>
#include "load_data.h"
#include "warp_to_png.h"
#include "utility.h"
#include "varprintf/varprintf.h"
///\cond never
using namespace std;
using namespace CVD;
using namespace varPrintf;
using namespace TooN;
///\endcond
/** Load images from a "Cambridge" style dataset.
@param dir The base directory of the dataset.
@param n The number of images in the dataset.
@param suffix Image filename suffix to use.
@return The loaded images.
@ingroup gDataset
*/
vector<Image<CVD::byte> > load_images_cambridge(string dir, int n, string suffix)
{
dir += "/frames/frame_%i." + suffix;
vector<Image<CVD::byte> > ret;
for(int i=0; i < n; i++)
{
Image<CVD::byte> im;
im = img_load(sPrintf(dir, i));
ret.push_back(im);
}
return ret;
}
/** Load images from an "Oxford VGG" style dataset.
@param dir The base directory of the dataset.
@param n The number of images in the dataset.
@return The loaded images.
@ingroup gDataset
*/
vector<Image<CVD::byte> > load_images_vgg(string dir, int n)
{
dir += "/img%i.ppm";
vector<Image<CVD::byte> > ret;
for(int i=0; i < n; i++)
ret.push_back(img_load(sPrintf(dir, i+1)));
return ret;
}
///Load an array from an istream
///@param i Stream to load from
///@param f array to load in to
///@ingroup gUtility
istream& operator>>(istream& i, array<float, 2>& f)
{
i >> f[0] >> f[1];
return i;
}
///Convert a vector in to an array
///@param vec Vector to convert
///@ingroup gUtility
array<float, 2> Arr(const Vector<2>& vec)
{
return {{(float)vec[0], (float)vec[1]}};
}
/**Load warps from a "Cambridge" repeatability dataset, with the warps
stored encoded in PNG files. See load_warps_cambridge
@param dir The base directory of the dataset.
@param num The numbers of images in the dataset.
@param size The size of the corresponding images.
@return <code>return_value[i][j][y][x]</code> is where pixel x, y in image i warps to in image j.
@ingroup gDataset
*/
vector<vector<Image<array<float,2> > > > load_warps_cambridge_png(string dir, int num, ImageRef size)
{
dir += "/pngwarps/warp_%i_%i.png";
vector<vector<Image<array<float, 2> > > > ret(num, vector<Image<array<float, 2> > >(num));
BasicImage<CVD::byte> tester(NULL, size);
array<float, 2> outside{{-1, -1}};
for(int from = 0; from < num; from ++)
for(int to = 0; to < num; to ++)
if(from != to)
{
string fname = sPrintf(dir, from, to);
Image<Rgb<unsigned short> > p = img_load(fname);
if(p.size() != size)
{
cerr << "Error: warp file " << fname << " is the wrong size!\n";
exit(1);
}
Image<array<float,2> > w(size, outside);
for(int y=0; y < size.y; y++)
for(int x=0; x < size.x; x++)
{
w[y][x][0] = p[y][x].red / MULTIPLIER - SHIFT;
w[y][x][1] = p[y][x].green / MULTIPLIER - SHIFT;
}
cerr << "Loaded " << fname << endl;
ret[from][to] = w;
}
return ret;
}
/**Load warps from a "Cambridge" repeatability dataset.
The dataset contains warps which round to outside the image by one pixel in the max direction.
Note that the line labelled "prune" is diasbled in the evaluation of the FAST-ER system. This
causes the two systems to produce slightly different results. If this line is commented out, then
FAST-ER generated detectors produce exactly the same results when loaded back in to this system.
@param dir The base directory of the dataset.
@param num The numbers of images in the dataset.
@param size The size of the corresponding images.
@return <code>return_value[i][j][y][x]</code> is where pixel x, y in image i warps to in image j.
@ingroup gDataset
*/
vector<vector<Image<array<float,2> > > > load_warps_cambridge(string dir, int num, ImageRef size)
{
dir += "/warps/warp_%i_%i.warp";
vector<vector<Image<array<float, 2> > > > ret(num, vector<Image<array<float, 2> > >(num));
BasicImage<CVD::byte> tester(NULL, size);
array<float, 2> outside{{-1, -1}};
for(int from = 0; from < num; from ++)
for(int to = 0; to < num; to ++)
if(from != to)
{
Image<array<float,2> > w(size, outside);
int n = size.x * size.y;
Image<array<float,2> >::iterator p = w.begin();
ifstream f;
string fname = sPrintf(dir, from, to);
f.open(fname.c_str());
if(!f.good())
{
cerr << "Error: " << fname << ": " << strerror(errno) << endl;
exit(1);
}
array<float, 2> v;
for(int i=0; i < n; ++i, ++p)
{
f >> v;
//prune
//if(v[0] >= 0 && v[1] >= 0 && v[0] <= size.x-1 && v[1] <= size.y-1)
*p = v;
}
if(!f.good())
{
cerr << "Error: " << fname << " went bad" << endl;
exit(1);
}
cerr << "Loaded " << fname << endl;
ret[from][to] = w;
}
return ret;
}
///Invert a matrix
///@param m Matrix to invert
///@ingroup gUtility
Matrix<3> invert(const Matrix<3>& m)
{
LU<3> i(m);
return i.get_inverse();
}
/**Load warps from an "Oxford VGG" repeatability dataset. The warps are stored
as homographies, so warps need to be generated.
@param dir The base directory of the dataset.
@param num The numbers of images in the dataset.
@param size The size of the corresponding images.
@return <code>return_value[i][j][y][x]</code> is where pixel x, y in image i warps to in image j.
@ingroup gDataset
*/
vector<vector<Image<array<float, 2> > > > load_warps_vgg(string dir, int num, ImageRef size)
{
dir += "/H1to%ip";
array<float, 2> outside{{-1, -1}};
//Load the homographies
vector<Matrix<3> > H_1_to_x;
//The first homography is always the identity.
{
Matrix<3> i = Identity;
H_1_to_x.push_back(i);
}
for(int i=2; i <= num; i++)
{
ifstream f;
string fname = sPrintf(dir, i).c_str();
f.open(fname.c_str());
Matrix<3> h;
f >> h;
if(!f.good())
{
cerr << "Error: " << fname << " went bad" << endl;
exit(1);
}
H_1_to_x.push_back(h);
}
vector<vector<Image<array<float, 2> > > > ret(num, vector<Image<array<float, 2> > >(num));
//Generate the warps.
for(int from = 0; from < num; from ++)
for(int to = 0; to < num; to ++)
if(from != to)
{
Matrix<3> from_to_one = invert(H_1_to_x[from]);
Matrix<3> one_to_to = H_1_to_x[to];
Matrix<3> from_to_to = one_to_to * from_to_one;
Image<array<float,2> > w(size, outside);
for(int y=0; y < size.y; y++)
for(int x=0; x < size.x; x++)
{
Vector<2> p = project(from_to_to * Vector<3>(makeVector( x, y, 1)));
if(p[0] >= 0 && p[1] >= 0 && p[0] <= size.x-1 && p[1] <= size.y-1)
w[y][x] = Arr(p);
}
ret[from][to] = w;
cerr << "Created warp " << from << " -> " << to << endl;
}
return ret;
}
enum DataFormat
{
Cambridge,
CambridgePNGWarp,
VGG
};
/**Load a dataset.
@param dir The base directory of the dataset.
@param num The number of images in the dataset.
@param format The type of the dataset. This should be one of `vgg', `cam-png' or `cam'.
@return The images and the warps.
@ingroup gDataset
*/
pair<vector<Image<CVD::byte> >, vector<vector<Image<array<float, 2> > > > > load_data(string dir, int num, string format)
{
vector<Image<CVD::byte> > images;
vector<vector<Image<array<float, 2> > > > warps;
DataFormat d;
if(format == "vgg")
d = VGG;
else if(format == "cam-png")
d = CambridgePNGWarp;
else
d = Cambridge;
switch(d)
{
case Cambridge:
images = load_images_cambridge(dir, num, "pgm");
break;
case CambridgePNGWarp:
images = load_images_cambridge(dir, num, "png");
break;
case VGG:
images = load_images_vgg(dir, num);
};
//Check for sanity
if(images.size() == 0)
{
cerr << "No images!\n";
exit(1);
}
for(unsigned int i=0; i < images.size(); i++)
if(images[i].size() != images[0].size())
{
cerr << "Images are different sizes!\n";
exit(1);
}
switch(d)
{
case CambridgePNGWarp:
warps = load_warps_cambridge_png(dir, num, images[0].size());
break;
case Cambridge:
warps = load_warps_cambridge(dir, num, images[0].size());
break;
case VGG:
warps = load_warps_vgg(dir, num, images[0].size());
};
return make_pair(images, warps);
}
/**
This function prunes a dataset so that no warped point will lie outside an image. This
will save on .in_image() tests later.
@param warps The warps to prune.
@param size the image size to prune to.
@ingroup gDataset
*/
void prune_warps(vector<vector<Image<array<float, 2> > > >& warps, ImageRef size)
{
BasicImage<CVD::byte> test(NULL, size);
array<float, 2> outside{{-1, -1}};
for(unsigned int i=0; i < warps.size(); i++)
for(unsigned int j=0; j < warps[i].size(); j++)
{
for(Image<array<float, 2> >::iterator p=warps[i][j].begin(); p != warps[i][j].end(); p++)
if(!test.in_image(ir_rounded(*p)))
*p = outside;
}
}