How to apply RANSAC on SURF, SIFT and ORB matching results

Haider picture Haider · Apr 14, 2013 · Viewed 26.6k times · Source

I'm working on image processing. I want to match 2D Features and I did many tests on SURF, SIFT, ORB.
How can I apply RANSAC on SURF/SIFT/ORB in OpenCV?

Answer

Danny picture Danny · Apr 15, 2013

OpenCV has the function cv::findHomography which can optionally use RANSAC to find the homography matrix relating two images. You can see an example of this function in action here.

Specifically the section of code you are interested in is:

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

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

Mat H = findHomography( obj, scene, CV_RANSAC ); 

You can then use the function cv::perspectiveTransform to warp the images according to the homography matrix.

Other options for cv::findHomography other than CV_RANSAC are 0 which uses every point and CV_LMEDS which uses the Least-Median method. More info can be found in the OpenCV camera calibration documentation here.