0

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: Circle Detection test

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:

Circle Rejection test

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

oceanicboy
  • 51
  • 1
  • 6
  • 1
    can you generate the canny edge detection results with the same parameters that are used by houghCircle (see documentation about `v2.HOUGH_GRADIENT, ..., param1=param1, param2=param2`). If in those canny edge detection results there are too many edges, houghCircle will have big problems, because it does only look at "active pixels". Another way is to implement a simple RANSAC based circle detection and use it on edge images directly, which might be more robust and/or accurate, as shown in https://stackoverflow.com/a/20734263/2393191 – Micka Jun 09 '22 at 15:45
  • I will certainly take a look at RANSAC. But what do you mean by 'canny edge detection' and 'active pixels'? Thank you for the response. – oceanicboy Jun 09 '22 at 15:47
  • Have a look at how hough works. Basically for every active pixel (an edge in this case) it calculates to which circle it could belong and in the end it is checked whether there is a circle with enough "support". – Micka Jun 09 '22 at 16:08
  • About canny edge detection: Have a look at param1 in https://docs.opencv.org/4.x/d3/de5/tutorial_js_houghcircles.html and have a look at cv2.canny function. – Micka Jun 09 '22 at 16:09

0 Answers0