Detecting balls in a bearing

Hello all,

After playing for hours with my mask’s parameters for the “SimpleBlobDetector” (seemed promising), no better results (maybe I played wrongly).
If I’m trying local maximas as i was told it’s the simplest way. I think I’m doing something wrong (again) : not even a good positive (only hundreds of false positives).
I looked at this post to understand a bit, but it may not be the proper way for my case.

import cv2
import numpy as np

img = cv2.imread('many_balls_in_bearing_with_light_good_side.jpg')
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

localmax = cv2.dilate(img1, None, iterations=40)
localmax = (img1 == localmax)

mask = (img1 >= 0.3*255)

result = levelmask & localmax
(nlabels, labels, stats, centroids) = cv2.connectedComponentsWithStats(result.astype(np.uint8), connectivity=8)
print(nlabels-1, "found")

cv2.imshow("Original Image", img)
for i in range(1, nlabels): 
    centroid = centroids[i]
    x, y = int(centroid[0]), int(centroid[1])
    cv2.circle(img, (x, y), 5, (0, 255, 0), -1) # Rayon de 5, couleur verte

cv2.imshow("Composants detectes", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

But the results are horrible so far :slight_smile:
image
And I thought image processing would be fun :smiling_face_with_tear: