Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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::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.

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::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

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