Watershed Compared to Houghs Transform
The Watershed algorithm is compared to using the Hough transform to detect circles. In doing so an inherent weakness was found with the Watershed algorithm for this particular application. Specifically, I am using HoughCircles in OpenCV and for the watershed approach I'm using distance_transform_edt from scipy and watershed from skimage.morphology.
To show the comparison I am using an ideal image constructed in code. Below is the image of two overlapping filled circles and the distance transform.Â
Two Solid Circles
The large circle has a radius r = 60 and the max distance is 60.03. It can be seen there are two local maxima (bright spots) and the code found two distinct segments.
Object Euclidean distance map
                                Â
Connected Segments
OpenCV findContours found the two circles in the watershed connected labels even though one is partially occluded. However, if the second circle has a hole in the center the algorithm breaks. This can occur if the second object is a washer. The images are repeated below for this second case.
Circle with Cutout
The smaller circle has a cut-out of radius 10 at the center. Â In this case the distance measurement has one strong maximum but many weak maxima. In fact the code found 12 segments.
Object Euclidean distance map
                                 Â
Connected Segments
As the min_distance parameter of the peak_local_max is increased the number of segments found reduces until finally the total combined contour of both objects is one segment. In summary, for identifying coins the watershed algorithm works for partially occluded coins but if a false coin is present the algorithm breaks. Â The following images are for the false coin case but uses instead HoughCircles to segment the two objects.
Hough Circle:
For this case the primary circle has a radius of 60 and the secondary has a radius of 40 with a cut-out in the center with radius 10. The HoughCircles param1 = 100, param2=15, min_radius=15 and max_radius=80. The detected radii are 60 and 38. In order to reject the circle with a cut-out secondary processing besides diameter is needed such as checking the center color against the mean color.
The following images do show an example of the watershed algorithm working properly with an easy case of non-touching coins and no false coins. The background in this case is a black velvet. Twelve (12) objects are found without error. This case was also using the ID-000 (sim card) as a reference dimension.
Threshold Blurred
                             Â
      Â
Results
Summary
The Watershed algorithm works on distinct objects but fails when objects touch or overlap. With multiple touching objects the individual objects morph into a contour that is non-circular.
I am planning another blog that will cover the coin detection algorithm in more detail.
The Python code for identifying coins using these two methods are located at my Bitbucket repository .