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
|
|
|
|
*/
|
|
|
|
|
2014-07-04 22:48:15 +08:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2016-02-15 21:37:29 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2011-07-12 05:08:35 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2017-12-18 18:44:11 +08:00
|
|
|
int main( int argc, char** argv )
|
2011-07-12 05:08:35 +08:00
|
|
|
{
|
2018-05-28 07:33:56 +08:00
|
|
|
/// Load source image and convert it to gray
|
|
|
|
CommandLineParser parser( argc, argv, "{@input | ../data/pic3.png | input image}" );
|
|
|
|
src = imread( parser.get<String>( "@input" ) );
|
|
|
|
if( src.empty() )
|
|
|
|
{
|
|
|
|
cout << "Could not open or find the image!\n" << endl;
|
|
|
|
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
2011-07-12 05:08:35 +08:00
|
|
|
|
2018-05-28 07:33:56 +08:00
|
|
|
/// Create Window
|
|
|
|
namedWindow( source_window );
|
2011-07-12 05:08:35 +08:00
|
|
|
|
2018-05-28 07:33:56 +08:00
|
|
|
/// Create Trackbar to set the number of corners
|
|
|
|
createTrackbar( "Max corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo );
|
2011-07-12 05:08:35 +08:00
|
|
|
|
2018-05-28 07:33:56 +08:00
|
|
|
imshow( source_window, src );
|
2011-07-12 05:08:35 +08:00
|
|
|
|
2018-05-28 07:33:56 +08:00
|
|
|
goodFeaturesToTrack_Demo( 0, 0 );
|
2011-07-12 05:08:35 +08:00
|
|
|
|
2018-05-28 07:33:56 +08:00
|
|
|
waitKey();
|
|
|
|
return 0;
|
2011-07-12 05:08:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function goodFeaturesToTrack_Demo.cpp
|
|
|
|
* @brief Apply Shi-Tomasi corner detector
|
|
|
|
*/
|
|
|
|
void goodFeaturesToTrack_Demo( int, void* )
|
|
|
|
{
|
2018-05-28 07:33:56 +08:00
|
|
|
/// Parameters for Shi-Tomasi algorithm
|
|
|
|
maxCorners = MAX(maxCorners, 1);
|
|
|
|
vector<Point2f> corners;
|
|
|
|
double qualityLevel = 0.01;
|
|
|
|
double minDistance = 10;
|
|
|
|
int blockSize = 3, gradientSize = 3;
|
|
|
|
bool useHarrisDetector = false;
|
|
|
|
double k = 0.04;
|
|
|
|
|
|
|
|
/// Copy the source image
|
|
|
|
Mat copy = src.clone();
|
|
|
|
|
|
|
|
/// Apply corner detection
|
|
|
|
goodFeaturesToTrack( src_gray,
|
|
|
|
corners,
|
|
|
|
maxCorners,
|
|
|
|
qualityLevel,
|
|
|
|
minDistance,
|
|
|
|
Mat(),
|
|
|
|
blockSize,
|
|
|
|
gradientSize,
|
|
|
|
useHarrisDetector,
|
|
|
|
k );
|
|
|
|
|
|
|
|
|
|
|
|
/// Draw corners detected
|
|
|
|
cout << "** Number of corners detected: " << corners.size() << endl;
|
|
|
|
int radius = 4;
|
|
|
|
for( size_t i = 0; i < corners.size(); i++ )
|
|
|
|
{
|
|
|
|
circle( copy, corners[i], radius, Scalar(rng.uniform(0,255), rng.uniform(0, 256), rng.uniform(0, 256)), FILLED );
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Show what you got
|
|
|
|
namedWindow( source_window );
|
|
|
|
imshow( source_window, copy );
|
2011-07-12 05:08:35 +08:00
|
|
|
}
|