提高opencv找圆的准确率

时间:2021-03-18 14:55:04

标签: python python-3.x opencv image-processing image-manipulation

所以我有一些图像需要从中提取白色圆圈的数量(准确度为 +90%),到目前为止我已经想出了这个

original image(has 183 circles)

and here's the result I get with my code

如你所见,它可以检测到大约一半,但它不是那么准确

目前的代码:

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()

对改进代码的任何帮助将不胜感激!

0 个答案:

没有答案
相关问题