-
Notifications
You must be signed in to change notification settings - Fork 13
/
demo.cpp
350 lines (326 loc) · 11.1 KB
/
demo.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
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
/*
BRISK - Binary Robust Invariant Scalable Keypoints
Reference implementation of
[1] Stefan Leutenegger,Margarita Chli and Roland Siegwart, BRISK:
Binary Robust Invariant Scalable Keypoints, in Proceedings of
the IEEE International Conference on Computer Vision (ICCV2011).
Copyright (C) 2011 The Autonomous Systems Lab (ASL), ETH Zurich,
Stefan Leutenegger, Simon Lynen and Margarita Chli.
This file is part of BRISK.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the ASL nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <opencv2/opencv.hpp>
#include "../include/brisk/brisk.h"
#include <fstream>
#include <iostream>
#include <list>
//standard configuration for the case of no file given
const int n=12;
const float r=2.5; // found 8-9-11, r=3.6, exponent 1.5
void help(char** argv){
std::cout << "This command line tool lets you evaluate different keypoint "
<< "detectors, descriptors and matchers." << std::endl
<< "usage:" << std::endl
<< argv[0] << " <dataset> <2nd> <detector> <descriptor> [descFile1 descFile1]" << std::endl
<< " " << "dataset: Folder containing the images. The images must be of .ppm "<< std::endl
<< " " << " format. They must be named img#.ppm, and there must be "<< std::endl
<< " " << " corresponding homographies named H1to#." << std::endl
<< " " << " You can also use the prefix rot-, then 2nd will be the" << std::endl
<< " " << " rotation in degrees." << std::endl
<< " " << "2nd: Number of the 2nd image (the 1st one is always img1.ppm)"<< std::endl
<< " " << " or the rotation in degrees, if rot- used." << std::endl
<< " " << "detector: Feature detector, e.g. AGAST, or BRISK. You can add the "<< std::endl
<< " " << " threshold, e.g. BRISK80 or SURF2000"<< std::endl
<< " " << "descriptor: Feature descriptor, e.g. SURF, BRIEF, BRISK or U-BRISK."<< std::endl
<< " " << "[descFile]: Optional: files with descriptors to act as detected points."<< std::endl;
}
int main(int argc, char ** argv) {
//std::cout<<sizeof(cv::Point2i)<<" "<<sizeof(CvPoint)<<std::endl;
// process command line args
if(argc != 5 && argc != 7 && argc != 1){
help(argv);
return 1;
}
// names of the two image files
std::string fname1;
std::string fname2;
cv::Mat imgRGB1;
cv::Mat imgRGB2;
cv::Mat imgRGB3;
bool do_rot=false;
// standard file extensions
std::vector<std::string> fextensions;
fextensions.push_back(".bmp");
fextensions.push_back(".jpeg");
fextensions.push_back(".jpg");
fextensions.push_back(".jpe");
fextensions.push_back(".jp2");
fextensions.push_back(".png");
fextensions.push_back(".pgm");
fextensions.push_back(".ppm");
fextensions.push_back(".sr");
fextensions.push_back(".ras");
fextensions.push_back(".tiff");
fextensions.push_back(".tif");
// if no arguments are passed:
if(argc==1){
int i=0;
int fextensions_size=fextensions.size();
while(imgRGB1.empty()||imgRGB2.empty()){
fname1 = "../res/images/img1"+fextensions[i];
fname2 = "../res/images/img2"+fextensions[i];
imgRGB1 = cv::imread(fname1);
imgRGB2 = cv::imread(fname2);
i++;
if(i>=fextensions_size) break;
}
if (imgRGB2.empty()||imgRGB2.empty())
{
std::cout<<"image(s) "<<fname1<<", "<<fname2<<" not found." << std::endl;
return 2;
}
}
else{
if(strncmp("rot-", argv[1], 4)==0){
do_rot=true;
int i=0;
int fextensions_size=fextensions.size();
while(imgRGB1.empty()){
fname1 = std::string(argv[1]+4)+"/img1"+fextensions[i];
imgRGB1 = cv::imread(fname1);
i++;
if(i>=fextensions_size) break;
}
if (imgRGB2.empty())
{
std::cout<<"image not found." << std::endl;
return 2;
}
}
else{
int i=0;
int fextensions_size=fextensions.size();
while(imgRGB1.empty()||imgRGB2.empty()){
fname1 = std::string(argv[1])+"/img1"+fextensions[i];
fname2 = std::string(argv[1])+"/img"+std::string(argv[2])+fextensions[i];
imgRGB1 = cv::imread(fname1);
imgRGB2 = cv::imread(fname2);
i++;
if(i>=fextensions_size) break;
}
if (imgRGB2.empty()||imgRGB2.empty())
{
std::cout<<"image(s)"<<fname1<<", "<<fname2<<" not found." << std::endl;
return 2;
}
}
//unsigned int N=atoi(argv[3]);
if (imgRGB1.empty())
{
fname1 = std::string(argv[1]+4)+"/img1.pgm";
imgRGB1 = cv::imread(fname1);
if (imgRGB1.empty()){
std::cout<<"image not found at " << fname1 << std::endl;
return 2;
}
}
}
// convert to grayscale
cv::Mat imgGray1;
cv::cvtColor(imgRGB1, imgGray1, CV_BGR2GRAY);
cv::Mat imgGray2;
if(!do_rot){
cv::cvtColor(imgRGB2, imgGray2, CV_BGR2GRAY);
}
// run FAST in first image
std::vector<cv::KeyPoint> keypoints, keypoints2;
int threshold;
// create the detector:
cv::Ptr<cv::FeatureDetector> detector;
if(argc==1){
detector = new brisk::BriskFeatureDetector(60,4);
}
else{
if(strncmp("FAST", argv[3], 4 )==0){
threshold = atoi(argv[3]+4);
if(threshold==0)
threshold = 30;
detector = new cv::FastFeatureDetector(threshold,true);
}
else if(strncmp("AGAST", argv[3], 5 )==0){
threshold = atoi(argv[3]+5);
if(threshold==0)
threshold = 30;
detector = new brisk::BriskFeatureDetector(threshold,0);
}
else if(strncmp("BRISK", argv[3], 5 )==0){
threshold = atoi(argv[3]+5);
if(threshold==0)
threshold = 30;
detector = new brisk::BriskFeatureDetector(threshold,4);
}
/*else if(strncmp("SURF", argv[3], 4 )==0){
threshold = atoi(argv[3]+4);
if(threshold==0)
threshold = 400;
detector = new cv::SurfFeatureDetector(threshold);
}
else if(strncmp("SIFT", argv[3], 4 )==0){
float thresh = 0.04 / cv::SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS / 2.0;
float edgeThreshold=atof(argv[3]+4);
if(edgeThreshold==0)
thresh = 10.0;
detector = new cv::SiftFeatureDetector(thresh,edgeThreshold);
}*/
else{
detector = cv::FeatureDetector::create( argv[3] );
}
if (detector.empty()){
std::cout << "Detector " << argv[3] << " not recognized. Check spelling!" << std::endl;
return 3;
}
}
// run the detector:
if(argc == 7){
// try to read descriptor files
std::string desc1 = std::string(argv[5]);
std::string desc2 = std::string(argv[6]);
std::ifstream descf1(desc1.c_str());
if(!descf1.good()){
std::cout<<"Descriptor file not found at " << desc1<<std::endl;
return 3;
}
std::ifstream descf2(desc2.c_str());
if(!descf2.good()){
std::cout<<"Descriptor file not found at " << desc2<<std::endl;
return 3;
}
// fill the keypoints
std::string str1;
std::stringstream strstrm1;
std::getline(descf1,str1);
std::getline(descf1,str1);
while(!descf1.eof()){
std::getline(descf1,str1);
float x,y,a;
strstrm1.str(str1);
strstrm1>>x;
strstrm1>>y;
strstrm1>>a;
float r=sqrt(1.0/a);
keypoints.push_back(cv::KeyPoint(x, y, 4.0*r));
}
std::string str2;
std::stringstream strstrm2;
std::getline(descf2,str2);
std::getline(descf2,str2);
while(!descf2.eof()){
std::getline(descf2,str2);
float x,y,a;
strstrm2.str(str2);
strstrm2>>x;
strstrm2>>y;
strstrm2>>a;
float r=sqrt(1.0/a);
keypoints2.push_back(cv::KeyPoint(x, y, 4.0*r));
}
// clean up
descf1.close();
descf2.close();
}
else{
detector->detect(imgGray1,keypoints);
detector->detect(imgGray2,keypoints2);
}
// now the extractor:
bool hamming=true;
cv::Ptr<cv::DescriptorExtractor> descriptorExtractor;
// now the extractor:
if(argc==1){
descriptorExtractor = new brisk::BriskDescriptorExtractor();
}
else{
if(std::string(argv[4])=="BRISK"){
descriptorExtractor = new brisk::BriskDescriptorExtractor();
}
else if(std::string(argv[4])=="U-BRISK"){
descriptorExtractor = new brisk::BriskDescriptorExtractor(false);
}
else if(std::string(argv[4])=="SU-BRISK"){
descriptorExtractor = new brisk::BriskDescriptorExtractor(false,false);
}
else if(std::string(argv[4])=="S-BRISK"){
descriptorExtractor = new brisk::BriskDescriptorExtractor(true,false);
}
else if(std::string(argv[4])=="BRIEF"){
descriptorExtractor = new cv::BriefDescriptorExtractor(64);
}
/*else if(std::string(argv[4])=="CALONDER"){
descriptorExtractor = new cv::CalonderDescriptorExtractor<float>("current.rtc");
hamming=false;
}
else if(std::string(argv[4])=="SURF"){
descriptorExtractor = new cv::SurfDescriptorExtractor();
hamming=false;
}
else if(std::string(argv[4])=="SIFT"){
descriptorExtractor = new cv::SiftDescriptorExtractor();
hamming=false;
}*/
else{
descriptorExtractor = cv::DescriptorExtractor::create( argv[4] );
}
if (descriptorExtractor.empty()){
hamming=false;
std::cout << "Descriptor " << argv[4] << " not recognized. Check spelling!" << std::endl;
return 4;
}
}
// get the descriptors
cv::Mat descriptors, descriptors2;
std::vector<cv::DMatch> indices;
// first image
descriptorExtractor->compute(imgGray2,keypoints2,descriptors2);
// and the second one
descriptorExtractor->compute(imgGray1,keypoints,descriptors);
// matching
std::vector<std::vector<cv::DMatch> > matches;
cv::Ptr<cv::DescriptorMatcher> descriptorMatcher;
if(hamming)
//descriptorMatcher = new cv::BruteForceMatcher<cv::HammingSse>();
descriptorMatcher = new cv::BFMatcher(cv::NORM_HAMMING);
else
descriptorMatcher = new cv::BFMatcher();
if(hamming)
descriptorMatcher->radiusMatch(descriptors2,descriptors,matches,100.0);
else
descriptorMatcher->radiusMatch(descriptors2,descriptors,matches,0.21);
cv::Mat outimg;
// drawing
drawMatches(imgRGB2, keypoints2, imgRGB1, keypoints,matches,outimg,
cv::Scalar(0,255,0), cv::Scalar(0,0,255),
std::vector<std::vector<char> >(), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
cv::namedWindow("Matches");
cv::imshow("Matches", outimg);
cv::waitKey();
return 0;
}