1
Detecting corners location in subpixeles {#tutorial_corner_subpixeles}
2
========================================
7
In this tutorial you will learn how to:
9
- Use the OpenCV function @ref cv::cornerSubPix to find more exact corner positions (more exact
18
This tutorial code's is shown lines below. You can also download it from
19
[here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/TrackingMotion/cornerSubPix_Demo.cpp)
21
#include "opencv2/highgui.hpp"
22
#include "opencv2/imgproc.hpp"
37
char* source_window = "Image";
40
void goodFeaturesToTrack_Demo( int, void* );
43
int main( int argc, char** argv )
45
/// Load source image and convert it to gray
46
src = imread( argv[1], 1 );
47
cvtColor( src, src_gray, COLOR_BGR2GRAY );
50
namedWindow( source_window, WINDOW_AUTOSIZE );
52
/// Create Trackbar to set the number of corners
53
createTrackbar( "Max corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo);
55
imshow( source_window, src );
57
goodFeaturesToTrack_Demo( 0, 0 );
64
* @function goodFeaturesToTrack_Demo.cpp
65
* @brief Apply Shi-Tomasi corner detector
67
void goodFeaturesToTrack_Demo( int, void* )
69
if( maxCorners < 1 ) { maxCorners = 1; }
71
/// Parameters for Shi-Tomasi algorithm
72
vector<Point2f> corners;
73
double qualityLevel = 0.01;
74
double minDistance = 10;
76
bool useHarrisDetector = false;
79
/// Copy the source image
83
/// Apply corner detection
84
goodFeaturesToTrack( src_gray,
95
/// Draw corners detected
96
cout<<"** Number of corners detected: "<<corners.size()<<endl;
98
for( int i = 0; i < corners.size(); i++ )
99
{ circle( copy, corners[i], r, Scalar(rng.uniform(0,255), rng.uniform(0,255),
100
rng.uniform(0,255)), -1, 8, 0 ); }
102
/// Show what you got
103
namedWindow( source_window, WINDOW_AUTOSIZE );
104
imshow( source_window, copy );
106
/// Set the neeed parameters to find the refined corners
107
Size winSize = Size( 5, 5 );
108
Size zeroZone = Size( -1, -1 );
109
TermCriteria criteria = TermCriteria( TermCriteria::EPS + TermCriteria::MAX_ITER, 40, 0.001 );
111
/// Calculate the refined corner locations
112
cornerSubPix( src_gray, corners, winSize, zeroZone, criteria );
115
for( int i = 0; i < corners.size(); i++ )
116
{ cout<<" -- Refined Corner ["<<i<<"] ("<<corners[i].x<<","<<corners[i].y<<")"<<endl; }
125
![](images/Corner_Subpixeles_Original_Image.jpg)
129
![](images/Corner_Subpixeles_Result.jpg)