diff --git a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp index da75026e4e..7338d7f4a2 100644 --- a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp +++ b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp @@ -7,49 +7,101 @@ #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include -#include using namespace cv; -/** - * @function main - */ -int main(int, char** argv) +namespace { - Mat src, src_gray; + // windows and trackbars name + const std::string windowName = "Hough Circle Detection Demo"; + const std::string cannyThresholdTrackbarName = "Canny threshold"; + const std::string accumulatorThresholdTrackbarName = "Accumulator Threshold"; + const std::string usage = "Usage : tutorial_HoughCircle_Demo \n"; - /// Read the image - src = imread( argv[1], 1 ); + // initial and max values of the parameters of interests. + const int cannyThresholdInitialValue = 200; + const int accumulatorThresholdInitialValue = 50; + const int maxAccumulatorThreshold = 200; + const int maxCannyThreshold = 255; - if( !src.data ) - { return -1; } - - /// Convert it to gray - cvtColor( src, src_gray, COLOR_BGR2GRAY ); - - /// Reduce the noise so we avoid false circle detection - GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 ); - - vector circles; - - /// Apply the Hough Transform to find the circles - HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 ); - - /// Draw the circles detected - for( size_t i = 0; i < circles.size(); i++ ) + void HoughDetection(const Mat& src_gray, const Mat& src_display, int cannyThreshold, int accumulatorThreshold) { - Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); - int radius = cvRound(circles[i][2]); - // circle center - circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 ); - // circle outline - circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 ); + // will hold the results of the detection + std::vector circles; + // runs the actual detection + HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, cannyThreshold, accumulatorThreshold, 0, 0 ); + + // clone the colour, input image for displaying purposes + Mat display = src_display.clone(); + for( size_t i = 0; i < circles.size(); i++ ) + { + Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); + int radius = cvRound(circles[i][2]); + // circle center + circle( display, center, 3, Scalar(0,255,0), -1, 8, 0 ); + // circle outline + circle( display, center, radius, Scalar(0,0,255), 3, 8, 0 ); + } + + // shows the results + imshow( windowName, display); + } +} + + +int main(int argc, char** argv) +{ + Mat src, src_gray; + + if (argc < 2) + { + std::cerr<<"No input image specified\n"; + std::cout<