Goal
In this tutorial you will learn how to:
Theory
Hough Circle Transform
Code
- What does this program do?
- Loads an image and blur it to reduce the noise
- Applies the Hough Circle Transform to the blurred image .
- Display the detected circle in a window.
- The sample code that we will explain can be downloaded from here. A slightly fancier version (which shows trackbars for changing the threshold values) can be found here.
#include <iostream>
static void help()
{
cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
"Usage:\n"
"./houghcircles <image_name>, Default is ../data/board.jpg\n" << endl;
}
int main(int argc, char** argv)
{
"{help h ||}{@image|../data/board.jpg|}"
);
if (parser.has("help"))
{
help();
return 0;
}
string filename = parser.get<string>("@image");
if(img.empty())
{
help();
cout << "can not open " << filename << endl;
return -1;
}
vector<Vec3f> circles;
100, 30, 1, 30
);
for( size_t i = 0; i < circles.size(); i++ )
{
}
imshow(
"detected circles", img);
return 0;
}
Explanation
- Load an image
string filename = parser.get<string>("@image");
if(img.empty())
{
help();
cout << "can not open " << filename << endl;
return -1;
}
- Convert it to grayscale:
- Apply a Median blur to reduce noise and avoid false circle detection:
- Proceed to apply Hough Circle Transform:
vector<Vec3f> circles;
gray.rows/16,
100, 30, 1, 30
);
with the arguments:
- gray: Input image (grayscale).
- circles: A vector that stores sets of 3 values:
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0078.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/283/0063.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0079.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/283/0063.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?V=2.7.0)
for each detected circle.
- HOUGH_GRADIENT: Define the detection method. Currently this is the only one available in OpenCV.
- dp = 1: The inverse ratio of resolution.
- min_dist = gray.rows/16: Minimum distance between detected centers.
- param_1 = 200: Upper threshold for the internal Canny edge detector.
- param_2 = 100*: Threshold for center detection.
- min_radius = 0: Minimum radio to be detected. If unknown, put zero as default.
- max_radius = 0: Maximum radius to be detected. If unknown, put zero as default.
- Draw the detected circles:
for( size_t i = 0; i < circles.size(); i++ )
{
}
You can see that we will draw the circle(s) on red and the center(s) with a small green dot
- Display the detected circle(s) and wait for the user to exit the program:
imshow(
"detected circles", img);
Result
The result of running the code above with a test image is shown below: