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:
- 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
andmaxRadius
ofzero
mean here?
circles
start with a minDist of 300 and decrease it by 10 every time you get less then 8 results from applyingHoughCircles
. 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