39590eb429e1fceb88a58f195fa949f22da17eb0
[profile/ivi/opencv.git] / doc / tutorials / imgproc / imgtrans / hough_circle / hough_circle.rst
1 .. _hough_circle:
2
3 Hough Circle Transform
4 ***********************
5
6 Goal
7 =====
8 In this tutorial you will learn how to:
9
10 * Use the OpenCV function :hough_circles:`HoughCircles <>` to detect circles in an image.
11
12 Theory
13 =======
14
15 Hough Circle Transform
16 ------------------------
17
18 * The Hough Circle Transform works in a *roughly* analogous way to the Hough Line Transform explained in the previous tutorial.
19 * In the line detection case, a line was defined by two parameters :math:`(r, \theta)`. In the circle case, we need three parameters to define a circle:
20
21   .. math::
22
23      C : ( x_{center}, y_{center}, r )
24
25   where :math:`(x_{center}, y_{center})` define the center position (gree point) and :math:`r` is the radius, which allows us to completely define a circle, as it can be seen below:
26
27   .. image:: images/Hough_Circle_Tutorial_Theory_0.jpg
28           :alt: Result of detecting circles with Hough Transform
29           :align: center
30
31 * For sake of efficiency, OpenCV implements a detection method slightly trickier than the standard Hough Transform: *The Hough gradient method*. For more details, please check the book *Learning OpenCV* or your favorite Computer Vision bibliography
32
33 Code
34 ======
35
36 #. **What does this program do?**
37
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    .. |TutorialHoughCirclesSimpleDownload| replace:: here
43    .. _TutorialHoughCirclesSimpleDownload: http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/cpp/houghcircles.cpp
44    .. |TutorialHoughCirclesFancyDownload| replace:: here
45    .. _TutorialHoughCirclesFancyDownload: http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp
46
47 #. The sample code that we will explain can be downloaded from |TutorialHoughCirclesSimpleDownload|_. A slightly fancier version (which shows both Hough standard and probabilistic with trackbars for changing the threshold values) can be found |TutorialHoughCirclesFancyDownload|_.
48
49 .. code-block:: cpp
50
51    #include "opencv2/highgui/highgui.hpp"
52    #include "opencv2/imgproc/imgproc.hpp"
53    #include <iostream>
54    #include <stdio.h>
55
56    using namespace cv;
57
58    /** @function main */
59    int main(int argc, char** argv)
60    {
61      Mat src, src_gray;
62
63      /// Read the image
64      src = imread( argv[1], 1 );
65
66      if( !src.data )
67        { return -1; }
68
69      /// Convert it to gray
70      cvtColor( src, src_gray, CV_BGR2GRAY );
71
72      /// Reduce the noise so we avoid false circle detection
73      GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
74
75      vector<Vec3f> circles;
76
77      /// Apply the Hough Transform to find the circles
78      HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );
79
80      /// Draw the circles detected
81      for( size_t i = 0; i < circles.size(); i++ )
82      {
83          Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
84          int radius = cvRound(circles[i][2]);
85          // circle center
86          circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
87          // circle outline
88          circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
89       }
90
91      /// Show your results
92      namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
93      imshow( "Hough Circle Transform Demo", src );
94
95      waitKey(0);
96      return 0;
97    }
98
99
100 Explanation
101 ============
102
103
104 #. Load an image
105
106    .. code-block:: cpp
107
108      src = imread( argv[1], 1 );
109
110      if( !src.data )
111        { return -1; }
112
113 #. Convert it to grayscale:
114
115    .. code-block:: cpp
116
117       cvtColor( src, src_gray, CV_BGR2GRAY );
118
119 #. Apply a Gaussian blur to reduce noise and avoid false circle detection:
120
121    .. code-block::  cpp
122
123       GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
124
125 #. Proceed to apply Hough Circle Transform:
126
127    .. code-block:: cpp
128
129       vector<Vec3f> circles;
130
131       HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );
132
133    with the arguments:
134
135    * *src_gray*: Input image (grayscale)
136    * *circles*: A vector that stores sets of 3 values: :math:`x_{c}, y_{c}, r` for each detected circle.
137    * *CV_HOUGH_GRADIENT*: Define the detection method. Currently this is the only one available in OpenCV
138    * *dp = 1*: The inverse ratio of resolution
139    * *min_dist = src_gray.rows/8*: Minimum distance between detected centers
140    * *param_1 = 200*: Upper threshold for the internal Canny edge detector
141    * *param_2* = 100*: Threshold for center detection.
142    * *min_radius = 0*: Minimum radio to be detected. If unknown, put zero as default.
143    * *max_radius = 0*: Maximum radius to be detected. If unknown, put zero as default
144
145 #. Draw the detected circles:
146
147    .. code-block:: cpp
148
149       for( size_t i = 0; i < circles.size(); i++ )
150       {
151          Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
152          int radius = cvRound(circles[i][2]);
153          // circle center
154          circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
155          // circle outline
156          circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
157        }
158
159    You can see that we will draw the circle(s) on red and the center(s) with a small green dot
160
161 #. Display the detected circle(s):
162
163    .. code-block:: cpp
164
165       namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
166       imshow( "Hough Circle Transform Demo", src );
167
168 #. Wait for the user to exit the program
169
170    .. code-block:: cpp
171
172       waitKey(0);
173
174
175 Result
176 =======
177
178 The result of running the code above with a test image is shown below:
179
180 .. image:: images/Hough_Circle_Tutorial_Result.jpg
181    :alt: Result of detecting circles with Hough Transform
182    :align: center