Via a USB microscope on a RPI4,I'm trying to check how many balls are on the image.I'm trying with the HoughCircles
solution and I have some results with tuning the parameters.
But I'm wondering if it's the right solution after an entire day of tuning
Via a USB microscope on a RPI4,I'm trying to check if there are some balls (and how many of them are they) on the image.I'm trying with the HoughCircles
solution.
Another post helped me a lot.
- Some results when no light from the microscope on the ball,
- No result at all if the light is too strong (and I thought it would be easier to make a threshold for a binary image with more light but apparently not)
- Medium results if the balls are sticked together (it detects 2 spheres, but not putting the center on the right place... And at the end I'll have 38 of them touching themselves in a micro-bearing ^^)
- No results with many balls in the bearing
import numpy as npimport cv2# img = cv2.imread('/home/stephane/Documents/Bastien_Bearing/Ball_on_paper_without_light.jpg')# img = cv2.imread('/home/stephane/Documents/Bastien_Bearing/Ball_on_paper_with_light.jpg')# img = cv2.imread('/home/stephane/Documents/Bastien_Bearing/Ball_white_paper_without_light.jpg')# img = cv2.imread('/home/stephane/Documents/Bastien_Bearing/2_balls_sticked_with_light.jpg')# img = cv2.imread('/home/stephane/Documents/Bastien_Bearing/many_balls_in_bearing_no_light_good_side.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)# # For Ball_white_paper_without_light.jpg# minDist = 100# param1 = 9 # param2 = 20 # minRadius = 3# maxRadius = 15# # For 2 balls# minDist = 10# param1 = 9 # param2 = 20 # minRadius = 3# maxRadius = 15# For many ballsminDist = 1param1 = 9param2 = 20 minRadius = 1maxRadius = 15circles = 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é.")cv2.imshow('img', img)cv2.waitKey(0)cv2.destroyAllWindows()
I really thought the image with light would be easier to process,Doesn't seem to be the case. Or I should threshold a binary version of my image ?I'd love to have a better result with the precision of multi balls detection, because at the end I could teach my cnc device to add a ball in an empty spot between the two rings of the bearing (at the end 38 balls will touch themselves in a circle)
Thanks a lot for any input/ideas :)A good day to all in all cases !Bastien