-
Notifications
You must be signed in to change notification settings - Fork 15
/
akaze_match.cpp
291 lines (256 loc) · 9.49 KB
/
akaze_match.cpp
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
//=============================================================================
//
// akaze_match.cpp
// Authors: Pablo F. Alcantarilla (1), Jesus Nuevo (2)
// Institutions: Toshiba Research Europe Ltd (1)
// TrueVision Solutions (2)
// Date: 07/10/2014
// Email: [email protected]
//
// AKAZE Features Copyright 2014, Pablo F. Alcantarilla, Jesus Nuevo
// All Rights Reserved
// See LICENSE for the license information
//=============================================================================
/**
* @file akaze_match.cpp
* @brief Main program for matching two images with AKAZE features
* @date Oct 07, 2014
* @author Pablo F. Alcantarilla
*/
#include <string>
#include <utility>
#include <vector>
#include "cimg/CImg.h"
#include "src/AKAZE.h"
#include "src/AKAZEConfig.h"
#include "src/utils.h"
#include "timer/timer.hpp"
/* ************************************************************************* */
// Image matching options
const float MIN_H_ERROR =
2.50f; ///< Maximum error in pixels to accept an inlier
const float DRATIO = 0.80f; ///< NNDR Matching value
/* ************************************************************************* */
/**
* @brief This function parses the command line arguments for setting A-KAZE
* parameters
* and image matching between two input images
* @param options Structure that contains A-KAZE settings
* @param img_path1 Path for the first input image
* @param img_path2 Path for the second input image
* @param homography_path Path for the file that contains the ground truth
* homography
*/
int parse_input_options(libAKAZE::AKAZEOptions& options, std::string& img_path1,
std::string& img_path2, std::string& homography_path,
int argc, char* argv[]);
/* ************************************************************************* */
int main(int argc, char* argv[]) {
// Variables
libAKAZE::AKAZEOptions options;
std::string img_path1, img_path2, homography_path;
float ratio = 0.0;
int nkpts1 = 0, nkpts2 = 0, nmatches = 0, ninliers = 0, noutliers = 0;
std::vector<libAKAZE::AKAZEKeypoint> kpts1, kpts2;
std::vector<std::pair<int, int> > dmatches;
libAKAZE::AKAZEDescriptors desc1, desc2;
Eigen::Matrix3f HG;
// Variables for measuring computation times
double takaze = 0.0, tmatch = 0.0;
// Parse the input command line options
if (parse_input_options(options, img_path1, img_path2, homography_path, argc,
argv)) {
return -1;
}
// Read image 1 and if necessary convert to grayscale.
cimg_library::CImg<float> img1(img_path1.c_str());
cimg_library::CImg<float> img2(img_path2.c_str());
// Read ground truth homography file
if (read_homography(homography_path, HG) == false) {
std::cout << "Invalid homography file!" << std::endl;
return -1;
}
// Convert the images to float
RowMatrixXf img1_32, img2_32;
ConvertCImgToEigen(img1, img1_32);
img1_32 /= 255.0;
ConvertCImgToEigen(img2, img2_32);
img2_32 /= 255.0;
// Create the first AKAZE object
options.img_width = img1.width();
options.img_height = img1.height();
libAKAZE::AKAZE evolution1(options);
// Create the second HKAZE object
options.img_width = img2.width();
options.img_height = img2.height();
libAKAZE::AKAZE evolution2(options);
timer::Timer timer;
// Create the nonlinear scale space
// and perform feature detection and description for image 1
evolution1.Create_Nonlinear_Scale_Space(img1_32);
evolution1.Feature_Detection(kpts1);
evolution1.Compute_Descriptors(kpts1, desc1);
evolution2.Create_Nonlinear_Scale_Space(img2_32);
evolution2.Feature_Detection(kpts2);
evolution2.Compute_Descriptors(kpts2, desc2);
takaze = timer.elapsedMs();
nkpts1 = kpts1.size();
nkpts2 = kpts2.size();
// Matching Descriptors!!
std::vector<std::pair<int, int> > matches, inliers;
timer.reset();
if (options.descriptor < libAKAZE::MLDB_UPRIGHT) {
match_features(desc1.float_descriptor, desc2.float_descriptor, DRATIO,
matches);
}
// Binary descriptor, use Hamming distance
else {
match_features(desc1.binary_descriptor, desc2.binary_descriptor, DRATIO,
matches);
}
tmatch = timer.elapsedMs();
// Compute Inliers!!
compute_inliers_homography(kpts1, kpts2, matches, inliers, HG, MIN_H_ERROR);
// Compute the inliers statistics
nmatches = matches.size();
ninliers = inliers.size();
noutliers = nmatches - ninliers;
ratio = 100.0 * ((float)ninliers / (float)nmatches);
// Show matching statistics
std::cout << "Number of Keypoints Image 1: " << nkpts1 << std::endl;
std::cout << "Number of Keypoints Image 2: " << nkpts2 << std::endl;
std::cout << "A-KAZE Features Extraction Time (ms): " << takaze << std::endl;
std::cout << "Matching Descriptors Time (ms): " << tmatch << std::endl;
std::cout << "Number of Matches: " << nmatches << std::endl;
std::cout << "Number of Inliers: " << ninliers << std::endl;
std::cout << "Number of Outliers: " << noutliers << std::endl;
std::cout << "Inliers Ratio: " << ratio << std::endl << std::endl;
cimg_library::CImg<float> img1_rgb =
img1.get_resize(img1.width(), img1.height(), img1.depth(), 3);
cimg_library::CImg<float> img2_rgb =
img2.get_resize(img2.width(), img2.height(), img2.depth(), 3);
draw_keypoints(img1_rgb, kpts1);
draw_keypoints(img2_rgb, kpts2);
cimg_library::CImg<float> matched_image;
draw_matches(img1_rgb, img2_rgb, kpts1, kpts2, inliers, matched_image);
matched_image.save("../output/matched_images.jpg");
}
/* ************************************************************************* */
int parse_input_options(libAKAZE::AKAZEOptions& options, std::string& img_path1,
std::string& img_path2, std::string& homography_path,
int argc, char* argv[]) {
// If there is only one argument return
if (argc == 1) {
show_input_options_help(1);
return -1;
}
// Set the options from the command line
else if (argc >= 2) {
// Load the default options
options = libAKAZE::AKAZEOptions();
if (!strcmp(argv[1], "--help")) {
show_input_options_help(1);
return -1;
}
img_path1 = argv[1];
img_path2 = argv[2];
if (argc >= 4) homography_path = argv[3];
for (int i = 3; i < argc; i++) {
if (!strcmp(argv[i], "--num_threads")) {
i = i + 1;
options.num_threads = atoi(argv[i]);
} else if (!strcmp(argv[i], "--soffset")) {
i = i + 1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
} else {
options.soffset = atof(argv[i]);
}
} else if (!strcmp(argv[i], "--omax")) {
i = i + 1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
} else {
options.omax = atof(argv[i]);
}
} else if (!strcmp(argv[i], "--dthreshold")) {
i = i + 1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
} else {
options.dthreshold = atof(argv[i]);
}
} else if (!strcmp(argv[i], "--sderivatives")) {
i = i + 1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
} else {
options.sderivatives = atof(argv[i]);
}
} else if (!strcmp(argv[i], "--nsublevels")) {
i = i + 1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
} else {
options.nsublevels = atoi(argv[i]);
}
} else if (!strcmp(argv[i], "--diffusivity")) {
i = i + 1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
} else {
options.diffusivity = libAKAZE::DIFFUSIVITY_TYPE(atoi(argv[i]));
}
} else if (!strcmp(argv[i], "--descriptor")) {
i = i + 1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
} else {
options.descriptor = libAKAZE::DESCRIPTOR_TYPE(atoi(argv[i]));
if (options.descriptor < 0 || options.descriptor > libAKAZE::MLDB) {
options.descriptor = libAKAZE::MLDB;
}
}
} else if (!strcmp(argv[i], "--descriptor_channels")) {
i = i + 1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
} else {
options.descriptor_channels = atoi(argv[i]);
if (options.descriptor_channels <= 0 ||
options.descriptor_channels > 3) {
options.descriptor_channels = 3;
}
}
} else if (!strcmp(argv[i], "--descriptor_size")) {
i = i+1;
if (i >= argc) {
std::cerr << "Error introducing input options!!" << std::endl;
return -1;
}
else {
options.descriptor_size = atoi(argv[i]);
if (options.descriptor_size < 0) {
options.descriptor_size = 0;
}
}
} else if (!strcmp(argv[i], "--verbose")) {
options.verbosity = true;
} else if (!strncmp(argv[i], "--", 2))
std::cerr << "Unknown command " << argv[i] << std::endl;
}
} else {
std::cerr << "Error introducing input options!!" << std::endl;
show_input_options_help(1);
return -1;
}
return 0;
}