如何在有噪声的图像中定位圆圈?

How to locate the circle in a image with noise?

我有这张图片,在我裁剪它之后,使用高斯模糊和均衡直方图,我需要检测它上面的圆圈。

我正在使用 OpenCV 中的 HoughCircles,但我很难执行此方法,因为它返回的是:

我的代码如下:

detected_circles = cv2.HoughCircles(eq,
                           cv2.HOUGH_GRADIENT,
                           minDist=6,
                           dp=1.1,
                           param1=150,
                           param2=200,
                           minRadius=50,
                           maxRadius=60)
  
# Draw circles that are detected.
if detected_circles is not None:
  
    # Convert the circle parameters a, b and r to integers.
    detected_circles = np.uint16(np.around(detected_circles))
  
    for pt in detected_circles[0, :]:
        a, b, r = pt[0], pt[1], pt[2]
  
        # Draw the circumference of the circle.
        cv2.circle(eq, (a, b), r, (0, 255, 0), 2)
plt.rc('figure', figsize=(10, 10))

plt.imshow(eq, cmap='gray')


这是 Python/OpenCV 在使用 HoughCircles 之前使用形态学和阈值清理它的一种方法。

输入:

import cv2
import numpy as np

# read image
img = cv2.imread('noisy_black_blob.png')

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

# apply close morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
morph = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel, iterations=3)

# threshold to binary
thresh = cv2.threshold(morph,128,255,cv2.THRESH_BINARY)[1]

# do hough transform for circles
circles = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, minDist=50, dp=1.1, param1=150, param2=10, minRadius=0, maxRadius=0)

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

# save results
cv2.imwrite('noisy_black_blob_thresh.jpg', thresh)
cv2.imwrite('noisy_black_blob_circle.jpg', result)

cv2.imshow("thresh", thresh)
cv2.imshow("circle", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

形态学后的阈值:

结果:

x,y,radius:
362 122 36