# In opecv function Hough circles how does parameter 1 and 2 affect circle detection and how can I adjust them to increase accuracy?

I'm using opencv houghcircle function to detect circles.

http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

Problem is if I set minimum radius to null it detects wrong center and draws smaller radius circle or sometimes larger radius circle. I'm looking for increasing accuracy of detection by adjusting threshold parameter 1 and 2.

If you have an idea what size circles you are looking for, then it would be best to set min_radius and max_radius accordingly. Otherwise, it will return anything circular of any size.

Parameters 1 and 2 don't affect accuracy as such, more reliability. Param 1 will set the sensitivity; how strong the edges of the circles need to be. Too high and it won't detect anything, too low and it will find too much clutter. Param 2 will set how many edge points it needs to find to declare that it's found a circle. Again, too high will detect nothing, too low will declare anything to be a circle. The ideal value of param 2 will be related to the circumference of the circles.

• thanks, though parameter 2 can be considered as an accuracy factor for circle detection. – Gaurav pawar Apr 22 '15 at 5:47

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

• excellent explanation and walk through! – pietz Apr 5 at 11:19
• is it correct to assume that if minDist is larger than the image itself, the algorithm will only detect a single circle it is most confident about? well, or no circle at all of course. – pietz Apr 5 at 12:39
• Add to your resume: Expert in finding circles :) Honestly though, thanks for your help! Really appreciate it. – pietz Apr 5 at 15:08
• I did some more research and found my previous statement was wrong. minDist is minimum radius for any found circle. maxDist the max. The algorithm forces found circles with a band of tolerance using dp. And min and max set a hard exclusion parameter. dp is which pixels can vote for a given circle. The min and max are hard exclusion for result found circles, throwing them out after they are found. – Eric Leschinski Apr 5 at 17:43
• I've added "expert in Hough circles" to my resume. In case we need to use computer vision for any traffic light bulbs. – Eric Leschinski Apr 5 at 18:03