From 6c9f4d6423f2234f4bc2556fc65fc6cbc86105f5 Mon Sep 17 00:00:00 2001 From: Ana Huaman Date: Mon, 11 Jul 2011 21:08:35 +0000 Subject: [PATCH] Added goodFeaturesToTrack_Demo.cpp code for tutorials --- .../goodFeaturesToTrack_Demo.cpp | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 samples/cpp/tutorial_code/TrackingMotion/goodFeaturesToTrack_Demo.cpp diff --git a/samples/cpp/tutorial_code/TrackingMotion/goodFeaturesToTrack_Demo.cpp b/samples/cpp/tutorial_code/TrackingMotion/goodFeaturesToTrack_Demo.cpp new file mode 100644 index 0000000000..247d678098 --- /dev/null +++ b/samples/cpp/tutorial_code/TrackingMotion/goodFeaturesToTrack_Demo.cpp @@ -0,0 +1,93 @@ +/** + * @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 +#include +#include + +using namespace cv; +using namespace std; + +/// Global variables +Mat src, src_gray; + +int maxCorners = 23; +int maxTrackbar = 100; + +RNG rng(12345); +char* source_window = "Image"; + +/// Function header +void goodFeaturesToTrack_Demo( int, void* ); + +/** + * @function main + */ +int main( int argc, char** argv ) +{ + /// Load source image and convert it to gray + src = imread( argv[1], 1 ); + cvtColor( src, src_gray, CV_BGR2GRAY ); + + /// Create Window + namedWindow( source_window, CV_WINDOW_AUTOSIZE ); + + /// Create Trackbar to set the number of corners + createTrackbar( "Max corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo ); + + 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; } + + /// Parameters for Shi-Tomasi algorithm + vector 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 + goodFeaturesToTrack( src_gray, + corners, + maxCorners, + qualityLevel, + minDistance, + Mat(), + blockSize, + useHarrisDetector, + k ); + + + /// Draw corners detected + cout<<"** Number of corners detected: "<