Finds circles in a grayscale image using the Hough transform.
The function finds circles in a grayscale image using a modification of the
Hough transform.
Example:
// C++ code:
#include
#include
#include
using namespace cv;
int main(int argc, char argv)
Mat img, gray;
if(argc != 2 && !(img=imread(argv[1], 1)).data)
return -1;
cvtColor(img, gray, CV_BGR2GRAY);
// smooth it, otherwise a lot of false circles may be detected
GaussianBlur(gray, gray, Size(9, 9), 2, 2);
vector circles;
HoughCircles(gray, circles, CV_HOUGH_GRADIENT,
2, gray->rows/4, 200, 100);
for(size_t i = 0; i < circles.size(); i++)
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// draw the circle center
circle(img, center, 3, Scalar(0,255,0), -1, 8, 0);
// draw the circle outline
circle(img, center, radius, Scalar(0,0,255), 3, 8, 0);
namedWindow("circles", 1);
imshow("circles", img);
return 0;
Note: Usually the function detects the centers of circles well. However, it
may fail to find correct radii. You can assist to the function by specifying
the radius range (minRadius
and maxRadius
) if you
know it. Or, you may ignore the returned radius, use only the center, and
find the correct radius using an additional procedure.
Note:
- An example using the Hough circle detector can be found at
opencv_source_code/samples/cpp/houghcircles.cpp