Intuition for parameters of HoughCircles:
image: 8-bit, single channel image. If working with a color image, convert to grayscale first.
method: Defines the method to detect circles in images. Currently, the only implemented method is cv2.HOUGH_GRADIENT
, which corresponds to the Yuen et al. paper.
dp: Resolution of the accumulator array. Votes cast are binned into squares set by dp size. Set too small and only perfect circles are found, set too high and noise collaborates to vote for non-circles.
minDist: Minimum distance between the center (x, y) coordinates of detected circles. If the minDist is too small, multiple circles in the same neighborhood as the original may be (falsely) detected. If the minDist is too large, then some circles may not be detected at all.
param1: Is a number forwarded to the Canny edge detector (applied to a grayscale image) that represents the threshold1 passed to Canny(...)
. Canny uses that number to guide edge detection: http://docs.opencv.org/2.4/modules/imgproc/doc/feature_detection.html?highlight=canny
param2: Accumulator threshold value for the cv2.HOUGH_GRADIENT
method. The smaller the threshold is, the more circles will be detected (including false circles). The larger the threshold is, the more circles will potentially be returned.
minRadius: Minimum size of the radius in pixels. Don't set minRadius
and maxRadius
far apart unless you want all possible circles that might be found in that range.
maxRadius: Maximum size of the radius (in pixels). Don't set minRadius
and maxRadius
far apart unless you want all possible circles found in that range.
If you don't know radius, you must auto-tune hyper parameters:
If you don't feed the right radius (+- 15 pixels) to HoughCircles, the output is next to useless because it returns circles everywhere and nowhere without rhyme or reason.
However there is a trick if you don't know the radius and don't know the optimal dp resolution and accumulator array threshold. HoughCircles is quick (linear time?), put HoughCircles in a loop, start out with a big minDist, fine dp resolution, and high accumulator voting threshold, and start guessing the largest radius of circle you're willing to find, HoughCircles returns nothing. Each iteration, slowly decrease radius, decrease threshold, increase dp resolution. Eventually HoughCircles will find a pixel-perfect circle.
Do this process a second time and approach from the other side, loop HoughCircles starting out guessing a minimum radius, low resolution, high voting threshold, eventually the smallest and highest quality circle will be found. If the two circles agree on a common circle, you can be very confidant the circle found is high quality, otherwise increment/decrement the hyperparameters at different ranges and speeds.
HoughCircles is mathematically precise and exactly correct, it has been proven to find pixel perfect circles even when you don't know radius. With hyper-parameters auto-tuned correctly for your images, the circles found are pixel perfect.
Not only can you use HoughCircles to find the middle of the circle, you can use it to define the outer lip and inner lip of the circle's edge gradient (sub pixel accuracy). For example, the outer lip or inner lip of a coin's circumference.
Many passes of many iterations must be used to auto-tune hyper parameters uniquely for every image to slowly discover which circles are the one you want.
Prove pixel perfect accuracy:
If performance must be proved as sub-pixel perfect for an industrial setting, you can't get around intuitively understanding what the algorithm is doing so you can fiddle with its implementation to suit your needs. For in depth guide, See: "Computer Vision: A Modern Approach" by David Forsyth and Jean Ponce, 2nd Edition, Chapter 10 on the Hough Transform.