Since I am not a favorite of providing of the shelf solutions but rather insights on how to tackle this problem, I will try to explain how I would go ahead with this problem.
- Perform a grayscaling and histogram equilization of the input image.
- Perform a thresholding on the image, based on the background color range to retrieve a binary blob image.
- Since the binary image will also contain the lines on the paper (no way of avoiding lines but keeping black pills) I would perform a erosion operator on the image then a dilation to grow the pill regions back.
- You will now have blobs that perfectly describe regions of pills.
- You are now able to perform a contours finding function, resulting in the amount of pills inside the image.
- If preferred you could set a region of interest to the seperate pills and use a classifier to decide which kind of pill you are dealing with.