32 vector<String> typeDesc;
33 vector<String> typeAlgoMatch;
34 vector<String> fileName;
43 typeDesc.push_back(
"AKAZE-DESCRIPTOR_KAZE_UPRIGHT");
44 typeDesc.push_back(
"AKAZE");
45 typeDesc.push_back(
"ORB");
46 typeDesc.push_back(
"BRISK");
49 typeAlgoMatch.push_back(
"BruteForce");
50 typeAlgoMatch.push_back(
"BruteForce-L1");
51 typeAlgoMatch.push_back(
"BruteForce-Hamming");
52 typeAlgoMatch.push_back(
"BruteForce-Hamming(2)");
54 const String keys =
"{@image1 | | Reference image }" 55 "{@image2 | | Comparison image }" 57 CommandLineParser parser(argc, argv, keys);
58 if (parser.has(
"help"))
63 fileName.push_back(parser.get<
string>((
int) 0));
64 fileName.push_back(parser.get<
string>(1));
65 Mat img1 = imread(fileName[0], IMREAD_GRAYSCALE);
66 Mat img2 = imread(fileName[1], IMREAD_GRAYSCALE);
67 if (img1.rows * img1.cols <= 0)
69 cout <<
"Image " << fileName[0] <<
" is empty or cannot be found\n";
72 if (img2.rows * img2.cols <= 0)
74 cout <<
"Image " << fileName[1] <<
" is empty or cannot be found\n";
78 vector<double> desMethCmp;
82 vector<String>::iterator itDesc;
83 for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
85 Ptr<DescriptorMatcher> descriptorMatcher;
87 vector<DMatch> matches;
89 vector<KeyPoint> keyImg1, keyImg2;
91 Mat descImg1, descImg2;
92 vector<String>::iterator itMatcher = typeAlgoMatch.end();
93 if (*itDesc ==
"AKAZE-DESCRIPTOR_KAZE_UPRIGHT")
95 b = AKAZE::create(AKAZE::DESCRIPTOR_KAZE_UPRIGHT);
97 if (*itDesc ==
"AKAZE")
101 if (*itDesc ==
"ORB")
105 else if (*itDesc ==
"BRISK")
112 b->detect(img1, keyImg1, Mat());
113 b->compute(img1, keyImg1, descImg1);
116 b->detectAndCompute(img2, Mat(), keyImg2, descImg2,
false);
119 for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); ++itMatcher)
121 descriptorMatcher = DescriptorMatcher::create(*itMatcher);
122 if (((*itMatcher ==
"BruteForce-Hamming") || (*itMatcher ==
"BruteForce-Hamming(2)")) &&
123 ((b->descriptorType() == CV_32F) || (b->defaultNorm() <= NORM_L2SQR)))
125 cout <<
"**************************************************************************\n";
126 cout <<
"It's strange. You should use Hamming distance only for a binary descriptor\n";
127 cout <<
"**************************************************************************\n";
129 if ((*itMatcher ==
"BruteForce" || *itMatcher ==
"BruteForce-L1") && (b->defaultNorm()
132 cout <<
"**************************************************************************\n";
133 cout <<
"It's strange. You shouldn't use L1 or L2 distance for a binary descriptor\n";
134 cout <<
"**************************************************************************\n";
138 descriptorMatcher->match(descImg1, descImg2, matches, Mat());
142 int nbMatch = int(matches.size());
143 Mat tab(nbMatch, 1, CV_32F);
144 for (
int i = 0; i < nbMatch; i++)
146 tab.at<
float>(i, 0) = matches[i].
distance;
148 sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
149 vector<DMatch> bestMatches;
150 for (
int i = 0; i < 30; i++)
152 bestMatches.push_back(matches[index.at<
int>(i, 0)]);
155 drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result);
156 namedWindow(*itDesc +
": " + *itMatcher, WINDOW_AUTOSIZE);
157 imshow(*itDesc +
": " + *itMatcher, result);
159 FileStorage fs(*itDesc +
"_" + *itMatcher +
".yml", FileStorage::WRITE);
160 fs <<
"Matches" << matches;
161 vector<DMatch>::iterator it;
162 cout <<
"**********Match results**********\n";
163 cout <<
"Index \tIndex \tdistance\n";
164 cout <<
"in img1\tin img2\n";
166 double cumSumDist2 = 0;
167 for (it = bestMatches.begin(); it != bestMatches.end(); ++it)
169 cout << it->queryIdx <<
"\t" << it->trainIdx <<
"\t" << it->distance <<
"\n";
170 Point2d p = keyImg1[it->queryIdx].pt - keyImg2[it->trainIdx].pt;
171 cumSumDist2 = p.x * p.x + p.y * p.y;
173 desMethCmp.push_back(cumSumDist2);
178 cout << e.msg << endl;
179 cout <<
"Cumulative distance cannot be computed." << endl;
180 desMethCmp.push_back(-1);
186 cout <<
"Feature : " << *itDesc <<
"\n";
187 if (itMatcher != typeAlgoMatch.end())
189 cout <<
"Matcher : " << *itMatcher <<
"\n";
191 cout << e.msg << endl;
195 cout <<
"Cumulative distance between keypoint match for different algorithm and feature detector \n\t";
196 cout <<
"We cannot say which is the best but we can say results are differents! \n\t";
197 for (vector<String>::iterator itMatcher = typeAlgoMatch.begin();
198 itMatcher != typeAlgoMatch.end(); ++itMatcher)
200 cout << *itMatcher <<
"\t";
203 for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
205 cout << *itDesc <<
"\t";
206 for (vector<String>::iterator itMatcher = typeAlgoMatch.begin();
207 itMatcher != typeAlgoMatch.end(); ++itMatcher, ++i)
209 cout << desMethCmp[i] <<
"\t";
float distance(double lat1, double lon1, double lat2, double lon2, int units)