l.geulig
l.geulig

Reputation: 1

cv2.HoughCircles fails to detect circles

original imageFor our experimental setup we want to align a circle (dark black on grey background) to a predefined position (red circle on the image below). To detect the circle, we are currently using the code below:

def findCirclesA(img, res=3, dis=2000, p1=1, p2=1):
    img = _cv2.cvtColor(img, _cv2.COLOR_BGR2GRAY)   
    
    if 0:
        #only use center cirlce!
        lx, ly = img.shape
        X, Y = _np.ogrid[0:lx, 0:ly]
        mask =(X - lx / 2) ** 2 + (Y - ly / 2) ** 2 > (lx/2 -10)**2 
        # Masks
        img[mask] = 0
        # Fancy indexing
        img[range(400), range(400)] = 255

        #done with cirlcing

    # using adaptive threshold
    img = _cv2.medianBlur(img,21)
    
    img = _cv2.adaptiveThreshold(img, 255, _cv2.ADAPTIVE_THRESH_GAUSSIAN_C, _cv2.THRESH_BINARY,1001,0) 
    
    img = _cv2.blur(img,(40,40))
    img = _cv2.blur(img,(20,20))
    
    targetRadiusPx=(450/2)/2 
    targetRadiusPx10per=targetRadiusPx/100 * 5 #3% of radius is allowed spread
    targetRadiusPxMax=int(targetRadiusPx+targetRadiusPx10per)
    targetRadiusPxMin=int(targetRadiusPx-targetRadiusPx10per)
    
    
    circles = _cv2.HoughCircles(img, _cv2.HOUGH_GRADIENT, res, dis, param1 = p1, param2 = p2, minRadius = targetRadiusPxMin, maxRadius = targetRadiusPxMax)
    
    if circles is not None:
        circles = _np.uint16(_np.around(circles))[0,:]    
    return circles#,imgTemp

This circle detected by this code is indicated in green, which does not overlap with the contour of the black circle. Any idea how to improve / change the code? image with markings

So far swichted from _cv2.ADAPTIVE_THRESH_MEAN_C to _cv2.ADAPTIVE_THRESH_GAUSSIAN_C and played around with thresholds

Upvotes: 0

Views: 52

Answers (0)

Related Questions