From 3e25c085bac83586113a3f1247c3bc2ea865cfdf Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Sun, 3 May 2015 11:42:27 +0200 Subject: [PATCH 01/12] A sample program : how to use ORB AKAZE BRISK descriptors for matching. see http://answers.opencv.org/question/61062/features2d-example/ --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 92 +++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 samples/cpp/matchmethod_orb_akaze_brisk.cpp diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp new file mode 100644 index 000000000..708c12964 --- /dev/null +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -0,0 +1,92 @@ +#include +#include +#include + +using namespace std; +using namespace cv; + + +int main(void) +{ + vector typeAlgoMatch; + typeAlgoMatch.push_back("BruteForce"); + typeAlgoMatch.push_back("BruteForce-Hamming"); + typeAlgoMatch.push_back("BruteForce-Hamming(2)"); + + vector typeDesc; + typeDesc.push_back("AKAZE"); + typeDesc.push_back("ORB"); + typeDesc.push_back("BRISK"); + + String dataFolder("../data/"); + vector fileName; + fileName.push_back("basketball1.png"); + fileName.push_back("basketball2.png"); + + Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE); + Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE); + + Ptr b; + + vector::iterator itDesc; +// Descriptor loop + for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++){ + Ptr descriptorMatcher; + vector matches; /* keyImg1; /* keyImg2; /*::iterator itMatcher = typeAlgoMatch.end(); + if (*itDesc == "AKAZE"){ + b = AKAZE::create(); + } + if (*itDesc == "ORB"){ + b = ORB::create(); + } + else if (*itDesc == "BRISK"){ + b = BRISK::create(); + } + try { + b->detect(img1, keyImg1, Mat()); + b->compute(img1, keyImg1, descImg1); + b->detectAndCompute(img2, Mat(),keyImg2, descImg2,false); + // Match method loop + for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){ + descriptorMatcher = DescriptorMatcher::create(*itMatcher); + descriptorMatcher->match(descImg1, descImg2, matches, Mat()); + // Keep best matches only to have a nice drawing + Mat index; + Mat tab(matches.size(), 1, CV_32F); + for (int i = 0; i(i, 0) = matches[i].distance; + sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING); + vector bestMatches; /*(i, 0)]); + + Mat result; + drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result); + namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE); + imshow(*itDesc + ": " + *itMatcher, result); + FileStorage fs(*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE); + fs<<"Matches"<::iterator it; + + cout << "Index \tIndex \tindex \tdistance\n"; + cout << "in img1\tin img2\timage\t\n"; + for (it = matches.begin(); it != matches.end(); it++) + cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->imgIdx << "\t" << it->distance<<"\n"; + waitKey(); + } + } + catch (Exception& e){ + cout << "Feature : " << *itDesc << "\n"; + if (itMatcher != typeAlgoMatch.end()) + cout << "Matcher : " << *itMatcher << "\n"; + cout< Date: Sun, 3 May 2015 11:58:32 +0200 Subject: [PATCH 02/12] Trailing whitespace --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index 708c12964..dcad48a74 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -12,7 +12,7 @@ int main(void) typeAlgoMatch.push_back("BruteForce"); typeAlgoMatch.push_back("BruteForce-Hamming"); typeAlgoMatch.push_back("BruteForce-Hamming(2)"); - + vector typeDesc; typeDesc.push_back("AKAZE"); typeDesc.push_back("ORB"); @@ -22,7 +22,7 @@ int main(void) vector fileName; fileName.push_back("basketball1.png"); fileName.push_back("basketball2.png"); - + Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE); Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE); @@ -61,19 +61,15 @@ int main(void) tab.at(i, 0) = matches[i].distance; sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING); vector bestMatches; /*(i, 0)]); - Mat result; drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result); namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE); imshow(*itDesc + ": " + *itMatcher, result); FileStorage fs(*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE); fs<<"Matches"<::iterator it; - cout << "Index \tIndex \tindex \tdistance\n"; cout << "in img1\tin img2\timage\t\n"; for (it = matches.begin(); it != matches.end(); it++) From 341de5d53fc16da9f4a55faa1ea6a57819f66ebb Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Sun, 3 May 2015 12:04:19 +0200 Subject: [PATCH 03/12] trailing whitespace --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index dcad48a74..f5a0a25fb 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -54,13 +54,13 @@ int main(void) for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){ descriptorMatcher = DescriptorMatcher::create(*itMatcher); descriptorMatcher->match(descImg1, descImg2, matches, Mat()); - // Keep best matches only to have a nice drawing + // Keep best matches only to have a nice drawing Mat index; Mat tab(matches.size(), 1, CV_32F); for (int i = 0; i(i, 0) = matches[i].distance; sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING); - vector bestMatches; /* bestMatches; for (int i = 0; i<30; i++) bestMatches.push_back(matches[index.at(i, 0)]); Mat result; From 56856a1810d9d3854778b87a3b21983b83e6dd68 Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Sun, 3 May 2015 13:28:23 +0200 Subject: [PATCH 04/12] Conversion size-t in int --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index f5a0a25fb..556870ec4 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -56,7 +56,7 @@ int main(void) descriptorMatcher->match(descImg1, descImg2, matches, Mat()); // Keep best matches only to have a nice drawing Mat index; - Mat tab(matches.size(), 1, CV_32F); + Mat tab(int(matches.size()), 1, CV_32F); for (int i = 0; i(i, 0) = matches[i].distance; sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING); From 9ddb8bdb8bf4c593d34647ff9f49be4e5a6abd04 Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Sun, 3 May 2015 14:08:24 +0200 Subject: [PATCH 05/12] Problem with warning: comparison between signed and unsigned integer expressions --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index 556870ec4..6711ba34b 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -56,8 +56,9 @@ int main(void) descriptorMatcher->match(descImg1, descImg2, matches, Mat()); // Keep best matches only to have a nice drawing Mat index; - Mat tab(int(matches.size()), 1, CV_32F); - for (int i = 0; i(i, 0) = matches[i].distance; sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING); vector bestMatches; From 09e9e8ad9fa4253fae623db21dd7df397421074c Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Mon, 4 May 2015 10:36:24 +0200 Subject: [PATCH 06/12] I have modified source file and add a chart to have distance between keypoint for decriptor function og matching algorithm My english is not good so you can change some words in my comment --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 128 +++++++++++++++----- 1 file changed, 99 insertions(+), 29 deletions(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index 6711ba34b..be6bf3553 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -5,37 +5,73 @@ using namespace std; using namespace cv; - -int main(void) +static void help() { + cout << "\n This program demonstrates how to detect compute and match ORB BRISK and AKAZE descriptors \n" + "Usage: \n" + " ./matchmethod_orb_akaze_brisk \n" + "Press a key when image window is active to change algorithm or descriptor"; +} + + + +int main(int argc, char *argv[]) +{ + vector typeDesc; vector typeAlgoMatch; + vector fileName; + help(); + // This descriptor are going to be detect and compute + typeDesc.push_back("AKAZE"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html + typeDesc.push_back("ORB"); // see http://docs.opencv.org/trunk/de/dbf/classcv_1_1BRISK.html + typeDesc.push_back("BRISK"); // see http://docs.opencv.org/trunk/db/d95/classcv_1_1ORB.html + // This algorithm would be used to match descriptors see http://docs.opencv.org/trunk/db/d39/classcv_1_1DescriptorMatcher.html#ab5dc5036569ecc8d47565007fa518257 typeAlgoMatch.push_back("BruteForce"); + typeAlgoMatch.push_back("BruteForce-L1"); typeAlgoMatch.push_back("BruteForce-Hamming"); typeAlgoMatch.push_back("BruteForce-Hamming(2)"); + if (argc==1) + { + fileName.push_back("../data/basketball1.png"); + fileName.push_back("../data/basketball2.png"); + } + else if (argc==3) + { + fileName.push_back(argv[1]); + fileName.push_back(argv[2]); + } + else + { + help(); + return(0); + } + Mat img1 = imread(fileName[0], IMREAD_GRAYSCALE); + Mat img2 = imread(fileName[1], IMREAD_GRAYSCALE); + if (img1.rows*img1.cols <= 0) + { + cout << "Image " << fileName[0] << " is empty or cannot be found\n"; + return(0); + } + if (img2.rows*img2.cols <= 0) + { + cout << "Image " << fileName[1] << " is empty or cannot be found\n"; + return(0); + } - vector typeDesc; - typeDesc.push_back("AKAZE"); - typeDesc.push_back("ORB"); - typeDesc.push_back("BRISK"); - - String dataFolder("../data/"); - vector fileName; - fileName.push_back("basketball1.png"); - fileName.push_back("basketball2.png"); - - Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE); - Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE); - + vector desMethCmp; Ptr b; + // Descriptor loop vector::iterator itDesc; -// Descriptor loop - for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++){ + for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++) + { Ptr descriptorMatcher; - vector matches; /* keyImg1; /* keyImg2; /* matches; + // keypoint for img1 and img2 + vector keyImg1, keyImg2; + // Descriptor for img1 and img2 + Mat descImg1, descImg2; vector::iterator itMatcher = typeAlgoMatch.end(); if (*itDesc == "AKAZE"){ b = AKAZE::create(); @@ -47,23 +83,31 @@ int main(void) b = BRISK::create(); } try { + // We can detect keypoint with detect method b->detect(img1, keyImg1, Mat()); + // and compute their descriptors with method compute b->compute(img1, keyImg1, descImg1); + // or detect and compute descriptors in one step b->detectAndCompute(img2, Mat(),keyImg2, descImg2,false); - // Match method loop - for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){ + // Match method loop + for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){ descriptorMatcher = DescriptorMatcher::create(*itMatcher); descriptorMatcher->match(descImg1, descImg2, matches, Mat()); - // Keep best matches only to have a nice drawing + // Keep best matches only to have a nice drawing. + // We sort distance between descriptor matches Mat index; int nbMatch=int(matches.size()); Mat tab(nbMatch, 1, CV_32F); for (int i = 0; i(i, 0) = matches[i].distance; + } sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING); vector bestMatches; for (int i = 0; i<30; i++) + { bestMatches.push_back(matches[index.at(i, 0)]); + } Mat result; drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result); namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE); @@ -71,19 +115,45 @@ int main(void) FileStorage fs(*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE); fs<<"Matches"<::iterator it; - cout << "Index \tIndex \tindex \tdistance\n"; - cout << "in img1\tin img2\timage\t\n"; - for (it = matches.begin(); it != matches.end(); it++) - cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->imgIdx << "\t" << it->distance<<"\n"; + cout<<"**********Match results**********\n"; + cout << "Index \tIndex \tdistance\n"; + cout << "in img1\tin img2\n"; + double cumSumDist2=0; // Use to compute distance between keyPoint matches and to evaluate match algorithm + for (it = bestMatches.begin(); it != bestMatches.end(); it++) + { + cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->distance << "\n"; + Point2d p=keyImg1[it->queryIdx].pt-keyImg2[it->trainIdx].pt; + cumSumDist2=p.x*p.x+p.y*p.y; + } + desMethCmp.push_back(cumSumDist2); waitKey(); } } - catch (Exception& e){ + catch (Exception& e) + { cout << "Feature : " << *itDesc << "\n"; if (itMatcher != typeAlgoMatch.end()) + { cout << "Matcher : " << *itMatcher << "\n"; + } cout<::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++) + { + cout<<*itMatcher<<"\t"; + } + cout << "\n"; + for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++) + { + cout << *itDesc << "\t"; + for (vector::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++, i++) + { + cout << desMethCmp[i]<<"\t"; + } + cout<<"\n"; + } return 0; } From 09930938e8e45416f8ce0cb607505908e027db65 Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Mon, 4 May 2015 10:59:43 +0200 Subject: [PATCH 07/12] Trailing white space first step --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index be6bf3553..9ec434269 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -21,7 +21,7 @@ int main(int argc, char *argv[]) vector typeAlgoMatch; vector fileName; help(); - // This descriptor are going to be detect and compute + // This descriptor are going to be detect and compute typeDesc.push_back("AKAZE"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html typeDesc.push_back("ORB"); // see http://docs.opencv.org/trunk/de/dbf/classcv_1_1BRISK.html typeDesc.push_back("BRISK"); // see http://docs.opencv.org/trunk/db/d95/classcv_1_1ORB.html @@ -67,11 +67,11 @@ int main(int argc, char *argv[]) { Ptr descriptorMatcher; // Match between img1 and img2 - vector matches; - // keypoint for img1 and img2 + vector matches; + // keypoint for img1 and img2 vector keyImg1, keyImg2; - // Descriptor for img1 and img2 - Mat descImg1, descImg2; + // Descriptor for img1 and img2 + Mat descImg1, descImg2; vector::iterator itMatcher = typeAlgoMatch.end(); if (*itDesc == "AKAZE"){ b = AKAZE::create(); From cfcef3ec980df34e88875904834069a239e8fff2 Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Wed, 6 May 2015 19:55:16 +0200 Subject: [PATCH 08/12] File was not saved due to wrong file name. Add a comment about bug 4308 --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index 9ec434269..500f8c412 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -21,6 +21,7 @@ int main(int argc, char *argv[]) vector typeAlgoMatch; vector fileName; help(); + system("cd"); // This descriptor are going to be detect and compute typeDesc.push_back("AKAZE"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html typeDesc.push_back("ORB"); // see http://docs.opencv.org/trunk/de/dbf/classcv_1_1BRISK.html @@ -112,13 +113,15 @@ int main(int argc, char *argv[]) drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result); namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE); imshow(*itDesc + ": " + *itMatcher, result); - FileStorage fs(*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE); + // Saved result could be wrong due to bug 4308 + FileStorage fs(*itDesc + "_" + *itMatcher + ".yml", FileStorage::WRITE); fs<<"Matches"<::iterator it; cout<<"**********Match results**********\n"; cout << "Index \tIndex \tdistance\n"; cout << "in img1\tin img2\n"; - double cumSumDist2=0; // Use to compute distance between keyPoint matches and to evaluate match algorithm + // Use to compute distance between keyPoint matches and to evaluate match algorithm + double cumSumDist2=0; for (it = bestMatches.begin(); it != bestMatches.end(); it++) { cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->distance << "\n"; From 966d50762dcf05e786238fb67cc90f637a49a0bf Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Thu, 7 May 2015 22:48:13 +0200 Subject: [PATCH 09/12] Try to include comment from @eduardo and @berak Akaze descriptor with DESCRIPTOR_KAZE_UPRIGHT added --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 89 ++++++++++++--------- 1 file changed, 53 insertions(+), 36 deletions(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index 500f8c412..072b1d678 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -21,8 +21,8 @@ int main(int argc, char *argv[]) vector typeAlgoMatch; vector fileName; help(); - system("cd"); // This descriptor are going to be detect and compute + typeDesc.push_back("AKAZE-DESCRIPTOR_KAZE_UPRIGHT"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html typeDesc.push_back("AKAZE"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html typeDesc.push_back("ORB"); // see http://docs.opencv.org/trunk/de/dbf/classcv_1_1BRISK.html typeDesc.push_back("BRISK"); // see http://docs.opencv.org/trunk/db/d95/classcv_1_1ORB.html @@ -74,9 +74,12 @@ int main(int argc, char *argv[]) // Descriptor for img1 and img2 Mat descImg1, descImg2; vector::iterator itMatcher = typeAlgoMatch.end(); + if (*itDesc == "AKAZE-DESCRIPTOR_KAZE_UPRIGHT"){ + b = AKAZE::create(AKAZE::DESCRIPTOR_KAZE_UPRIGHT); + } if (*itDesc == "AKAZE"){ b = AKAZE::create(); - } + } if (*itDesc == "ORB"){ b = ORB::create(); } @@ -93,44 +96,57 @@ int main(int argc, char *argv[]) // Match method loop for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){ descriptorMatcher = DescriptorMatcher::create(*itMatcher); - descriptorMatcher->match(descImg1, descImg2, matches, Mat()); - // Keep best matches only to have a nice drawing. - // We sort distance between descriptor matches - Mat index; - int nbMatch=int(matches.size()); - Mat tab(nbMatch, 1, CV_32F); - for (int i = 0; idescriptorType() == CV_32F || b->defaultNorm() <= NORM_L2SQR) ) { - tab.at(i, 0) = matches[i].distance; + cout << "**************************************************************************\n"; + cout << "It's strange. You should use Hamming distance only for a binary descriptor\n"; + cout << "**************************************************************************\n"; } - sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING); - vector bestMatches; - for (int i = 0; i<30; i++) + try { - bestMatches.push_back(matches[index.at(i, 0)]); + descriptorMatcher->match(descImg1, descImg2, matches, Mat()); + // Keep best matches only to have a nice drawing. + // We sort distance between descriptor matches + Mat index; + int nbMatch=int(matches.size()); + Mat tab(nbMatch, 1, CV_32F); + for (int i = 0; i(i, 0) = matches[i].distance; + } + sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING); + vector bestMatches; + for (int i = 0; i<30; i++) + { + bestMatches.push_back(matches[index.at(i, 0)]); + } + Mat result; + drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result); + namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE); + imshow(*itDesc + ": " + *itMatcher, result); + // Saved result could be wrong due to bug 4308 + FileStorage fs(*itDesc + "_" + *itMatcher + ".yml", FileStorage::WRITE); + fs<<"Matches"<::iterator it; + cout<<"**********Match results**********\n"; + cout << "Index \tIndex \tdistance\n"; + cout << "in img1\tin img2\n"; + // Use to compute distance between keyPoint matches and to evaluate match algorithm + double cumSumDist2=0; + for (it = bestMatches.begin(); it != bestMatches.end(); it++) + { + cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->distance << "\n"; + Point2d p=keyImg1[it->queryIdx].pt-keyImg2[it->trainIdx].pt; + cumSumDist2=p.x*p.x+p.y*p.y; + } + desMethCmp.push_back(cumSumDist2); + waitKey(); } - Mat result; - drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result); - namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE); - imshow(*itDesc + ": " + *itMatcher, result); - // Saved result could be wrong due to bug 4308 - FileStorage fs(*itDesc + "_" + *itMatcher + ".yml", FileStorage::WRITE); - fs<<"Matches"<::iterator it; - cout<<"**********Match results**********\n"; - cout << "Index \tIndex \tdistance\n"; - cout << "in img1\tin img2\n"; - // Use to compute distance between keyPoint matches and to evaluate match algorithm - double cumSumDist2=0; - for (it = bestMatches.begin(); it != bestMatches.end(); it++) - { - cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->distance << "\n"; - Point2d p=keyImg1[it->queryIdx].pt-keyImg2[it->trainIdx].pt; - cumSumDist2=p.x*p.x+p.y*p.y; + catch (Exception& e) + { + desMethCmp.push_back(-1); + } } - desMethCmp.push_back(cumSumDist2); - waitKey(); - } } catch (Exception& e) { @@ -139,11 +155,12 @@ int main(int argc, char *argv[]) { cout << "Matcher : " << *itMatcher << "\n"; } - cout<::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++) { cout<<*itMatcher<<"\t"; From f6f5b6035b6d0961e580246938f07cf4458d4095 Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Thu, 7 May 2015 22:55:38 +0200 Subject: [PATCH 10/12] trailing whitespace --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index 072b1d678..b14abfe51 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -102,7 +102,7 @@ int main(int argc, char *argv[]) cout << "It's strange. You should use Hamming distance only for a binary descriptor\n"; cout << "**************************************************************************\n"; } - try + try { descriptorMatcher->match(descImg1, descImg2, matches, Mat()); // Keep best matches only to have a nice drawing. From 26d7da68fc81e9c881c5a27e39f50606a00a453a Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Fri, 8 May 2015 09:57:05 +0200 Subject: [PATCH 11/12] warning C4101: 'e' line 146 --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index b14abfe51..7b595b50b 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -86,7 +86,8 @@ int main(int argc, char *argv[]) else if (*itDesc == "BRISK"){ b = BRISK::create(); } - try { + try + { // We can detect keypoint with detect method b->detect(img1, keyImg1, Mat()); // and compute their descriptors with method compute @@ -144,6 +145,8 @@ int main(int argc, char *argv[]) } catch (Exception& e) { + cout << e.msg << endl; + cout << "Cumulative distance cannot be computed." << endl; desMethCmp.push_back(-1); } } From 16e20e5c73d2ab6ba26fea315c7f96bd1efa6daa Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Sat, 9 May 2015 10:29:53 +0200 Subject: [PATCH 12/12] An another improvement? --- samples/cpp/matchmethod_orb_akaze_brisk.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index 7b595b50b..4e3884d69 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -97,12 +97,18 @@ int main(int argc, char *argv[]) // Match method loop for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){ descriptorMatcher = DescriptorMatcher::create(*itMatcher); - if ((*itMatcher == "BruteForce-Hamming" || *itMatcher == "BruteForce-Hamming(2)") && (b->descriptorType() == CV_32F || b->defaultNorm() <= NORM_L2SQR) ) + if ((*itMatcher == "BruteForce-Hamming" || *itMatcher == "BruteForce-Hamming(2)") && (b->descriptorType() == CV_32F || b->defaultNorm() <= NORM_L2SQR)) { cout << "**************************************************************************\n"; cout << "It's strange. You should use Hamming distance only for a binary descriptor\n"; cout << "**************************************************************************\n"; } + if ((*itMatcher == "BruteForce" || *itMatcher == "BruteForce-L1") && (b->defaultNorm() >= NORM_HAMMING)) + { + cout << "**************************************************************************\n"; + cout << "It's strange. You shouldn't use L1 or L2 distance for a binary descriptor\n"; + cout << "**************************************************************************\n"; + } try { descriptorMatcher->match(descImg1, descImg2, matches, Mat());