How to match ORB descriptors with flann LSH?
I want to match ORB descriptors extracted from two images with the code below:
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
int main(int argc, char** argv)
{
cv::Mat img1 = cv::imread("rgb0.jpg", CV_LOAD_IMAGE_GRAYSCALE);//CV_LOAD_IMAGE_GRAYSCALE
cv::Mat img2 = cv::imread("rgb1.jpg", CV_LOAD_IMAGE_GRAYSCALE);//CV_LOAD_IMAGE_COLOR
if(img1.empty() || img2.empty())
{
std::cout<<"Can't read one of the images"<<std::endl;
return -1;
}
cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create("ORB");
cv::Ptr<cv::DescriptorExtractor> descriptor = cv::DescriptorExtractor::create("ORB" );
std::vector<cv::KeyPoint> keypoints1, keypoints2;
detector->detect(img1, keypoints1);
std::cout<<"find "<<keypoints1.size()<<" keypoints in img1"<<std::endl;
detector->detect(img2, keypoints2);
std::cout<<"find "<<keypoints2.size()<<" keypoints in img2"<<std::endl;
cv::Mat descriptors1, descriptors2;
descriptor->compute(img1, keypoints1, descriptors1);
descriptor->compute(img2, keypoints2, descriptors2);
cv::Ptr<cv::DescriptorMatcher> matcher_;
//matcher_ = new cv::BFMatcher(cv::NORM_HAMMING);//this works correctly
matcher_ = new cv::FlannBasedMatcher(new cv::flann::LshIndexParams(5, 24, 2));
//matcher_ = new cv::FlannBasedMatcher();
std::vector<cv::DMatch> matches;
matcher_->match(descriptors1, descriptors2, matches);
std::cout<<"Find "<<matches.size()<<" matches"<<std::endl;
cv::namedWindow("matches", 1);
cv::Mat img_matches;
cv::drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
cv::imshow("matches", img_matches);
cv::waitKey(0);
return 0;
}
but there is compile error saying: terminate called after throwing an instance of 'cvflann::anyimpl::bad_any_cast'. In this post i see that people say this is a bug and the fix is given. However, the fix does not work for me. In fact, the fix is a small modification about lsh_index.h, but according to my understanding, this file is not used at all. "opencv2/features2d/features2d.hpp" includes "opencv2/flann/miniflann.hpp", but i see no inclusion about lsh_index.h in that file.
btw, i'm using opencv2.4.2
at which line of your code, the error gets generated?
thanks for the quick response Steven. i just edited my post. I think the error is with flann matcher. The BFMatcher works fine for me.
It's probably this one http://answers.opencv.org/question/503/how-to-use-the-lshindexparams/
@sammy: i see that post. but the method there doesn't work for me :(
Then you should also check whether ORB is a binary descriptor. LSH is for binary descriptors.
ORB is not a pure binary descriptor, but based on the binary descriptor BRIEF, hence I cannot assure it will work with LSH. Use for example the BRIEF or FREAK descriptor which is in fact a pure binary descriptor.