From 8a2de2eccae4dde31a70ef4b0e86e6282157fa52 Mon Sep 17 00:00:00 2001 From: Adrien BAK Date: Mon, 11 Nov 2013 14:47:54 +0900 Subject: [PATCH 1/6] modified HoughCircleDemo to introduce variable params via trackbars Conflicts: samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp --- .../ImgTrans/HoughCircle_Demo.cpp | 89 +++++++++++++------ 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp index da75026e4e..0790d903cd 100644 --- a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp +++ b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp @@ -6,50 +6,89 @@ #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" -#include -#include using namespace cv; -/** - * @function main - */ +namespace +{ + // windows and trackbars name + const std::string windowName = "Hough Circle Detection Demo"; + const std::string cannyThresholdTrackbarName = "Canny threshold"; + const std::string accumulatorThresholdTrackbarName = "Accumulator Threshold"; + + // 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; + + void HoughDetection(const Mat& src_gray, const Mat& src_display, int cannyThreshold, int accumulatorThreshold) + { + // will hold the results of the detection + std::vector circles; + // runs the actual detection + HoughCircles( src_gray, circles, 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, char** argv) { Mat src, src_gray; - /// Read the image - src = imread( argv[1], 1 ); + // Read the image + src = imread( argv[1], 1 ); if( !src.data ) { return -1; } - /// Convert it to gray + // Convert it to gray cvtColor( src, src_gray, COLOR_BGR2GRAY ); - /// Reduce the noise so we avoid false circle detection + // Reduce the noise so we avoid false circle detection GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 ); - vector circles; + //declare and initialize both parameters that are subjects to change + int cannyThreshold = cannyThresholdInitialValue; + int accumulatorThreshold = accumulatorThresholdInitialValue; - /// Apply the Hough Transform to find the circles - HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 ); + // create the main window, and attach the trackbars + namedWindow( windowName, WINDOW_AUTOSIZE ); + createTrackbar(cannyThresholdTrackbarName, windowName, &cannyThreshold,maxCannyThreshold); + createTrackbar(accumulatorThresholdTrackbarName, windowName, &accumulatorThreshold, maxAccumulatorThreshold); - /// Draw the circles detected - for( size_t i = 0; i < circles.size(); i++ ) + // inifinite loop to display + // and refresh the content of the output image + // unti the user presses q or Q + int key = 0; + while(key != 'q' && key != 'Q') { - 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 ); + // those paramaters cannot be =0 + // so we must check here + cannyThreshold = std::max(cannyThreshold, 1); + accumulatorThreshold = std::max(accumulatorThreshold, 1); + + //runs the detection, and update the display + HoughDetection(src_gray, src, cannyThreshold, accumulatorThreshold); + + // get user key + key = waitKey(10); } - /// Show your results - namedWindow( "Hough Circle Transform Demo", WINDOW_AUTOSIZE ); - imshow( "Hough Circle Transform Demo", src ); - - waitKey(0); return 0; } From 514f1fae9548e35c4005b4f2d2b4fad7a550bc28 Mon Sep 17 00:00:00 2001 From: Adrien BAK Date: Mon, 11 Nov 2013 14:55:12 +0900 Subject: [PATCH 2/6] improved error handling --- samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp index 0790d903cd..0522e1892f 100644 --- a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp +++ b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp @@ -6,6 +6,7 @@ #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" +#include using namespace cv; @@ -55,7 +56,11 @@ int main(int, char** argv) src = imread( argv[1], 1 ); if( !src.data ) - { return -1; } + { + std::cerr<<"Invalid input image\n"; + std::cout<<"Usage : tutorial_HoughCircle_Demo \n"; + return -1; + } // Convert it to gray cvtColor( src, src_gray, COLOR_BGR2GRAY ); From 1c025be06869e1a23c6739ea82d3b56dfe070400 Mon Sep 17 00:00:00 2001 From: Adrien BAK Date: Wed, 13 Nov 2013 16:13:47 +0900 Subject: [PATCH 3/6] fix typos --- samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp index 0522e1892f..ee512414d2 100644 --- a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp +++ b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp @@ -77,9 +77,9 @@ int main(int, char** argv) createTrackbar(cannyThresholdTrackbarName, windowName, &cannyThreshold,maxCannyThreshold); createTrackbar(accumulatorThresholdTrackbarName, windowName, &accumulatorThreshold, maxAccumulatorThreshold); - // inifinite loop to display + // infinite loop to display // and refresh the content of the output image - // unti the user presses q or Q + // until the user presses q or Q int key = 0; while(key != 'q' && key != 'Q') { From dda3b534ada60f44a451c136a434b7b3cd1d40ae Mon Sep 17 00:00:00 2001 From: Adrien BAK Date: Wed, 13 Nov 2013 16:21:09 +0900 Subject: [PATCH 4/6] fix indent --- .../ImgTrans/HoughCircle_Demo.cpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp index ee512414d2..5cfcbd34bf 100644 --- a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp +++ b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp @@ -25,42 +25,42 @@ namespace void HoughDetection(const Mat& src_gray, const Mat& src_display, int cannyThreshold, int accumulatorThreshold) { - // will hold the results of the detection - std::vector circles; - // runs the actual detection - HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, src_gray.rows/8, cannyThreshold, accumulatorThreshold, 0, 0 ); + // will hold the results of the detection + std::vector circles; + // runs the actual detection + HoughCircles( src_gray, circles, 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++ ) - { + // 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); + // shows the results + imshow( windowName, display); } } int main(int, char** argv) { - Mat src, src_gray; + Mat src, src_gray; // Read the image src = imread( argv[1], 1 ); - if( !src.data ) - { - std::cerr<<"Invalid input image\n"; - std::cout<<"Usage : tutorial_HoughCircle_Demo \n"; - return -1; - } + if( !src.data ) + { + std::cerr<<"Invalid input image\n"; + std::cout<<"Usage : tutorial_HoughCircle_Demo \n"; + return -1; + } // Convert it to gray cvtColor( src, src_gray, COLOR_BGR2GRAY ); From 094d7c4926e53fd2da0c1eb55b17208a3f21e067 Mon Sep 17 00:00:00 2001 From: Adrien BAK Date: Thu, 28 Nov 2013 11:42:44 +0900 Subject: [PATCH 5/6] build fix --- samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp index 5cfcbd34bf..86b69af0f4 100644 --- a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp +++ b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp @@ -28,7 +28,7 @@ namespace // will hold the results of the detection std::vector circles; // runs the actual detection - HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, src_gray.rows/8, cannyThreshold, accumulatorThreshold, 0, 0 ); + 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(); From 38904c9a11979846647b62b611a99a5906188125 Mon Sep 17 00:00:00 2001 From: Adrien BAK Date: Thu, 28 Nov 2013 12:09:17 +0900 Subject: [PATCH 6/6] fix exception being thrown when no arguments are passed --- .../cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp index 86b69af0f4..7338d7f4a2 100644 --- a/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp +++ b/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp @@ -16,6 +16,7 @@ namespace 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"; // initial and max values of the parameters of interests. const int cannyThresholdInitialValue = 200; @@ -48,17 +49,24 @@ namespace } -int main(int, char** argv) +int main(int argc, char** argv) { Mat src, src_gray; + if (argc < 2) + { + std::cerr<<"No input image specified\n"; + std::cout<\n"; + std::cout<