From 39660003a272105f9b21259e2936b65a9f105377 Mon Sep 17 00:00:00 2001 From: matlabbe Date: Mon, 29 Oct 2012 20:50:56 +0000 Subject: [PATCH] Example: added hard-coded switch to choose between BFMatcher and FLANN matching. git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@180 620bd6b2-0a58-f614-fd9a-1bd335dccda9 --- example/main.cpp | 96 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 27 deletions(-) diff --git a/example/main.cpp b/example/main.cpp index 599c50bd..7ef90c3b 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -95,35 +95,55 @@ int main(int argc, char * argv[]) //////////////////////////// cv::Mat results; cv::Mat dists; + std::vector > matches; + bool isBinaryDescriptors; int k=2; // find the 2 nearest neighbors + bool useBFMatcher = false; // SET TO TRUE TO USE BRUTE FORCE MATCHER (may give better results with binary descriptors) if(objectDescriptors.type()==CV_8U) { - // Binary descriptors detected (from ORB or Brief) + // Binary descriptors detected (from ORB, Brief, BRISK, FREAK) + printf("Binary descriptors detected...\n"); + isBinaryDescriptors = true; + if(useBFMatcher) + { + cv::BFMatcher matcher(cv::NORM_HAMMING); + matcher.knnMatch(objectDescriptors, sceneDescriptors, matches, k); + } + else + { + // Create Flann LSH index + cv::flann::Index flannIndex(sceneDescriptors, cv::flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING); + printf("Time creating FLANN LSH index = %d ms\n", time.restart()); + results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index + dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1 ?!?!? NOTE OpenCV doc is not clear about that... - // Create Flann LSH index - cv::flann::Index flannIndex(sceneDescriptors, cv::flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING); - printf("Time creating FLANN LSH index = %d ms\n", time.restart()); - results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index - dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1 ?!?!? NOTE OpenCV doc is not clear about that... - - // search (nearest neighbor) - flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() ); - printf("Time nearest neighbor search = %d ms\n", time.restart()); + // search (nearest neighbor) + flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() ); + } } else { // assume it is CV_32F + printf("Float descriptors detected...\n"); + isBinaryDescriptors = false; + if(useBFMatcher) + { + cv::BFMatcher matcher(cv::NORM_L2); + matcher.knnMatch(objectDescriptors, sceneDescriptors, matches, k); + } + else + { + // Create Flann KDTree index + cv::flann::Index flannIndex(sceneDescriptors, cv::flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN); + printf("Time creating FLANN KDTree index = %d ms\n", time.restart()); + results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index + dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1 - // Create Flann KDTree index - cv::flann::Index flannIndex(sceneDescriptors, cv::flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN); - printf("Time creating FLANN KDTree index = %d ms\n", time.restart()); - results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index - dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1 - - // search (nearest neighbor) - flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() ); - printf("Time nearest neighbor search = %d ms\n", time.restart()); + // search (nearest neighbor) + flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() ); + } } + printf("Time nearest neighbor search = %d ms\n", time.restart()); @@ -141,17 +161,39 @@ int main(int argc, char * argv[]) std::vector mpts_1, mpts_2; // Used for homography std::vector indexes_1, indexes_2; // Used for homography std::vector outlier_mask; // Used for homography - for(int i=0; i(i,0) <= nndrRatio * dists.at(i,1)) + for(int i=0; i(i,0), dists.at(i,1)); + if(isBinaryDescriptors || //Binary, just take the nearest + dists.at(i,0) <= nndrRatio * dists.at(i,1)) + { + mpts_1.push_back(objectKeypoints.at(i).pt); + indexes_1.push_back(i); - mpts_2.push_back(sceneKeypoints.at(results.at(i,0)).pt); - indexes_2.push_back(results.at(i,0)); + mpts_2.push_back(sceneKeypoints.at(results.at(i,0)).pt); + indexes_2.push_back(results.at(i,0)); + } + } + } + else + { + for(unsigned int i=0; i