1 .. _feature_homography:
3 Features2D + Homography to find a known object
4 **********************************************
9 In this tutorial you will learn how to:
11 .. container:: enumeratevisibleitemswithsquare
13 * Use the function :find_homography:`findHomography<>` to find the transform between matched keypoints.
14 * Use the function :perspective_transform:`perspectiveTransform<>` to map the points.
23 This tutorial code's is shown lines below. You can also download it from `here <http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/cpp/tutorial_code/features2D/SURF_Homography.cpp>`_
29 #include "opencv2/core/core.hpp"
30 #include "opencv2/features2d/features2d.hpp"
31 #include "opencv2/highgui/highgui.hpp"
32 #include "opencv2/calib3d/calib3d.hpp"
39 int main( int argc, char** argv )
42 { readme(); return -1; }
44 Mat img_object = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
45 Mat img_scene = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );
47 if( !img_object.data || !img_scene.data )
48 { std::cout<< " --(!) Error reading images " << std::endl; return -1; }
50 //-- Step 1: Detect the keypoints using SURF Detector
53 SurfFeatureDetector detector( minHessian );
55 std::vector<KeyPoint> keypoints_object, keypoints_scene;
57 detector.detect( img_object, keypoints_object );
58 detector.detect( img_scene, keypoints_scene );
60 //-- Step 2: Calculate descriptors (feature vectors)
61 SurfDescriptorExtractor extractor;
63 Mat descriptors_object, descriptors_scene;
65 extractor.compute( img_object, keypoints_object, descriptors_object );
66 extractor.compute( img_scene, keypoints_scene, descriptors_scene );
68 //-- Step 3: Matching descriptor vectors using FLANN matcher
69 FlannBasedMatcher matcher;
70 std::vector< DMatch > matches;
71 matcher.match( descriptors_object, descriptors_scene, matches );
73 double max_dist = 0; double min_dist = 100;
75 //-- Quick calculation of max and min distances between keypoints
76 for( int i = 0; i < descriptors_object.rows; i++ )
77 { double dist = matches[i].distance;
78 if( dist < min_dist ) min_dist = dist;
79 if( dist > max_dist ) max_dist = dist;
82 printf("-- Max dist : %f \n", max_dist );
83 printf("-- Min dist : %f \n", min_dist );
85 //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
86 std::vector< DMatch > good_matches;
88 for( int i = 0; i < descriptors_object.rows; i++ )
89 { if( matches[i].distance < 3*min_dist )
90 { good_matches.push_back( matches[i]); }
94 drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
95 good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
96 vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
98 //-- Localize the object
99 std::vector<Point2f> obj;
100 std::vector<Point2f> scene;
102 for( int i = 0; i < good_matches.size(); i++ )
104 //-- Get the keypoints from the good matches
105 obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
106 scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
109 Mat H = findHomography( obj, scene, CV_RANSAC );
111 //-- Get the corners from the image_1 ( the object to be "detected" )
112 std::vector<Point2f> obj_corners(4);
113 obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
114 obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
115 std::vector<Point2f> scene_corners(4);
117 perspectiveTransform( obj_corners, scene_corners, H);
119 //-- Draw lines between the corners (the mapped object in the scene - image_2 )
120 line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
121 line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
122 line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
123 line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
125 //-- Show detected matches
126 imshow( "Good Matches & Object detection", img_matches );
132 /** @function readme */
134 { std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl; }
143 #. And here is the result for the detected object (highlighted in green)
145 .. image:: images/Feature_Homography_Result.jpg