title: How to count tablets successfully?
body: <p>My last question on image recognition seemed to be too broad, so I would like to ask a more concrete question.</p>
  <p>First the background. I have already developed a (round) pill counter. It uses something similar to <a href="https://docs.opencv.org/3.2.0/d3/db4/tutorial_py_watershed.html" rel="nofollow noreferrer">this tutorial</a>. After I made it I also found something similar with <a href="https://www.pyimagesearch.com/2015/11/02/watershed-opencv/" rel="nofollow noreferrer">this other tutorial</a>.</p>
  <p>However my method fails for something like this image</p>
  <p><a href="https://i.stack.imgur.com/E31kX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E31kX.jpg" alt="The original problem" /></a></p>
  <p>Although the segmentation process is a bit complicated (because of the semi-transparency of the tablets) I have managed to get it</p>
  <p><a href="https://i.stack.imgur.com/JEr6Y.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JEr6Y.jpg" alt="segmented image" /></a></p>
  <p>My problem is here. How can I count the elongated tablets, separating each one from the image, similar to the final results in the linked tutorials?</p>
  <p>So far I have applied distance transform and then my own version of watershed and I got</p>
  <p><a href="https://i.stack.imgur.com/tHbpg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tHbpg.jpg" alt="bad results" /></a></p>
  <p>As you can see it fails in the adjacent tablets (distance transform usually does).</p>
  <p>Take into account that the solution does have to work for this image and also for other arrangements of the tablets, the most difficult being for example</p>
  <p><a href="https://i.stack.imgur.com/aE0rs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aE0rs.jpg" alt="difficult case" /></a></p>
  <p>I am open to use OpenCV or if necessary implement on my own algorithms. So far I have tried both (used OpenCV functions and also programmed my own libraries) I am also open to use C++, or python or other. (I programmed them in C++ and I have done it on C# too).</p>