2011-07-12 05:08:35 +08:00
|
|
|
/**
|
|
|
|
* @function goodFeaturesToTrack_Demo.cpp
|
|
|
|
* @brief Demo code for detecting corners using Shi-Tomasi method
|
|
|
|
* @author OpenCV team
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/// Global variables
|
|
|
|
Mat src, src_gray;
|
|
|
|
|
|
|
|
int maxCorners = 23;
|
|
|
|
int maxTrackbar = 100;
|
|
|
|
|
|
|
|
RNG rng(12345);
|
2012-11-07 22:21:20 +08:00
|
|
|
const char* source_window = "Image";
|
2011-07-12 05:08:35 +08:00
|
|
|
|
|
|
|
/// Function header
|
|
|
|
void goodFeaturesToTrack_Demo( int, void* );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
2012-11-07 22:21:20 +08:00
|
|
|
int main( int, char** argv )
|
2011-07-12 05:08:35 +08:00
|
|
|
{
|
|
|
|
/// Load source image and convert it to gray
|
|
|
|
src = imread( argv[1], 1 );
|
2013-10-30 20:34:27 +08:00
|
|
|
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
2011-07-12 05:08:35 +08:00
|
|
|
|
|
|
|
/// Create Window
|
2013-10-30 20:34:27 +08:00
|
|
|
namedWindow( source_window, WINDOW_AUTOSIZE );
|
2011-07-12 05:08:35 +08:00
|
|
|
|
|
|
|
/// Create Trackbar to set the number of corners
|
2012-10-17 07:18:30 +08:00
|
|
|
createTrackbar( "Max corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo );
|
2011-07-12 05:08:35 +08:00
|
|
|
|
|
|
|
imshow( source_window, src );
|
|
|
|
|
|
|
|
goodFeaturesToTrack_Demo( 0, 0 );
|
|
|
|
|
|
|
|
waitKey(0);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function goodFeaturesToTrack_Demo.cpp
|
|
|
|
* @brief Apply Shi-Tomasi corner detector
|
|
|
|
*/
|
|
|
|
void goodFeaturesToTrack_Demo( int, void* )
|
|
|
|
{
|
|
|
|
if( maxCorners < 1 ) { maxCorners = 1; }
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-07-12 05:08:35 +08:00
|
|
|
/// Parameters for Shi-Tomasi algorithm
|
|
|
|
vector<Point2f> corners;
|
|
|
|
double qualityLevel = 0.01;
|
|
|
|
double minDistance = 10;
|
|
|
|
int blockSize = 3;
|
|
|
|
bool useHarrisDetector = false;
|
|
|
|
double k = 0.04;
|
|
|
|
|
|
|
|
/// Copy the source image
|
|
|
|
Mat copy;
|
|
|
|
copy = src.clone();
|
|
|
|
|
|
|
|
/// Apply corner detection
|
2012-10-17 07:18:30 +08:00
|
|
|
goodFeaturesToTrack( src_gray,
|
|
|
|
corners,
|
|
|
|
maxCorners,
|
|
|
|
qualityLevel,
|
|
|
|
minDistance,
|
|
|
|
Mat(),
|
|
|
|
blockSize,
|
|
|
|
useHarrisDetector,
|
|
|
|
k );
|
|
|
|
|
2011-07-12 05:08:35 +08:00
|
|
|
|
|
|
|
/// Draw corners detected
|
|
|
|
cout<<"** Number of corners detected: "<<corners.size()<<endl;
|
|
|
|
int r = 4;
|
2012-11-07 22:21:20 +08:00
|
|
|
for( size_t i = 0; i < corners.size(); i++ )
|
2011-07-12 05:08:35 +08:00
|
|
|
{ circle( copy, corners[i], r, Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255)), -1, 8, 0 ); }
|
|
|
|
|
|
|
|
/// Show what you got
|
2013-10-30 20:34:27 +08:00
|
|
|
namedWindow( source_window, WINDOW_AUTOSIZE );
|
2012-10-17 07:18:30 +08:00
|
|
|
imshow( source_window, copy );
|
2011-07-12 05:08:35 +08:00
|
|
|
}
|