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 int thresh = 200; 13 int max_thresh = 255; 14 15 char* source_window = "Source image"; 16 char* corners_window = "Corners detected"; 17 18 /// Function header 19 void cornerHarris_demo( int, void* ); 20 21 /** @function main */ 22 int main( int argc, char** argv ) 23 { 24 /// Load source image and convert it to gray 25 src = imread( argv[1], 1 ); 26 cvtColor( src, src_gray, CV_BGR2GRAY ); 27 28 /// Create a window and a trackbar 29 namedWindow( source_window, CV_WINDOW_AUTOSIZE ); 30 createTrackbar( "Threshold: ", source_window, &thresh, max_thresh, cornerHarris_demo ); 31 imshow( source_window, src ); 32 33 cornerHarris_demo( 0, 0 ); 34 35 waitKey(0); 36 return(0); 37 } 38 39 /** @function cornerHarris_demo */ 40 void cornerHarris_demo( int, void* ) 41 { 42 43 Mat dst, dst_norm, dst_norm_scaled; 44 dst = Mat::zeros( src.size(), CV_32FC1 ); 45 46 /// Detector parameters 47 int blockSize = 2; 48 int apertureSize = 3; 49 double k = 0.04; 50 51 /// Detecting corners 52 cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT ); 53 54 /// Normalizing 55 normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() ); 56 convertScaleAbs( dst_norm, dst_norm_scaled ); 57 58 /// Drawing a circle around corners 59 for( int j = 0; j < dst_norm.rows ; j++ ) 60 { for( int i = 0; i < dst_norm.cols; i++ ) 61 { 62 if( (int) dst_norm.at<float>(j,i) > thresh ) 63 { 64 circle( dst_norm_scaled, Point( i, j ), 5, Scalar(0), 2, 8, 0 ); 65 } 66 } 67 } 68 /// Showing the result 69 namedWindow( corners_window, CV_WINDOW_AUTOSIZE ); 70 imshow( corners_window, dst_norm_scaled ); 71 }