I have a transparent plastic tube that shall contain exactly 10 black little balls. But sometimes there will be 11 or 9 pills in there. Is there any way to detect the number of balls in the tube.
Now with cv2
, the best I can do is the following:
import cv2
import numpy as np
original = cv2.imread("d.jpg", cv2.IMREAD_GRAYSCALE)
retval, image = cv2.threshold(original, 50, 255, cv2.THRESH_BINARY)
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
I get a black and white image for a better contrast.
I tried to count the number of black pixels then divide it by a number to get the number of balls. But since there are many balls overlapping each other, it doesn't work well no matter how I tune that number.
Is there any other way to count them.
Here are more examples: