-1

I am trying to find centroid of circular objects OR a circle that can bounding around circular objects in a grayscale image.

So far what I have done is turn that grayscale image to binary image using adaptive thresholding.

Grayscale image

enter image description here

Threshold image

enter image description here

Up till now, i have used hough transform and Findcontour. None of these method work.

What should be an approach to this?

nathancy
  • 42,661
  • 14
  • 115
  • 137
Totally New
  • 147
  • 5
  • What did not work with Hough transform? Were it able to find at least a few circles? – marco romelli Jun 26 '19 at 14:38
  • A potential approach in this order is to convert image to grayscale, gaussian blur image, perform adaptive thresholding, canny edge detection, find contours, iterate through contours and filter using contour area and a min/max threshold area. For each filtered contour find centroid using `cv2.moments()` – nathancy Jun 26 '19 at 21:14

1 Answers1

0

I got a decent result using the Hough transform for circles. This is the pipeline:

img = cv2.imread('I7Ykpbs.jpg', 0)
img = cv2.GaussianBlur(img, (5, 5), 2, 2)
img_th = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                            cv2.THRESH_BINARY, 9, 3)
circles = cv2.HoughCircles(img_th, cv2.HOUGH_GRADIENT, 2, minDist=30, 
                           param1=200, param2=40, minRadius=10, maxRadius=20)

for i in range(circles.shape[1]):
    c = circles[0,i,:]
    center = (np.round(c[0]), np.round(c[1]))
    radius = np.round(c[2])
    # print(center)
    # print(radius)
    if np.linalg.norm(np.array([600., 600.])-center) < 500.:
        cv2.circle(img, center, 3, (0,255,0), -1, 8, 0)
        cv2.circle(img, center, radius, (0,0,255), 3, 8, 0)

plt.imshow(img)
plt.show()

It's not perfect but I think you can start from here and do some finetuning on parameters and preprocessing to optimize the result. Detected circles

marco romelli
  • 1,143
  • 8
  • 19