This tutorial code's is shown lines below.
#include <stdio.h>
#include <iostream>
#include "opencv2/xfeatures2d.hpp"
using namespace cv::xfeatures2d;
void readme();
int main( int argc, char** argv )
{
if( argc != 3 )
{ readme(); return -1; }
if( !img_object.
data || !img_scene.
data )
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }
int minHessian = 400;
Ptr<SURF> detector = SURF::create( minHessian );
std::vector<KeyPoint> keypoints_object, keypoints_scene;
Mat descriptors_object, descriptors_scene;
detector->detectAndCompute( img_object,
Mat(), keypoints_object, descriptors_object );
detector->detectAndCompute( img_scene,
Mat(), keypoints_scene, descriptors_scene );
std::vector< DMatch > matches;
matcher.
match( descriptors_object, descriptors_scene, matches );
double max_dist = 0; double min_dist = 100;
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;
}
printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );
std::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]); }
}
drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for( size_t i = 0; i < good_matches.size(); i++ )
{
obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
}
std::vector<Point2f> obj_corners(4);
std::vector<Point2f> scene_corners(4);
imshow(
"Good Matches & Object detection", img_matches );
return 0;
}
void readme()
{ std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl; }