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

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.

I have three questions here:
minDist parameter do? Could you explain how it works? I read the documentation but could not understand.minRadius and maxRadius of zero mean here?What could be the reason that the second coin from the left in the first row was not detected?
param1.
Reduce the value of param1 and you will get a perfect answer.What does the minDist parameter do? Could you explain how it works? I read the documentation but could not understand.
minDist is the minimum value between 2 circles. If you reduce the value of minDist you will get multiple neighbor circles.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.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With