Why does one circle go undetected by HoughCircles? How does minDist work?
Asked Answered
E

1

6

I was trying to detect circles in the following image using HoughCircles.

enter image description here

Here is the code that I was tuning to find all the circles.

    import cv2
    import numpy as np


    img = cv2.imread("images/coins.jpg", 0)

    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

    minDist = 247

    circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,minDist,
                                param1=170,param2=80,minRadius=0,maxRadius=0)


    print(circles)

    #print("Number of circles detected ", circles.length)


    circles = np.uint16(np.around(circles))

    for i in circles[0,:]:
        # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)


    cv2.imshow('detected circles',cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

For everything I tried, I was not able to detect one coin. The detected circles looked as follows.

enter image description here

I have three questions here:

  • What could be the reason that the second coin from the left in the first row was not detected?
  • What does the minDist parameter do? Could you explain how it works? I read the documentation but could not understand.
  • What do minRadius and maxRadius of zero mean here?
Exon answered 26/1, 2019 at 10:15 Comment(7)
minDist Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed. If the distance between your red dots is too small you get false results ...Combine
minRadius Minimum circle radius. maxRadius Maximum circle radius. .. The minimal needed radius of your green cirles and the maximum possible radius for the green things ....Combine
@PatrickArtner Yeah, I had read this in the documentation. But could not understand the multiple circles' thing.Exon
@PatrickArtner What does maxRadius = 0 mean? The detected circles do not have zero radius.Exon
See the 2005 -- 00 are circles ... if you lower the 247 to say 40, guess they will be detected as well. and the 0 is the defaultvalue given ... I would guess that means is is not used (although the documentation does not say so explicitly). But if it would be used when 0 ... it should never find anything, should it?Combine
I would probably write a loop and count the length of circles start with a minDist of 300 and decrease it by 10 every time you get less then 8 results from applying HoughCircles. If you have an idea about the size of the coins in pixles I would set minRadius to 2/3 of a coin-radius and maxRadius to 4/3 of a coin-radius to rule out finding very small or too big circles. If you still do not get 8 coins, you probably need to enhance the contour (sharpen f.e) but I would guess the image is fine as is as the differences between the 7 and the 1 coin aren't that great to a naked eye.Combine
“For everything I tried, I was not able to detect one coin.” It would be helpful if you showed what you tried. It makes answering easier.Fichu
H
4
  1. What could be the reason that the second coin from the left in the first row was not detected?

    • The second coin is not detected because of the canny edge detection parameter: param1. Reduce the value of param1 and you will get a perfect answer.
  2. What does the minDist parameter do? Could you explain how it works? I read the documentation but could not understand.

    • And as per the documentation minDist is the minimum value between 2 circles. If you reduce the value of minDist you will get multiple neighbor circles.
  3. What do minRadius and maxRadius of zero mean here?

    • minRadius Minimum circle radius.
    • maxRadius Maximum circle radius. If <= 0, uses the maximum image dimension. If < 0, returns centers without finding the radius.

Here is full code:

import cv2
import numpy as np


img = cv2.imread("coins.jpg", 0)

cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

minDist = 247

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,minDist,
                param1=150,param2=80,minRadius=0,maxRadius=0)


print(circles)

#print("Number of circles detected ", circles.length)


circles = np.uint16(np.around(circles))

for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

##cv2.imwrite('detected_circle.jpg',cimg)
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here you can see I got the second coin too. enter image description here

Hoarsen answered 26/1, 2019 at 10:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.