-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmatch_brouteforce_fast.cxx
105 lines (79 loc) · 2.74 KB
/
match_brouteforce_fast.cxx
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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp> //
#include <opencv2/nonfree/nonfree.hpp>
#include <iostream>
#include <dirent.h>
#include <ctime>
using namespace cv;
using namespace std;
#define IMG_DIR "./imgs/"
bool has_suffix(const std::string &str, const std::string &suffix);
void showFiles(string &method){
DIR *dir;
struct dirent *ent;
cout << "We Have Imags as follows:\n";
if((dir = opendir(IMG_DIR)) != NULL){
int count = 0;
while((ent = readdir(dir)) != NULL){
string name = (string)(ent->d_name);
// jpg , jpeg imags
if(has_suffix(name, "g")){
count ++;
cout << name << "\t";
if(count == 5){
cout << endl;
count = 0;
}
}
}
}else{
cout << "cannot open the directory!\n";
exit(-1);
}
cout << "\n Choose Two ,I will match them by " << method <<endl;
}
int main(int argc, const char *argv[]){
if(argc != 2){
cout << "usage:match <method>\n" ;
exit(-1);
}
string method = string(argv[1]);
showFiles(method);
string image1_name,image2_name;
getline(cin, image1_name);
getline(cin, image2_name);
Mat image1 = imread(IMG_DIR + image1_name, 0);
Mat image2 = imread(IMG_DIR + image2_name, 0);
FastFeatureDetector fast(40); // 检测的阈值为40
SurfDescriptorExtractor extractor;
//Ptr<DescriptorExtractor> extractor = DescriptorExtractor::create("SIFT");
// WHY CANNOT WORK ???
clock_t begin = clock();
vector<KeyPoint> keypoints1, keypoints2;
fast.detect(image1, keypoints1);
fast.detect(image2, keypoints2);
cout << "# keypoints of image1 :" << keypoints1.size() << endl;
cout << "# keypoints of image2 :" << keypoints2.size() << endl;
Mat descriptors1,descriptors2;
extractor.compute(image1,keypoints1,descriptors1);
extractor.compute(image2,keypoints2,descriptors2);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << "Time Costs : " << elapsed_secs << endl;
BFMatcher bfmatcher(NORM_L2, true);
vector<DMatch> matches;
bfmatcher.match(descriptors1, descriptors2, matches);
cout << "# matches : " << matches.size() << endl;
// show it on an image
Mat output;
drawMatches(image1, keypoints1, image2, keypoints2, matches, output);
imshow("Matches result",output);
waitKey(0);
return 0;
}
bool has_suffix(const std::string &str, const std::string &suffix)
{
return str.size() >= suffix.size() &&
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}