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