一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

 

 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 = 23;
14 int maxTrackbar = 100;
15 
16 RNG rng(12345);
17 char* source_window = "Image";
18 
19 /// Function header
20 void goodFeaturesToTrack_Demo( int, void* );
21 
22 /**
23  * @function main
24  */
25 int main( int argc, char** argv )
26 {
27   /// Load source image and convert it to gray
28   src = imread( argv[1], 1 );
29   cvtColor( src, src_gray, CV_BGR2GRAY );
30 
31   /// Create Window
32   namedWindow( source_window, CV_WINDOW_AUTOSIZE );
33 
34   /// Create Trackbar to set the number of corners
35   createTrackbar( "Max  corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo );
36 
37   imshow( source_window, src );
38 
39   goodFeaturesToTrack_Demo( 0, 0 );
40 
41   waitKey(0);
42   return(0);
43 }
44 
45 /**
46  * @function goodFeaturesToTrack_Demo.cpp
47  * @brief Apply Shi-Tomasi corner detector
48  */
49 void goodFeaturesToTrack_Demo( int, void* )
50 {
51   if( maxCorners < 1 ) { maxCorners = 1; }
52 
53   /// Parameters for Shi-Tomasi algorithm
54   vector<Point2f> corners;
55   double qualityLevel = 0.01;
56   double minDistance = 10;
57   int blockSize = 3;
58   bool useHarrisDetector = false;
59   double k = 0.04;
60 
61   /// Copy the source image
62   Mat copy;
63   copy = src.clone();
64 
65   /// Apply corner detection
66   goodFeaturesToTrack( src_gray,
67                corners,
68                maxCorners,
69                qualityLevel,
70                minDistance,
71                Mat(),
72                blockSize,
73                useHarrisDetector,
74                k );
75 
76 
77   /// Draw corners detected
78   cout<<"** Number of corners detected: "<<corners.size()<<endl;
79   int r = 4;
80   for( int i = 0; i < corners.size(); i++ )
81      { circle( copy, corners[i], r, Scalar(rng.uniform(0,255), rng.uniform(0,255),
82               rng.uniform(0,255)), -1, 8, 0 ); }
83 
84   /// Show what you got
85   namedWindow( source_window, CV_WINDOW_AUTOSIZE );
86   imshow( source_window, copy );
87 }
posted on 2020-01-09 13:28  一杯清酒邀明月  阅读(203)  评论(0编辑  收藏  举报