From 8ecfa9b21fd7fa6d8a8bfa5fc04c63065005e96b Mon Sep 17 00:00:00 2001 From: Prasanna Krishnasamy Date: Sun, 21 May 2017 01:31:25 +0000 Subject: [PATCH] Adding example 12-02 --- CMakeLists.txt | 2 ++ example_12-02.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 example_12-02.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fd98ca8..b6015f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ add_executable( example_11-01 example_11-01.cpp ) add_executable( example_11-02 example_11-02.cpp ) add_executable( example_11-03 example_11-03.cpp ) add_executable( example_12-01 example_12-01.cpp ) +add_executable( example_12-02 example_12-02.cpp ) add_executable( example_16-01 example_16-01.cpp ) target_link_libraries( example_02-01 ${OpenCV_LIBS} ) @@ -65,6 +66,7 @@ target_link_libraries( example_11-01 ${OpenCV_LIBS} ) target_link_libraries( example_11-02 ${OpenCV_LIBS} ) target_link_libraries( example_11-03 ${OpenCV_LIBS} ) target_link_libraries( example_12-01 ${OpenCV_LIBS} ) +target_link_libraries( example_12-02 ${OpenCV_LIBS} ) target_link_libraries( example_16-01 ${OpenCV_LIBS} ) diff --git a/example_12-02.cpp b/example_12-02.cpp new file mode 100644 index 0000000..435247e --- /dev/null +++ b/example_12-02.cpp @@ -0,0 +1,44 @@ +// Example 12-2. Using cv::HoughCircles() to return a sequence of circles found in a +// grayscale image + +#include +#include +#include + +using namespace cv; +using namespace std; + +int main(int argc, char** argv) { + + if(argc != 2) { + cout << "Hough Circle detect\nUsage: " <\n" << endl; + return -1; + } + + cv::Mat src, image; + + src = cv::imread( argv[1], 1 ); + if( src.empty() ) { cout << "Cannot load " << argv[1] << endl; return -1; } + + cv::cvtColor(src, image, cv::COLOR_BGR2GRAY); + cv::GaussianBlur(image, image, Size(5,5), 0, 0); + + vector circles; + cv::HoughCircles(image, circles, cv::HOUGH_GRADIENT, 2, image.cols/4); + + for( size_t i = 0; i < circles.size(); ++i ) { + cv::circle( + src, + cv::Point(cvRound(circles[i][0]), cvRound(circles[i][1])), + cvRound(circles[i][2]), + cv::Scalar(0,0,255), + 2, + cv::LINE_AA + ); + } + + cv::imshow( "Hough Circles", src); + cv::waitKey(0); + + return 0; +}