Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?
like image 350
Suhail Gupta Avatar asked Jan 26 '19 10:01

Suhail Gupta


1 Answers

  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

like image 131
patelnisheet Avatar answered Oct 10 '22 21:10

patelnisheet