From 3e25c085bac83586113a3f1247c3bc2ea865cfdf Mon Sep 17 00:00:00 2001 From: laurentBerger Date: Sun, 3 May 2015 11:42:27 +0200 Subject: [PATCH] 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 0000000000..708c129647 --- /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<