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"
33 #include "opencv2/nonfree/nonfree.hpp"
40 int main( int argc, char** argv )
43 { readme(); return -1; }
45 Mat img_object = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
46 Mat img_scene = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );
48 if( !img_object.data || !img_scene.data )
49 { std::cout<< " --(!) Error reading images " << std::endl; return -1; }
51 //-- Step 1: Detect the keypoints using SURF Detector
54 SurfFeatureDetector detector( minHessian );
56 std::vector<KeyPoint> keypoints_object, keypoints_scene;
58 detector.detect( img_object, keypoints_object );
59 detector.detect( img_scene, keypoints_scene );
61 //-- Step 2: Calculate descriptors (feature vectors)
62 SurfDescriptorExtractor extractor;
64 Mat descriptors_object, descriptors_scene;
66 extractor.compute( img_object, keypoints_object, descriptors_object );
67 extractor.compute( img_scene, keypoints_scene, descriptors_scene );
69 //-- Step 3: Matching descriptor vectors using FLANN matcher
70 FlannBasedMatcher matcher;
71 std::vector< DMatch > matches;
72 matcher.match( descriptors_object, descriptors_scene, matches );
74 double max_dist = 0; double min_dist = 100;
76 //-- Quick calculation of max and min distances between keypoints
77 for( int i = 0; i < descriptors_object.rows; i++ )
78 { double dist = matches[i].distance;
79 if( dist < min_dist ) min_dist = dist;
80 if( dist > max_dist ) max_dist = dist;
83 printf("-- Max dist : %f \n", max_dist );
84 printf("-- Min dist : %f \n", min_dist );
86 //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
87 std::vector< DMatch > good_matches;
89 for( int i = 0; i < descriptors_object.rows; i++ )
90 { if( matches[i].distance < 3*min_dist )
91 { good_matches.push_back( matches[i]); }
95 drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
96 good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
97 vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
99 //-- Localize the object
100 std::vector<Point2f> obj;
101 std::vector<Point2f> scene;
103 for( int i = 0; i < good_matches.size(); i++ )
105 //-- Get the keypoints from the good matches
106 obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
107 scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
110 Mat H = findHomography( obj, scene, CV_RANSAC );
112 //-- Get the corners from the image_1 ( the object to be "detected" )
113 std::vector<Point2f> obj_corners(4);
114 obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
115 obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
116 std::vector<Point2f> scene_corners(4);
118 perspectiveTransform( obj_corners, scene_corners, H);
120 //-- Draw lines between the corners (the mapped object in the scene - image_2 )
121 line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
122 line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
123 line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
124 line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
126 //-- Show detected matches
127 imshow( "Good Matches & Object detection", img_matches );
133 /** @function readme */
135 { std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl; }
144 #. And here is the result for the detected object (highlighted in green)
146 .. image:: images/Feature_Homography_Result.jpg