Home | History | Annotate | Download | only in TrackingMotion
      1 /**
      2  * @function cornerHarris_Demo.cpp
      3  * @brief Demo code for detecting corners using Harris-Stephens method
      4  * @author OpenCV team
      5  */
      6 
      7 #include "opencv2/imgcodecs.hpp"
      8 #include "opencv2/highgui/highgui.hpp"
      9 #include "opencv2/imgproc/imgproc.hpp"
     10 #include <iostream>
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 
     14 using namespace cv;
     15 using namespace std;
     16 
     17 /// Global variables
     18 Mat src, src_gray;
     19 int thresh = 200;
     20 int max_thresh = 255;
     21 
     22 const char* source_window = "Source image";
     23 const char* corners_window = "Corners detected";
     24 
     25 /// Function header
     26 void cornerHarris_demo( int, void* );
     27 
     28 /**
     29  * @function main
     30  */
     31 int main( int, char** argv )
     32 {
     33   /// Load source image and convert it to gray
     34   src = imread( argv[1], 1 );
     35   cvtColor( src, src_gray, COLOR_BGR2GRAY );
     36 
     37   /// Create a window and a trackbar
     38   namedWindow( source_window, WINDOW_AUTOSIZE );
     39   createTrackbar( "Threshold: ", source_window, &thresh, max_thresh, cornerHarris_demo );
     40   imshow( source_window, src );
     41 
     42   cornerHarris_demo( 0, 0 );
     43 
     44   waitKey(0);
     45   return(0);
     46 }
     47 
     48 /**
     49  * @function cornerHarris_demo
     50  * @brief Executes the corner detection and draw a circle around the possible corners
     51  */
     52 void cornerHarris_demo( int, void* )
     53 {
     54 
     55   Mat dst, dst_norm, dst_norm_scaled;
     56   dst = Mat::zeros( src.size(), CV_32FC1 );
     57 
     58   /// Detector parameters
     59   int blockSize = 2;
     60   int apertureSize = 3;
     61   double k = 0.04;
     62 
     63   /// Detecting corners
     64   cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );
     65 
     66   /// Normalizing
     67   normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
     68   convertScaleAbs( dst_norm, dst_norm_scaled );
     69 
     70   /// Drawing a circle around corners
     71   for( int j = 0; j < dst_norm.rows ; j++ )
     72      { for( int i = 0; i < dst_norm.cols; i++ )
     73           {
     74             if( (int) dst_norm.at<float>(j,i) > thresh )
     75               {
     76                circle( dst_norm_scaled, Point( i, j ), 5,  Scalar(0), 2, 8, 0 );
     77               }
     78           }
     79      }
     80   /// Showing the result
     81   namedWindow( corners_window, WINDOW_AUTOSIZE );
     82   imshow( corners_window, dst_norm_scaled );
     83 }
     84