Circle detection without overlapping

前端 未结 3 690
慢半拍i
慢半拍i 2021-01-23 17:08

I want to do circle detection under the condition that: overlap circles will be count as 1 circle.

Particularly, when I do circle detection and put the letter \"P\" to e

3条回答
  •  灰色年华
    2021-01-23 17:56

    I would suggest using contours instead. However, if you do want to use HoughCircles, look at the 4th parameter in the function. Changing this, I could get rid of the overlappings. Additionally, I tweaked a bit the parameters for canny threshold in the HoughCircles function until I got the desired results. I'd suggest understanding the parameters well before coming up with a conclusion.

    Code:

    import cv2
    import numpy as np
    
    arr = cv2.imread("U:/SO/032OR.jpg")
    print(arr.shape)
    imggray = cv2.cvtColor(arr, cv2.COLOR_BGR2GRAY)
    # Not median blur 
    imggray = cv2.GaussianBlur(imggray, (9,9),3)
    
    circles_norm = cv2.HoughCircles(imggray, cv2.HOUGH_GRADIENT, 1, imggray.shape[0]/16, 
                                    param1=20, param2=8, minRadius=15, maxRadius=30)
    circles_norm = np.uint16(np.around(circles_norm))[0,:]
    
    for i in circles_norm:
        center = (i[0], i[1])
        cv2.putText(arr, 'P', (i[0], i[1]), cv2.FONT_HERSHEY_COMPLEX, 0.5, 
                   (0,0,255),1,cv2.LINE_AA)
    

    Result:

提交回复
热议问题