I'm trying to match SURF points between 2 images using SURF with opencv. One is the rotation of the other. The problem is that the few matching it can found are wrong.
I mainly took my code from opencv http://docs.opencv.org/doc/tutorials/features2d/feature_flann_matcher/feature_flann_matcher.html#feature-flann-matcher
Here is the code:
string imageName1="test_right_rotate.jpg";
string imageName2="test_right.jpg";
Mat image1 = imread( imageName1, 1 );
Mat image2 = imread( imageName2, 1 );
int minHessian = 500;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1, keypoints_2;
detector->detect( image1, keypoints_1 );
detector->detect( image2, keypoints_2 );
SurfDescriptorExtractor extractor;
Mat descriptors_1, descriptors_2;
extractor.compute( image1, keypoints_1, descriptors_1 );
extractor.compute( image2, keypoints_2, descriptors_2 );
FlannBasedMatcher matcher;
std::vector<vector<DMatch > > matches;
matcher.knnMatch(descriptors_1,descriptors_2, matches, 2);
std::vector< DMatch > good_matches;
for(int i = 0; i < min(descriptors_1.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
{
if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
{
good_matches.push_back(matches[i][0]);
}
}
Mat img_matches;
drawMatches( image1, keypoints_1, image2, keypoints_2, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
It works well for scaled images and translated images but fails for rotated images.
EDIT:
I don't know what is wrong but instead of using SURF, I used ORB as detector and FREAK as extractor and it works a lot better. You can find the code on this SO : https://stackoverflow.com/questions/12491022/opencv-freak-fast-retina-keypoint-descriptor