简体   繁体   中英

Opencv HoughCircles missing obvious circle

I have tried some variations in the parameters, read a detailed description of their meaning, but I can't seem to detect a simple circle in an image. This is a simplified function I have tried:

def get_a_circles(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    circles = cv2.HoughCircles(gray, # input grayscale image
                              cv2.HOUGH_GRADIENT, 
                              2, 
                              minDist=5, 
                              param1=10, param2=200,
                              minRadius=0, 
                               maxRadius=0) 
    return circles

which, when run on this image:

img = cv2.imread("step2.jpg")
get_a_circle(img.copy())

在此处输入图片说明

returns none. It does however detect the circles in this image:

image circles found & highlighted
在此处输入图片说明 在此处输入图片说明

I tried to add some blur to the image that fails, with either gray = cv2.medianBlur(gray, 5) or gray = cv2.GaussianBlur(gray,(5,5),0) but it does not improve the results.

Any suggestions on what to try with that image? (It would seem it's an obvious circle)

You need to lower param2 in HoughCircles in Python/OpenCV.

Input:

在此处输入图片说明

import cv2
import numpy as np

# Read image
img = cv2.imread('dot.jpg')
hh, ww = img.shape[:2]

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# median filter
gray = cv2.medianBlur(gray, 5)

# get Hough circles
min_dist = int(ww/20)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, minDist=min_dist, param1=150, param2=10, minRadius=0, maxRadius=0)
print(circles)

# draw circles
result = img.copy()
for circle in circles[0]:
    # draw the circle in the output image, then draw a rectangle
    # corresponding to the center of the circle
    (x,y,r) = circle
    x = int(x)
    y = int(y)
    cv2.circle(result, (x, y), r, (0, 0, 255), 1)

# save results
cv2.imwrite('dot_circle.jpg', result)

# show images
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM