Hello all,
I’m trying to detect (and count) the number of balls (I know the diameter 0,28mm) in a bearing.
So far I tried using opencv with HoughCircles, but the results are quite bad
I tried something like this :
import numpy as np
import cv2
# img = cv2.imread('/home/stephane/Documents/Bastien_Bearing/2_balls_sticked.jpg')
img = cv2.imread('/home/stephane/Documents/Bastien_Bearing/many_balls_in_bearing_with_light_good_side.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.medianBlur(gray, 25) #cv2.bilateralFilter(gray,10,50,50)
# For 2 balls
minDist = 10
param1 = 9
param2 = 20
minRadius = 3
maxRadius = 15
# # For many balls
# minDist = 1
# param1 = 9
# param2 = 20
# minRadius = 1
# maxRadius = 15
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)
if circles is not None:
circles = np.uint16(np.around(circles))
print("Nombre de cercles détectés:", len(circles[0]))
for i in circles[0,:]:
cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
else:
print("Aucun cercle détecté.")
# Show result for testing:
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Somebody told me that I was maybe going iin the wrong dirction,
That I should have source images (unfiltered, no median blur, no filtering, no adjusting). Getting a decent point light source and shut out all other light. you’ll get that light reflected in points on all the balls. those are local maxima. easily found.
So, first I’m not sure on how to find local maximas so far (but trying ),
Second I’m wondering If I should “simply” use a “filter” to look for RED areas
If anybody has an idea to put mee on the right track it would be awesome
Otherwise I’ll keep in touch if I found anything in the meantime.