Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / doc / tutorials / features2d / feature_homography / feature_homography.rst
1 .. _feature_homography:
2
3 Features2D + Homography to find a known object
4 **********************************************
5
6 Goal
7 =====
8
9 In this tutorial you will learn how to:
10
11 .. container:: enumeratevisibleitemswithsquare
12
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.
15
16
17 Theory
18 ======
19
20 Code
21 ====
22
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>`_
24
25 .. code-block:: cpp
26
27    #include <stdio.h>
28    #include <iostream>
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"
34
35    using namespace cv;
36
37    void readme();
38
39    /** @function main */
40    int main( int argc, char** argv )
41    {
42      if( argc != 3 )
43      { readme(); return -1; }
44
45      Mat img_object = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
46      Mat img_scene = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );
47
48      if( !img_object.data || !img_scene.data )
49      { std::cout<< " --(!) Error reading images " << std::endl; return -1; }
50
51      //-- Step 1: Detect the keypoints using SURF Detector
52      int minHessian = 400;
53
54      SurfFeatureDetector detector( minHessian );
55
56      std::vector<KeyPoint> keypoints_object, keypoints_scene;
57
58      detector.detect( img_object, keypoints_object );
59      detector.detect( img_scene, keypoints_scene );
60
61      //-- Step 2: Calculate descriptors (feature vectors)
62      SurfDescriptorExtractor extractor;
63
64      Mat descriptors_object, descriptors_scene;
65
66      extractor.compute( img_object, keypoints_object, descriptors_object );
67      extractor.compute( img_scene, keypoints_scene, descriptors_scene );
68
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 );
73
74      double max_dist = 0; double min_dist = 100;
75
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;
81      }
82
83      printf("-- Max dist : %f \n", max_dist );
84      printf("-- Min dist : %f \n", min_dist );
85
86      //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
87      std::vector< DMatch > good_matches;
88
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]); }
92      }
93
94      Mat img_matches;
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 );
98
99      //-- Localize the object
100      std::vector<Point2f> obj;
101      std::vector<Point2f> scene;
102
103      for( int i = 0; i < good_matches.size(); i++ )
104      {
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 );
108      }
109
110      Mat H = findHomography( obj, scene, CV_RANSAC );
111
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);
117
118      perspectiveTransform( obj_corners, scene_corners, H);
119
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 );
125
126      //-- Show detected matches
127      imshow( "Good Matches & Object detection", img_matches );
128
129      waitKey(0);
130      return 0;
131      }
132
133      /** @function readme */
134      void readme()
135      { std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl; }
136
137 Explanation
138 ============
139
140 Result
141 ======
142
143
144 #. And here is the result for the detected object (highlighted in green)
145
146    .. image:: images/Feature_Homography_Result.jpg
147       :align: center
148       :height: 200pt
149