Skip to content

Commit d99ced6

Browse files
committed
Tutorial Hough Circles
1 parent 9ff33da commit d99ced6

File tree

8 files changed

+319
-96
lines changed

8 files changed

+319
-96
lines changed
Lines changed: 116 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
Hough Circle Transform {#tutorial_hough_circle}
22
======================
33

4+
@prev_tutorial{tutorial_hough_lines}
5+
@next_tutorial{tutorial_remap}
6+
47
Goal
58
----
69

710
In this tutorial you will learn how to:
811

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.
1013

1114
Theory
1215
------
@@ -31,31 +34,96 @@ Theory
3134
the best radius for each candidate center. For more details, please check the book *Learning
3235
OpenCV* or your favorite Computer Vision bibliography
3336

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+
3442
Code
3543
----
3644

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
4664

4765
Explanation
4866
-----------
4967

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:
59127

60128
- *gray*: Input image (grayscale).
61129
- *circles*: A vector that stores sets of 3 values: \f$x_{c}, y_{c}, r\f$ for each detected
@@ -69,16 +137,39 @@ Explanation
69137
- *min_radius = 0*: Minimum radius to be detected. If unknown, put zero as default.
70138
- *max_radius = 0*: Maximum radius to be detected. If unknown, put zero as default.
71139

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
75165

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
78169

79170
Result
80171
------
81172

82173
The result of running the code above with a test image is shown below:
83174

84-
![](images/Hough_Circle_Tutorial_Result.jpg)
175+
![](images/Hough_Circle_Tutorial_Result.png)
Loading

doc/tutorials/imgproc/table_of_content_imgproc.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ In this section you will learn about the image processing (manipulation) functio
133133

134134
- @subpage tutorial_hough_circle
135135

136+
*Languages:* C++, Java, Python
137+
136138
*Compatibility:* \> OpenCV 2.0
137139

138140
*Author:* Ana Huamán

samples/cpp/houghcircles.cpp

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @file houghcircles.cpp
3+
* @brief This program demonstrates circle finding with the Hough transform
4+
*/
5+
#include "opencv2/imgcodecs.hpp"
6+
#include "opencv2/highgui.hpp"
7+
#include "opencv2/imgproc.hpp"
8+
9+
using namespace cv;
10+
using namespace std;
11+
12+
int main(int argc, char** argv)
13+
{
14+
//![load]
15+
const char* filename = argc >=2 ? argv[1] : "../../../data/smarties.png";
16+
17+
// Loads an image
18+
Mat src = imread( filename, IMREAD_COLOR );
19+
20+
// Check if image is loaded fine
21+
if(src.empty()){
22+
printf(" Error opening image\n");
23+
printf(" Program Arguments: [image_name -- default %s] \n", filename);
24+
return -1;
25+
}
26+
//![load]
27+
28+
//![convert_to_gray]
29+
Mat gray;
30+
cvtColor(src, gray, COLOR_BGR2GRAY);
31+
//![convert_to_gray]
32+
33+
//![reduce_noise]
34+
medianBlur(gray, gray, 5);
35+
//![reduce_noise]
36+
37+
//![houghcircles]
38+
vector<Vec3f> circles;
39+
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
40+
gray.rows/16, // change this value to detect circles with different distances to each other
41+
100, 30, 1, 30 // change the last two parameters
42+
// (min_radius & max_radius) to detect larger circles
43+
);
44+
//![houghcircles]
45+
46+
//![draw]
47+
for( size_t i = 0; i < circles.size(); i++ )
48+
{
49+
Vec3i c = circles[i];
50+
Point center = Point(c[0], c[1]);
51+
// circle center
52+
circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA);
53+
// circle outline
54+
int radius = c[2];
55+
circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA);
56+
}
57+
//![draw]
58+
59+
//![display]
60+
imshow("detected circles", src);
61+
waitKey();
62+
//![display]
63+
64+
return 0;
65+
}

samples/data/smarties.png

88.7 KB
Loading

0 commit comments

Comments
 (0)