pythonopencvcomputer-visionobject-detectionhough-transform

Image Edge Detection with python


I'd like to recognize 6 circles in the image below.

enter image description here

enter image description here

When I tried using cv2.canny and HoughCircles, it didn't recognize the circle well.

Do I need to modify the parameters? Or is there any other good way?

The results I want are as follows.

enter image description here


Solution

  • As others mentioned, the usage of the HoughCircles is to be taken with a grain of salt. The shapes mentioned look more like ellipsoids than circles.

    With that being said, here is what I got by going through a standard procedure:

    im = cv2.imread("test_circles.jpg")
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) # convert to RGB, I use this so I can plot with matplotlib, not very important, just make sure to stick with one colourspace
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # convert to grayscale
    blurred = cv2.GaussianBlur(gray, (3, 3), 2) # reduce noise a bit
    circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=1, minDist=20, 
                               param1=50, param2=30, minRadius=10, maxRadius=50) # get the circles
    if circles is not None:
        circles = np.uint16(np.around(circles))
        for i in circles[0, :]:
            if 5 < i[2] < 20: # check the radius of the circle, ajust as needed
                # circle itself
                cv2.circle(im, (i[0], i[1]), i[2], (0, 255, 0), 2)
                # center of the image...
                cv2.circle(im, (i[0], i[1]), 2, (0, 0, 255), 3)
    
    plt.imshow(im)
    plt.show()
    

    The results are like this:

    With circles

    Though not the best of approaches, since I needed to cut one more circle that was detected to the right of the mentioned ones. Try it yourself and adjust the parameters as needed.