Home | History | Annotate | Download | only in hough_circle
      1 Hough Circle Transform {#tutorial_hough_circle}
      2 ======================
      3 
      4 Goal
      5 ----
      6 
      7 In this tutorial you will learn how to:
      8 
      9 -   Use the OpenCV function @ref cv::HoughCircles to detect circles in an image.
     10 
     11 Theory
     12 ------
     13 
     14 ### Hough Circle Transform
     15 
     16 -   The Hough Circle Transform works in a *roughly* analogous way to the Hough Line Transform
     17     explained in the previous tutorial.
     18 -   In the line detection case, a line was defined by two parameters \f$(r, \theta)\f$. In the circle
     19     case, we need three parameters to define a circle:
     20 
     21     \f[C : ( x_{center}, y_{center}, r )\f]
     22 
     23     where \f$(x_{center}, y_{center})\f$ define the center position (green point) and \f$r\f$ is the radius,
     24     which allows us to completely define a circle, as it can be seen below:
     25 
     26     ![](images/Hough_Circle_Tutorial_Theory_0.jpg)
     27 
     28 -   For sake of efficiency, OpenCV implements a detection method slightly trickier than the standard
     29     Hough Transform: *The Hough gradient method*, which is made up of two main stages. The first
     30     stage involves edge detection and finding the possible circle centers and the second stage finds
     31     the best radius for each candidate center. For more details, please check the book *Learning
     32     OpenCV* or your favorite Computer Vision bibliography
     33 
     34 Code
     35 ----
     36 
     37 -#  **What does this program do?**
     38     -   Loads an image and blur it to reduce the noise
     39     -   Applies the *Hough Circle Transform* to the blurred image .
     40     -   Display the detected circle in a window.
     41 
     42 -#  The sample code that we will explain can be downloaded from [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/houghcircles.cpp).
     43     A slightly fancier version (which shows trackbars for
     44     changing the threshold values) can be found [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp).
     45     @include samples/cpp/houghcircles.cpp
     46 
     47 Explanation
     48 -----------
     49 
     50 -#  Load an image
     51     @code{.cpp}
     52     src = imread( argv[1], 1 );
     53 
     54     if( !src.data )
     55       { return -1; }
     56     @endcode
     57 -#  Convert it to grayscale:
     58     @code{.cpp}
     59     cvtColor( src, src_gray, COLOR_BGR2GRAY );
     60     @endcode
     61 -#  Apply a Gaussian blur to reduce noise and avoid false circle detection:
     62     @code{.cpp}
     63     GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
     64     @endcode
     65 -#  Proceed to apply Hough Circle Transform:
     66     @code{.cpp}
     67     vector<Vec3f> circles;
     68 
     69     HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );
     70     @endcode
     71     with the arguments:
     72 
     73     -   *src_gray*: Input image (grayscale).
     74     -   *circles*: A vector that stores sets of 3 values: \f$x_{c}, y_{c}, r\f$ for each detected
     75         circle.
     76     -   *HOUGH_GRADIENT*: Define the detection method. Currently this is the only one available in
     77         OpenCV.
     78     -   *dp = 1*: The inverse ratio of resolution.
     79     -   *min_dist = src_gray.rows/8*: Minimum distance between detected centers.
     80     -   *param_1 = 200*: Upper threshold for the internal Canny edge detector.
     81     -   *param_2* = 100\*: Threshold for center detection.
     82     -   *min_radius = 0*: Minimum radio to be detected. If unknown, put zero as default.
     83     -   *max_radius = 0*: Maximum radius to be detected. If unknown, put zero as default.
     84 
     85 -#  Draw the detected circles:
     86     @code{.cpp}
     87     for( size_t i = 0; i < circles.size(); i++ )
     88     {
     89        Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
     90        int radius = cvRound(circles[i][2]);
     91        // circle center
     92        circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
     93        // circle outline
     94        circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
     95      }
     96     @endcode
     97     You can see that we will draw the circle(s) on red and the center(s) with a small green dot
     98 
     99 -#  Display the detected circle(s):
    100     @code{.cpp}
    101     namedWindow( "Hough Circle Transform Demo", WINDOW_AUTOSIZE );
    102     imshow( "Hough Circle Transform Demo", src );
    103     @endcode
    104 -#  Wait for the user to exit the program
    105     @code{.cpp}
    106     waitKey(0);
    107     @endcode
    108 
    109 Result
    110 ------
    111 
    112 The result of running the code above with a test image is shown below:
    113 
    114 ![](images/Hough_Circle_Tutorial_Result.jpg)
    115