Home | History | Annotate | Download | only in TrackingMotion
      1 /**
      2  * @function cornerDetector_Demo.cpp
      3  * @brief Demo code for detecting corners using OpenCV built-in functions
      4  * @author OpenCV team
      5  */
      6 #include "opencv2/imgcodecs.hpp"
      7 #include "opencv2/highgui/highgui.hpp"
      8 #include "opencv2/imgproc/imgproc.hpp"
      9 #include <iostream>
     10 #include <stdio.h>
     11 #include <stdlib.h>
     12 
     13 using namespace cv;
     14 using namespace std;
     15 
     16 /// Global variables
     17 Mat src, src_gray;
     18 Mat myHarris_dst; Mat myHarris_copy; Mat Mc;
     19 Mat myShiTomasi_dst; Mat myShiTomasi_copy;
     20 
     21 int myShiTomasi_qualityLevel = 50;
     22 int myHarris_qualityLevel = 50;
     23 int max_qualityLevel = 100;
     24 
     25 double myHarris_minVal; double myHarris_maxVal;
     26 double myShiTomasi_minVal; double myShiTomasi_maxVal;
     27 
     28 RNG rng(12345);
     29 
     30 const char* myHarris_window = "My Harris corner detector";
     31 const char* myShiTomasi_window = "My Shi Tomasi corner detector";
     32 
     33 /// Function headers
     34 void myShiTomasi_function( int, void* );
     35 void myHarris_function( int, void* );
     36 
     37 /**
     38  * @function main
     39  */
     40 int main( int, char** argv )
     41 {
     42   /// Load source image and convert it to gray
     43   src = imread( argv[1], 1 );
     44   cvtColor( src, src_gray, COLOR_BGR2GRAY );
     45 
     46   /// Set some parameters
     47   int blockSize = 3; int apertureSize = 3;
     48 
     49   /// My Harris matrix -- Using cornerEigenValsAndVecs
     50   myHarris_dst = Mat::zeros( src_gray.size(), CV_32FC(6) );
     51   Mc = Mat::zeros( src_gray.size(), CV_32FC1 );
     52 
     53   cornerEigenValsAndVecs( src_gray, myHarris_dst, blockSize, apertureSize, BORDER_DEFAULT );
     54 
     55   /* calculate Mc */
     56   for( int j = 0; j < src_gray.rows; j++ )
     57      { for( int i = 0; i < src_gray.cols; i++ )
     58           {
     59             float lambda_1 = myHarris_dst.at<Vec6f>(j, i)[0];
     60             float lambda_2 = myHarris_dst.at<Vec6f>(j, i)[1];
     61             Mc.at<float>(j,i) = lambda_1*lambda_2 - 0.04f*pow( ( lambda_1 + lambda_2 ), 2 );
     62           }
     63      }
     64 
     65   minMaxLoc( Mc, &myHarris_minVal, &myHarris_maxVal, 0, 0, Mat() );
     66 
     67   /* Create Window and Trackbar */
     68   namedWindow( myHarris_window, WINDOW_AUTOSIZE );
     69   createTrackbar( " Quality Level:", myHarris_window, &myHarris_qualityLevel, max_qualityLevel, myHarris_function );
     70   myHarris_function( 0, 0 );
     71 
     72   /// My Shi-Tomasi -- Using cornerMinEigenVal
     73   myShiTomasi_dst = Mat::zeros( src_gray.size(), CV_32FC1 );
     74   cornerMinEigenVal( src_gray, myShiTomasi_dst, blockSize, apertureSize, BORDER_DEFAULT );
     75 
     76   minMaxLoc( myShiTomasi_dst, &myShiTomasi_minVal, &myShiTomasi_maxVal, 0, 0, Mat() );
     77 
     78   /* Create Window and Trackbar */
     79   namedWindow( myShiTomasi_window, WINDOW_AUTOSIZE );
     80   createTrackbar( " Quality Level:", myShiTomasi_window, &myShiTomasi_qualityLevel, max_qualityLevel, myShiTomasi_function );
     81   myShiTomasi_function( 0, 0 );
     82 
     83   waitKey(0);
     84   return(0);
     85 }
     86 
     87 /**
     88  * @function myShiTomasi_function
     89  */
     90 void myShiTomasi_function( int, void* )
     91 {
     92   myShiTomasi_copy = src.clone();
     93 
     94   if( myShiTomasi_qualityLevel < 1 ) { myShiTomasi_qualityLevel = 1; }
     95 
     96   for( int j = 0; j < src_gray.rows; j++ )
     97      { for( int i = 0; i < src_gray.cols; i++ )
     98           {
     99             if( myShiTomasi_dst.at<float>(j,i) > myShiTomasi_minVal + ( myShiTomasi_maxVal - myShiTomasi_minVal )*myShiTomasi_qualityLevel/max_qualityLevel )
    100               { circle( myShiTomasi_copy, Point(i,j), 4, Scalar( rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255) ), -1, 8, 0 ); }
    101           }
    102      }
    103   imshow( myShiTomasi_window, myShiTomasi_copy );
    104 }
    105 
    106 /**
    107  * @function myHarris_function
    108  */
    109 void myHarris_function( int, void* )
    110 {
    111   myHarris_copy = src.clone();
    112 
    113   if( myHarris_qualityLevel < 1 ) { myHarris_qualityLevel = 1; }
    114 
    115   for( int j = 0; j < src_gray.rows; j++ )
    116      { for( int i = 0; i < src_gray.cols; i++ )
    117           {
    118             if( Mc.at<float>(j,i) > myHarris_minVal + ( myHarris_maxVal - myHarris_minVal )*myHarris_qualityLevel/max_qualityLevel )
    119               { circle( myHarris_copy, Point(i,j), 4, Scalar( rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255) ), -1, 8, 0 ); }
    120           }
    121      }
    122   imshow( myHarris_window, myHarris_copy );
    123 }
    124