1
1
Hough Circle Transform {#tutorial_hough_circle}
2
2
======================
3
3
4
+ @prev_tutorial{tutorial_hough_lines}
5
+ @next_tutorial{tutorial_remap}
6
+
4
7
Goal
5
8
----
6
9
7
10
In this tutorial you will learn how to:
8
11
9
- - Use the OpenCV function @ ref cv:: HoughCircles to detect circles in an image.
12
+ - Use the OpenCV function ** HoughCircles() ** to detect circles in an image.
10
13
11
14
Theory
12
15
------
@@ -31,31 +34,96 @@ Theory
31
34
the best radius for each candidate center. For more details, please check the book * Learning
32
35
OpenCV* or your favorite Computer Vision bibliography
33
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
+
34
42
Code
35
43
----
36
44
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/opencv/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/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp ) .
45
- @include samples/cpp/houghcircles.cpp
45
+ @add_toggle_cpp
46
+ The sample code that we will explain can be downloaded from
47
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp ) .
48
+ A slightly fancier version (which shows trackbars for changing the threshold values) can be found
49
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/HoughCircle_Demo.cpp ) .
50
+ @include samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp
51
+ @end_toggle
52
+
53
+ @add_toggle_java
54
+ The sample code that we will explain can be downloaded from
55
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java ) .
56
+ @include samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java
57
+ @end_toggle
58
+
59
+ @add_toggle_python
60
+ The sample code that we will explain can be downloaded from
61
+ [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py ) .
62
+ @include samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py
63
+ @end_toggle
46
64
47
65
Explanation
48
66
-----------
49
67
50
- -# Load an image
51
- @snippet samples/cpp/houghcircles.cpp load
52
- -# Convert it to grayscale:
53
- @snippet samples/cpp/houghcircles.cpp convert_to_gray
54
- -# Apply a Median blur to reduce noise and avoid false circle detection:
55
- @snippet samples/cpp/houghcircles.cpp reduce_noise
56
- -# Proceed to apply Hough Circle Transform:
57
- @snippet samples/cpp/houghcircles.cpp houghcircles
58
- with the arguments:
68
+ The image we used can be found [ here] ( https://raw.githubusercontent.com/opencv/opencv/master/samples/data/smarties.png )
69
+
70
+ #### Load an image:
71
+
72
+ @add_toggle_cpp
73
+ @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp load
74
+ @end_toggle
75
+
76
+ @add_toggle_java
77
+ @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py load
78
+ @end_toggle
79
+
80
+ @add_toggle_python
81
+ @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java load
82
+ @end_toggle
83
+
84
+ #### Convert it to grayscale:
85
+
86
+ @add_toggle_cpp
87
+ @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp convert_to_gray
88
+ @end_toggle
89
+
90
+ @add_toggle_java
91
+ @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py convert_to_gray
92
+ @end_toggle
93
+
94
+ @add_toggle_python
95
+ @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java convert_to_gray
96
+ @end_toggle
97
+
98
+ #### Apply a Median blur to reduce noise and avoid false circle detection:
99
+
100
+ @add_toggle_cpp
101
+ @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp reduce_noise
102
+ @end_toggle
103
+
104
+ @add_toggle_java
105
+ @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py reduce_noise
106
+ @end_toggle
107
+
108
+ @add_toggle_python
109
+ @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java reduce_noise
110
+ @end_toggle
111
+
112
+ #### Proceed to apply Hough Circle Transform:
113
+
114
+ @add_toggle_cpp
115
+ @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp houghcircles
116
+ @end_toggle
117
+
118
+ @add_toggle_java
119
+ @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py houghcircles
120
+ @end_toggle
121
+
122
+ @add_toggle_python
123
+ @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java houghcircles
124
+ @end_toggle
125
+
126
+ - with the arguments:
59
127
60
128
- * gray* : Input image (grayscale).
61
129
- * circles* : A vector that stores sets of 3 values: \f$x_ {c}, y_ {c}, r\f$ for each detected
@@ -69,16 +137,39 @@ Explanation
69
137
- * min_radius = 0* : Minimum radius to be detected. If unknown, put zero as default.
70
138
- * max_radius = 0* : Maximum radius to be detected. If unknown, put zero as default.
71
139
72
- -# Draw the detected circles:
73
- @snippet samples/cpp/houghcircles.cpp draw
74
- You can see that we will draw the circle(s) on red and the center(s) with a small green dot
140
+ #### Draw the detected circles:
141
+
142
+ @add_toggle_cpp
143
+ @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp draw
144
+ @end_toggle
145
+
146
+ @add_toggle_java
147
+ @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py draw
148
+ @end_toggle
149
+
150
+ @add_toggle_python
151
+ @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java draw
152
+ @end_toggle
153
+
154
+ You can see that we will draw the circle(s) on red and the center(s) with a small green dot
155
+
156
+ #### Display the detected circle(s) and wait for the user to exit the program:
157
+
158
+ @add_toggle_cpp
159
+ @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp display
160
+ @end_toggle
161
+
162
+ @add_toggle_java
163
+ @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py display
164
+ @end_toggle
75
165
76
- -# Display the detected circle(s) and wait for the user to exit the program:
77
- @snippet samples/cpp/houghcircles.cpp display
166
+ @add_toggle_python
167
+ @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java display
168
+ @end_toggle
78
169
79
170
Result
80
171
------
81
172
82
173
The result of running the code above with a test image is shown below:
83
174
84
- ![ ] ( images/Hough_Circle_Tutorial_Result.jpg )
175
+ ![ ] ( images/Hough_Circle_Tutorial_Result.png )
0 commit comments