Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/matchmethod_orb_akaze_brisk.cpp
16337 views
1
#include <opencv2/core.hpp>
2
#include <opencv2/imgproc.hpp>
3
#include <opencv2/features2d.hpp>
4
#include <opencv2/highgui.hpp>
5
#include <vector>
6
#include <iostream>
7
8
using namespace std;
9
using namespace cv;
10
11
static void help()
12
{
13
cout << "\n This program demonstrates how to detect compute and match ORB BRISK and AKAZE descriptors \n"
14
"Usage: \n"
15
" ./matchmethod_orb_akaze_brisk --image1=<image1(../data/basketball1.png as default)> --image2=<image2(../data/basketball2.png as default)>\n"
16
"Press a key when image window is active to change algorithm or descriptor";
17
}
18
19
20
21
int main(int argc, char *argv[])
22
{
23
vector<String> typeDesc;
24
vector<String> typeAlgoMatch;
25
vector<String> fileName;
26
// This descriptor are going to be detect and compute
27
typeDesc.push_back("AKAZE-DESCRIPTOR_KAZE_UPRIGHT"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html
28
typeDesc.push_back("AKAZE"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html
29
typeDesc.push_back("ORB"); // see http://docs.opencv.org/trunk/de/dbf/classcv_1_1BRISK.html
30
typeDesc.push_back("BRISK"); // see http://docs.opencv.org/trunk/db/d95/classcv_1_1ORB.html
31
// This algorithm would be used to match descriptors see http://docs.opencv.org/trunk/db/d39/classcv_1_1DescriptorMatcher.html#ab5dc5036569ecc8d47565007fa518257
32
typeAlgoMatch.push_back("BruteForce");
33
typeAlgoMatch.push_back("BruteForce-L1");
34
typeAlgoMatch.push_back("BruteForce-Hamming");
35
typeAlgoMatch.push_back("BruteForce-Hamming(2)");
36
cv::CommandLineParser parser(argc, argv,
37
"{ @image1 | ../data/basketball1.png | }"
38
"{ @image2 | ../data/basketball2.png | }"
39
"{help h ||}");
40
if (parser.has("help"))
41
{
42
help();
43
return 0;
44
}
45
fileName.push_back(parser.get<string>(0));
46
fileName.push_back(parser.get<string>(1));
47
Mat img1 = imread(fileName[0], IMREAD_GRAYSCALE);
48
Mat img2 = imread(fileName[1], IMREAD_GRAYSCALE);
49
if (img1.rows*img1.cols <= 0)
50
{
51
cout << "Image " << fileName[0] << " is empty or cannot be found\n";
52
return(0);
53
}
54
if (img2.rows*img2.cols <= 0)
55
{
56
cout << "Image " << fileName[1] << " is empty or cannot be found\n";
57
return(0);
58
}
59
60
vector<double> desMethCmp;
61
Ptr<Feature2D> b;
62
63
// Descriptor loop
64
vector<String>::iterator itDesc;
65
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
66
{
67
Ptr<DescriptorMatcher> descriptorMatcher;
68
// Match between img1 and img2
69
vector<DMatch> matches;
70
// keypoint for img1 and img2
71
vector<KeyPoint> keyImg1, keyImg2;
72
// Descriptor for img1 and img2
73
Mat descImg1, descImg2;
74
vector<String>::iterator itMatcher = typeAlgoMatch.end();
75
if (*itDesc == "AKAZE-DESCRIPTOR_KAZE_UPRIGHT"){
76
b = AKAZE::create(AKAZE::DESCRIPTOR_KAZE_UPRIGHT);
77
}
78
if (*itDesc == "AKAZE"){
79
b = AKAZE::create();
80
}
81
if (*itDesc == "ORB"){
82
b = ORB::create();
83
}
84
else if (*itDesc == "BRISK"){
85
b = BRISK::create();
86
}
87
try
88
{
89
// We can detect keypoint with detect method
90
b->detect(img1, keyImg1, Mat());
91
// and compute their descriptors with method compute
92
b->compute(img1, keyImg1, descImg1);
93
// or detect and compute descriptors in one step
94
b->detectAndCompute(img2, Mat(),keyImg2, descImg2,false);
95
// Match method loop
96
for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); ++itMatcher){
97
descriptorMatcher = DescriptorMatcher::create(*itMatcher);
98
if ((*itMatcher == "BruteForce-Hamming" || *itMatcher == "BruteForce-Hamming(2)") && (b->descriptorType() == CV_32F || b->defaultNorm() <= NORM_L2SQR))
99
{
100
cout << "**************************************************************************\n";
101
cout << "It's strange. You should use Hamming distance only for a binary descriptor\n";
102
cout << "**************************************************************************\n";
103
}
104
if ((*itMatcher == "BruteForce" || *itMatcher == "BruteForce-L1") && (b->defaultNorm() >= NORM_HAMMING))
105
{
106
cout << "**************************************************************************\n";
107
cout << "It's strange. You shouldn't use L1 or L2 distance for a binary descriptor\n";
108
cout << "**************************************************************************\n";
109
}
110
try
111
{
112
descriptorMatcher->match(descImg1, descImg2, matches, Mat());
113
// Keep best matches only to have a nice drawing.
114
// We sort distance between descriptor matches
115
Mat index;
116
int nbMatch=int(matches.size());
117
Mat tab(nbMatch, 1, CV_32F);
118
for (int i = 0; i<nbMatch; i++)
119
{
120
tab.at<float>(i, 0) = matches[i].distance;
121
}
122
sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
123
vector<DMatch> bestMatches;
124
for (int i = 0; i<30; i++)
125
{
126
bestMatches.push_back(matches[index.at<int>(i, 0)]);
127
}
128
Mat result;
129
drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result);
130
namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE);
131
imshow(*itDesc + ": " + *itMatcher, result);
132
// Saved result could be wrong due to bug 4308
133
FileStorage fs(*itDesc + "_" + *itMatcher + ".yml", FileStorage::WRITE);
134
fs<<"Matches"<<matches;
135
vector<DMatch>::iterator it;
136
cout<<"**********Match results**********\n";
137
cout << "Index \tIndex \tdistance\n";
138
cout << "in img1\tin img2\n";
139
// Use to compute distance between keyPoint matches and to evaluate match algorithm
140
double cumSumDist2=0;
141
for (it = bestMatches.begin(); it != bestMatches.end(); ++it)
142
{
143
cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->distance << "\n";
144
Point2d p=keyImg1[it->queryIdx].pt-keyImg2[it->trainIdx].pt;
145
cumSumDist2=p.x*p.x+p.y*p.y;
146
}
147
desMethCmp.push_back(cumSumDist2);
148
waitKey();
149
}
150
catch (Exception& e)
151
{
152
cout << e.msg << endl;
153
cout << "Cumulative distance cannot be computed." << endl;
154
desMethCmp.push_back(-1);
155
}
156
}
157
}
158
catch (Exception& e)
159
{
160
cout << "Feature : " << *itDesc << "\n";
161
if (itMatcher != typeAlgoMatch.end())
162
{
163
cout << "Matcher : " << *itMatcher << "\n";
164
}
165
cout << e.msg << endl;
166
}
167
}
168
int i=0;
169
cout << "Cumulative distance between keypoint match for different algorithm and feature detector \n\t";
170
cout << "We cannot say which is the best but we can say results are different! \n\t";
171
for (vector<String>::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); ++itMatcher)
172
{
173
cout<<*itMatcher<<"\t";
174
}
175
cout << "\n";
176
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
177
{
178
cout << *itDesc << "\t";
179
for (vector<String>::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); ++itMatcher, ++i)
180
{
181
cout << desMethCmp[i]<<"\t";
182
}
183
cout<<"\n";
184
}
185
return 0;
186
}
187
188