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
This commit is contained in:
matlabbe 2012-10-29 20:50:56 +00:00
parent 72bdae12b0
commit 39660003a2

View File

@ -95,35 +95,55 @@ int main(int argc, char * argv[])
//////////////////////////// ////////////////////////////
cv::Mat results; cv::Mat results;
cv::Mat dists; cv::Mat dists;
std::vector<std::vector<cv::DMatch> > matches;
bool isBinaryDescriptors;
int k=2; // find the 2 nearest neighbors 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) 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 // search (nearest neighbor)
cv::flann::Index flannIndex(sceneDescriptors, cv::flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING); flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() );
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());
} }
else else
{ {
// assume it is CV_32F // 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 // search (nearest neighbor)
cv::flann::Index flannIndex(sceneDescriptors, cv::flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN); flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() );
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());
} }
printf("Time nearest neighbor search = %d ms\n", time.restart());
@ -141,17 +161,39 @@ int main(int argc, char * argv[])
std::vector<cv::Point2f> mpts_1, mpts_2; // Used for homography std::vector<cv::Point2f> mpts_1, mpts_2; // Used for homography
std::vector<int> indexes_1, indexes_2; // Used for homography std::vector<int> indexes_1, indexes_2; // Used for homography
std::vector<uchar> outlier_mask; // Used for homography std::vector<uchar> outlier_mask; // Used for homography
for(int i=0; i<objectDescriptors.rows; ++i) // Check if this descriptor matches with those of the objects
if(!useBFMatcher)
{ {
// Check if this descriptor matches with those of the objects for(int i=0; i<objectDescriptors.rows; ++i)
// Apply NNDR
if(dists.at<float>(i,0) <= nndrRatio * dists.at<float>(i,1))
{ {
mpts_1.push_back(objectKeypoints.at(i).pt); // Apply NNDR
indexes_1.push_back(i); //printf("q=%d dist1=%f dist2=%f\n", i, dists.at<float>(i,0), dists.at<float>(i,1));
if(isBinaryDescriptors || //Binary, just take the nearest
dists.at<float>(i,0) <= nndrRatio * dists.at<float>(i,1))
{
mpts_1.push_back(objectKeypoints.at(i).pt);
indexes_1.push_back(i);
mpts_2.push_back(sceneKeypoints.at(results.at<int>(i,0)).pt); mpts_2.push_back(sceneKeypoints.at(results.at<int>(i,0)).pt);
indexes_2.push_back(results.at<int>(i,0)); indexes_2.push_back(results.at<int>(i,0));
}
}
}
else
{
for(unsigned int i=0; i<matches.size(); ++i)
{
// Apply NNDR
//printf("q=%d dist1=%f dist2=%f\n", matches.at(i).at(0).queryIdx, matches.at(i).at(0).distance, matches.at(i).at(1).distance);
if(isBinaryDescriptors || //Binary, just take the nearest
matches.at(i).at(0).distance <= nndrRatio * matches.at(i).at(1).distance)
{
mpts_1.push_back(objectKeypoints.at(matches.at(i).at(0).queryIdx).pt);
indexes_1.push_back(matches.at(i).at(0).queryIdx);
mpts_2.push_back(sceneKeypoints.at(matches.at(i).at(0).trainIdx).pt);
indexes_2.push_back(matches.at(i).at(0).trainIdx);
}
} }
} }