Home | History | Annotate | Download | only in feature_detection
      1 Feature Detection {#tutorial_feature_detection}
      2 =================
      3 
      4 Goal
      5 ----
      6 
      7 In this tutorial you will learn how to:
      8 
      9 -   Use the @ref cv::FeatureDetector interface in order to find interest points. Specifically:
     10     -   Use the cv::xfeatures2d::SURF and its function cv::xfeatures2d::SURF::detect to perform the
     11         detection process
     12     -   Use the function @ref cv::drawKeypoints to draw the detected keypoints
     13 
     14 Theory
     15 ------
     16 
     17 Code
     18 ----
     19 
     20 This tutorial code's is shown lines below.
     21 @code{.cpp}
     22 #include <stdio.h>
     23 #include <iostream>
     24 #include "opencv2/core.hpp"
     25 #include "opencv2/features2d.hpp"
     26 #include "opencv2/xfeatures2d.hpp"
     27 #include "opencv2/highgui.hpp"
     28 
     29 using namespace cv;
     30 using namespace cv::xfeatures2d;
     31 
     32 void readme();
     33 
     34 /* @function main */
     35 int main( int argc, char** argv )
     36 {
     37   if( argc != 3 )
     38   { readme(); return -1; }
     39 
     40   Mat img_1 = imread( argv[1], IMREAD_GRAYSCALE );
     41   Mat img_2 = imread( argv[2], IMREAD_GRAYSCALE );
     42 
     43   if( !img_1.data || !img_2.data )
     44   { std::cout<< " --(!) Error reading images " << std::endl; return -1; }
     45 
     46   //-- Step 1: Detect the keypoints using SURF Detector
     47   int minHessian = 400;
     48 
     49   Ptr<SURF> detector = SURF::create( minHessian );
     50 
     51   std::vector<KeyPoint> keypoints_1, keypoints_2;
     52 
     53   detector->detect( img_1, keypoints_1 );
     54   detector->detect( img_2, keypoints_2 );
     55 
     56   //-- Draw keypoints
     57   Mat img_keypoints_1; Mat img_keypoints_2;
     58 
     59   drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
     60   drawKeypoints( img_2, keypoints_2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
     61 
     62   //-- Show detected (drawn) keypoints
     63   imshow("Keypoints 1", img_keypoints_1 );
     64   imshow("Keypoints 2", img_keypoints_2 );
     65 
     66   waitKey(0);
     67 
     68   return 0;
     69   }
     70 
     71   /* @function readme */
     72   void readme()
     73   { std::cout << " Usage: ./SURF_detector <img1> <img2>" << std::endl; }
     74 @endcode
     75 
     76 Explanation
     77 -----------
     78 
     79 Result
     80 ------
     81 
     82 -#  Here is the result of the feature detection applied to the first image:
     83 
     84     ![](images/Feature_Detection_Result_a.jpg)
     85 
     86 -#  And here is the result for the second image:
     87 
     88     ![](images/Feature_Detection_Result_b.jpg)
     89