# Can anyone explain about "cv2.HoughCircles" method parameter in Simple language?

**URL:** <https://forum.opencv.org/t/can-anyone-explain-about-cv2-houghcircles-method-parameter-in-simple-language/4840>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [August 19, 2021, 7:18am UTC](https://forum.opencv.org/t/can-anyone-explain-about-cv2-houghcircles-method-parameter-in-simple-language/4840 "2021-08-19T07:18:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![asif432](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/asif432/32/2957_2.png) [@asif432](https://forum.opencv.org/u/asif432)\
**Post date:** [August 19, 2021, 7:18am UTC](https://forum.opencv.org/t/can-anyone-explain-about-cv2-houghcircles-method-parameter-in-simple-language/4840/1 "2021-08-19T07:18:20Z")

</div>

I’m using OpenCV python library to find circles from concentric circle and successfully found all the circles but not able to understand the parameter of the “cv2.HoughCircles” method. I also hardcoded the range for “MinRadius” and “MaxRadius”. I just want to define the range for the radius based on the given image. I also go through the official document for “cv2.HoughCircles”  
[here](https://docs.opencv.org/4.5.2/d3/de5/tutorial_js_houghcircles.html)  
but this is also not understandale for me. Any help would be much appreciated!

Here is an image to find the circle:

 ![GoldenSpike](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/395742f51b91ed5ffadec60a3e29124fe4cae730.jpeg)

Here is my python code:

```auto
import numpy as np
import cv2
image = cv2.imread("GoldenSpike.png",0)
output = cv2.imread("GoldenSpike.png",1)
cv2.imshow("Original image", image)
cv2.waitKey()

blurred = cv2.GaussianBlur(image,(11,11),0)

cv2.imshow("Blurred image", blurred)
cv2.waitKey()
previous=0;
i=4
for maxR in range(9,425,9):
    # Finds circles in a grayscale image using the Hough transform
    circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, 100,
                             param1=100,param2=90,minRadius=i,maxRadius=maxR)
    i+=4
    # cv2.HoughCircles function has a lot of parameters, so you can find more about it in documentation
    # or you can use cv2.HoughCircles? in jupyter nootebook to get that 

    # Check to see if there is any detection
    if circles is not None:
        # If there are some detections, convert radius and x,y(center) coordinates to integer
        circles = np.round(circles[0, :]).astype("int")

        for (x, y, r) in circles:
            # Draw the circle in the output image
            cv2.circle(output, (x, y), r, (0,255,0), 1)
            # Draw a rectangle(center) in the output image
            cv2.rectangle(output, (x - 2, y - 2), (x + 2, y + 2), (0,255,0), -1)

cv2.imshow("Detections",output)
cv2.imwrite("CirclesDetection.jpg",output)
cv2.waitKey()

```

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [August 19, 2021, 12:06pm UTC](https://forum.opencv.org/t/can-anyone-explain-about-cv2-houghcircles-method-parameter-in-simple-language/4840/2 "2021-08-19T12:06:32Z")

</div>

According to the documentation, the `method` parameter has to be HOUGH\_GRADIENT.

So `HoughCircles` detects the contours using the Canny operator, and for each point `p` and radius `r` it computes the sum of the contour pixels for a given circle `pc`:

```
Circ_Hough(p,r)=SUM(Canny(pc)==1 where d(p,pc=r))
# p - central pixel of a circle, r - radius, pc - pixels on the circle

```

Now, the `param1` parameter is the canny threshold `maxval`. It means that the gradient value higher than `param1` is always considered a contour pixel. For more detailed explanation see [this tutorial](https://docs.opencv.org/3.4/da/d22/tutorial_py_canny.html).  
You can check the effect of this parameter by running only the canny operator on the image:

```
cny = cv2.canny(image,param1/2,param1)
cv2.imshow("canny",cny)

```

The `param2`is the circle detection threshold. It means that for a point to be considered as a circle center, `Circ_Hough(p,r)>param2`. If `param2` is too low, you’ll have false detections, if it’s too high, some good detections will be discarded.
