我有這張圖片,在裁剪它之后,使用高斯模糊 e 均衡它的直方圖,我需要檢測它上面的圓圈。

我正在使用HoughCirclesfrom OpenCV,但我很難做到這一點,因為它回傳了這個:

我的代碼如下:
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')
uj5u.com熱心網友回復:
這是 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483300.html
