CLAHE Python bindings
[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
34    using namespace cv;
35
36    void readme();
37
38    /** @function main */
39    int main( int argc, char** argv )
40    {
41      if( argc != 3 )
42      { readme(); return -1; }
43
44      Mat img_object = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
45      Mat img_scene = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );
46
47      if( !img_object.data || !img_scene.data )
48      { std::cout<< " --(!) Error reading images " << std::endl; return -1; }
49
50      //-- Step 1: Detect the keypoints using SURF Detector
51      int minHessian = 400;
52
53      SurfFeatureDetector detector( minHessian );
54
55      std::vector<KeyPoint> keypoints_object, keypoints_scene;
56
57      detector.detect( img_object, keypoints_object );
58      detector.detect( img_scene, keypoints_scene );
59
60      //-- Step 2: Calculate descriptors (feature vectors)
61      SurfDescriptorExtractor extractor;
62
63      Mat descriptors_object, descriptors_scene;
64
65      extractor.compute( img_object, keypoints_object, descriptors_object );
66      extractor.compute( img_scene, keypoints_scene, descriptors_scene );
67
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 );
72
73      double max_dist = 0; double min_dist = 100;
74
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;
80      }
81
82      printf("-- Max dist : %f \n", max_dist );
83      printf("-- Min dist : %f \n", min_dist );
84
85      //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
86      std::vector< DMatch > good_matches;
87
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]); }
91      }
92
93      Mat img_matches;
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 );
97
98      //-- Localize the object
99      std::vector<Point2f> obj;
100      std::vector<Point2f> scene;
101
102      for( int i = 0; i < good_matches.size(); i++ )
103      {
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 );
107      }
108
109      Mat H = findHomography( obj, scene, CV_RANSAC );
110
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);
116
117      perspectiveTransform( obj_corners, scene_corners, H);
118
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 );
124
125      //-- Show detected matches
126      imshow( "Good Matches & Object detection", img_matches );
127
128      waitKey(0);
129      return 0;
130      }
131
132      /** @function readme */
133      void readme()
134      { std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl; }
135
136 Explanation
137 ============
138
139 Result
140 ======
141
142
143 #. And here is the result for the detected object (highlighted in green)
144
145    .. image:: images/Feature_Homography_Result.jpg
146       :align: center
147       :height: 200pt
148