C++ - OpenCV feature detection with ORB

YaronGh picture YaronGh · Jul 11, 2016 · Viewed 23.9k times · Source

I'm trying to extract and match features with OpenCV using ORB for detecting and FLANN for matching, and i get a really weird result. After loading my 2 images and converting them to grayscale, here's my code:

// Initiate ORB detector
    Ptr<FeatureDetector> detector = ORB::create();

// find the keypoints and descriptors with ORB
    detector->detect(gray_image1, keypoints_object);
    detector->detect(gray_image2, keypoints_scene);

    Ptr<DescriptorExtractor> extractor = ORB::create();
    extractor->compute(gray_image1, keypoints_object, descriptors_object );
    extractor->compute(gray_image2, keypoints_scene, descriptors_scene );

// Flann needs the descriptors to be of type CV_32F
    descriptors_scene.convertTo(descriptors_scene, CV_32F);
    descriptors_object.convertTo(descriptors_object, CV_32F);

    FlannBasedMatcher matcher;
    vector<DMatch> matches;
    matcher.match( descriptors_object, descriptors_scene, matches );

    double max_dist = 0; double min_dist = 100;

    //-- Quick calculation of max and min distances between keypoints
    for( int i = 0; i < descriptors_object.rows; i++ )
    {
        double dist = matches[i].distance;
        if( dist < min_dist ) min_dist = dist;
        if( dist > max_dist ) max_dist = dist;
    }

    //-- Use only "good" matches (i.e. whose distance is less than 3*min_dist )
    vector< DMatch > good_matches;

    for( int i = 0; i < descriptors_object.rows; i++ )
    {
        if( matches[i].distance < 3*min_dist )
        {
            good_matches.push_back( matches[i]);
        }
    }


    vector< Point2f > obj;
    vector< Point2f > scene;


    for( int i = 0; i < good_matches.size(); i++ )
    {
        //-- Get the keypoints from the good matches
        obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
        scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
    }

    // Find the Homography Matrix
    Mat H = findHomography( obj, scene, CV_RANSAC );
    // Use the Homography Matrix to warp the images
    cv::Mat result;
    warpPerspective(image1,result,H,Size(image1.cols+image2.cols,image1.rows));
    cv::Mat half(result,cv::Rect(0,0,image2.cols,image2.rows));
    image2.copyTo(half);
    imshow( "Result", result );

And this is a screen shot of the weird result i'm getting: screen shot

What might be the problem?

Thanks!

Answer

PhilLab picture PhilLab · Jul 11, 2016

You are experiencing the results of a bad matching: The homography which fits the data is not "realistic" and thus distorts the image.

You can debug your matching with imshow( "Good Matches", img_matches ); as done in the example.

There are multiple approaches to improve your matches:

  1. Use the crossCheck option
  2. Use the SIFT ratio test
  3. Use the OutputArray mask in cv::findHompgraphy to identify totally wrong homography computations
  4. ... and so on...