I am trying to differentiate images based on whether they contain circles or not, but it keeps on detecting circles where there are none and misses obvious ones. I have been playing around with the parameters (though I am not being entirely sure what param1 and 2 do) but nothing seems to work. Here is the code for detecting circles and drawing the circle it detects (adapted from another user):
def circle_detection(filename):
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.medianBlur(gray, 25)
minDist = 70
param1 = 71
param2 = 20
minRadius = 1
maxRadius = 20
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
circles1 = sorted(circles, key = lambda x:x[2])
for (x, y, r) in circles1:
r_mm = round(r/109, 2)
cv2.circle(img, (x,y), r, (0, 0, 255), 1)
cv2.circle(img, (x,y), 1, (0,0,255), 1)
return img
else:
return None
Here are some of my results from a few of the images I am looking at. I want to exclude images 8, 20 and 24, but it detects a circle. The circles on the other images also seem to be misplaced:
Here are some of the images that were excluded by my code this run, where some clearly have circles. Other parameters I've tested seem to detect circles for some, but then rejects others that had circles before:
I am not sure what the best parameters are to get the best results, nor know if there is a better way to exclude the images that I want. I am ok with a few images being incorrectly identified, but my code seems to detect circles or not arbitrarily.
Here is a link to some of the images: https://mcgill-my.sharepoint.com/:f:/g/personal/lewis_mackay_mail_mcgill_ca/EmyvEVgf-K5HiCDWOVuLti0BmoNAxBmMIeLc0dJYtkobow?e=IjEDuP