I have this image, and, after I cropp it, use a Gaussian Blur e equalize it histogram, I need to detect the circle on it.
I am using HoughCircles
from OpenCV
, but I am having difficult to do this methodology because it is returning this:
My code is the following:
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')
CodePudding user response:
Here is one way in Python/OpenCV using morphology and thresholding to clean it up before using HoughCircles.
Input:
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()
Threshold after Morphology:
Result:
x,y,radius:
362 122 36