How to match ORB descriptors with flann LSH?

asked 2013-03-29 07:31:37 -0600

yangyangcv gravatar image

updated 2013-03-29 07:42:35 -0600

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);

    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

edit retag flag offensive close merge delete


at which line of your code, the error gets generated?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-29 07:34:27 -0600 )edit

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.

yangyangcv gravatar imageyangyangcv ( 2013-03-29 07:44:48 -0600 )edit
sammy gravatar imagesammy ( 2013-03-29 08:35:06 -0600 )edit

@sammy: i see that post. but the method there doesn't work for me :(

yangyangcv gravatar imageyangyangcv ( 2013-03-30 19:30:37 -0600 )edit

Then you should also check whether ORB is a binary descriptor. LSH is for binary descriptors.

sammy gravatar imagesammy ( 2013-03-31 06:39:23 -0600 )edit

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.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-31 09:50:53 -0600 )edit