Find the number of clusters in this image:
I'm trying to find the number of cluster in this image. I tried openCV morphologyEx and erode but can't seem to get a single pixel for each cluster. Please suggest which would be the best way to count the number of clusters in an image using openCV preferably in Python.
--Edit
I tried thinning, erode and morphologyEx(closing) but couldn't converge the clusters to a single pixel. Below are some of things I tried.
kernel = np.ones((2, 2), np.uint8) #[[1,1,1],[1,1,1],[1,1,1]
erosion = cv2.erode(img, kernel, iterations=1)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('test1.jpg', erosion)
cv2.imwrite('test2.jpg', closing)
img = cv2.imread(file, 0)
size = np.size(img)
skel = np.zeros(img.shape, np.uint8)
#ret, img = cv2.threshold(img, 127, 255, 0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
done = False
while (not done):
eroded = cv2.erode(img, element)
temp = cv2.dilate(eroded, element)
temp = cv2.subtract(img, temp)
skel = cv2.bitwise_or(skel, temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
if zeros == size:
done = True
cv2.imwrite('thinning.jpg', skel)
