1 #include "opencv2/highgui/highgui.hpp" 2 #include "opencv2/imgproc/imgproc.hpp" 3 #include <iostream> 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 using namespace cv; 8 using namespace std; 9 10 /// Global variables 11 Mat src, src_gray; 12 13 int maxCorners = 10; 14 int maxTrackbar = 25; 15 16 RNG rng(12345); 17 char* source_window = "Image"; 18 19 /// Function header 20 void goodFeaturesToTrack_Demo( int, void* ); 21 22 /** @function main */ 23 int main( int argc, char** argv ) 24 { 25 /// Load source image and convert it to gray 26 src = imread( argv[1], 1 ); 27 cvtColor( src, src_gray, CV_BGR2GRAY ); 28 29 /// Create Window 30 namedWindow( source_window, CV_WINDOW_AUTOSIZE ); 31 32 /// Create Trackbar to set the number of corners 33 createTrackbar( "Max corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo); 34 35 imshow( source_window, src ); 36 37 goodFeaturesToTrack_Demo( 0, 0 ); 38 39 waitKey(0); 40 return(0); 41 } 42 43 /** 44 * @function goodFeaturesToTrack_Demo.cpp 45 * @brief Apply Shi-Tomasi corner detector 46 */ 47 void goodFeaturesToTrack_Demo( int, void* ) 48 { 49 if( maxCorners < 1 ) { maxCorners = 1; } 50 51 /// Parameters for Shi-Tomasi algorithm 52 vector<Point2f> corners; 53 double qualityLevel = 0.01; 54 double minDistance = 10; 55 int blockSize = 3; 56 bool useHarrisDetector = false; 57 double k = 0.04; 58 59 /// Copy the source image 60 Mat copy; 61 copy = src.clone(); 62 63 /// Apply corner detection 64 goodFeaturesToTrack( src_gray, 65 corners, 66 maxCorners, 67 qualityLevel, 68 minDistance, 69 Mat(), 70 blockSize, 71 useHarrisDetector, 72 k ); 73 74 75 /// Draw corners detected 76 cout<<"** Number of corners detected: "<<corners.size()<<endl; 77 int r = 4; 78 for( int i = 0; i < corners.size(); i++ ) 79 { circle( copy, corners[i], r, Scalar(rng.uniform(0,255), rng.uniform(0,255), 80 rng.uniform(0,255)), -1, 8, 0 ); } 81 82 /// Show what you got 83 namedWindow( source_window, CV_WINDOW_AUTOSIZE ); 84 imshow( source_window, copy ); 85 86 /// Set the neeed parameters to find the refined corners 87 Size winSize = Size( 5, 5 ); 88 Size zeroZone = Size( -1, -1 ); 89 TermCriteria criteria = TermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 40, 0.001 ); 90 91 /// Calculate the refined corner locations 92 cornerSubPix( src_gray, corners, winSize, zeroZone, criteria ); 93 94 /// Write them down 95 for( int i = 0; i < corners.size(); i++ ) 96 { cout<<" -- Refined Corner ["<<i<<"] ("<<corners[i].x<<","<<corners[i].y<<")"<<endl; } 97 }