Improving the accuracy of opencv in finding circles

Viewed 137

So I have a few images that I need to extract the amount of white circles(with +90% accuracy) from and so far I've come up with this

original image(has 183 circles)

and here's the result I get with my code

as you can see it can detect about half of it but its not that accurate

the code so far:

def check_for_cells(img):
    found = 0
    found_list = []

    img = cv2.imread(img, cv2.IMREAD_COLOR)
    width, height = img.shape[:2]
    img = cv2.resize(img, (int(width/2), int(height/3)))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


    blurred = cv2.GaussianBlur(gray, (15,15), 0)
    histo_norm = cv2.equalizeHist(blurred) 
    _, th = cv2.threshold(blurred, 121, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) 

    min_dist = 20
    param1 = 95
    param2 = 18
    minRadius = 3
    maxRadius = 14


    circles = cv2.HoughCircles(histo_norm, cv2.HOUGH_GRADIENT, 1, min_dist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)
    circles_th = cv2.HoughCircles(th, cv2.HOUGH_GRADIENT, 1, min_dist, param1=79, param2=12, minRadius=minRadius, maxRadius=maxRadius)

    if circles is not None:
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
            cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
            found_list.append([(i[0], i[1]), i[2]])
            found += 1


    if circles_th is not None:
        circles_th = np.uint16(np.around(circles_th))
        for i in circles_th[0,:]:
            if [(i[0], i[1]), i[2]] not in found_list:
                cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
                found_list.append([(i[0], i[1]), i[2]])
                found += 1


    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img, str(found), (0,100), font, 4, (0,0,0), 10)
    # print(found)

    cv2.namedWindow('img', cv2.WINDOW_NORMAL)
    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Any help to improve the code would be appreciated!

0 Answers
Related