CS4495/7495
Final Project:
Counting with Watershed Segmentation
By Justin Jang


Intoduction

The goal of this project is to compare the use of binary vs. grayscale morphological operators when used in conjunction with watershed segmentation to segment out and count items in an image.

Euclidean Depth Map

The Euclidean Depth Map (EDM) for a binary image is an image where each pixel location contains the distance of that pixel to the closest pixel with value 1 in the binary image. My algorithm involvs pre-computing a generic depth map of twice the image width and height. The appropriate window of this is selected for each pixel, and aligned at that pixel. The values of pixels across this alignment that are smaller than the current distance stored at that pixel are updated with the generic distance map value.

Watershed Segmentation

To get the idea of Watershed segmentation one can think of two different ways of doing it. One way is to imagine rain falling into a bunch of valleys. As the water fills, individual catchment basins form. As catchment basins rise and are about to touch, a dam in build straight up to prevent mixing basins. The other is to imagine a tub with many valleys in it and holes at every local minimum. As the tub is dipped into water, the basins fill from the bottom. Dams are built in the same way as the rain fall analogy.

Top-hat and Bot-hat transforms

The Top-hat is defined as the image minus the opening of the image.
The Bot-hat is defined as the closing of the image minus the image.


MATLAB Code



1. The Binary Morphology-Based Approach

The script for this part is test_1.m.

The basic algorithm is to threshold the grayscale values to get a binary image of the stuff we want to count. The morphological close and open operators are then applied to this image in succession and this binary image is inverted. From here, the Euclidean Distance Map is computed and inverted resulting in an image with valleys where the object we want to count are and with plateaus where non-counting-matter is. We apply the watershed segmentation to this valley image to uniquely label each segment. The number of segments can easily be counted by getting the max label value in the segmented image.

Original color imageGrayscaleHistogram equalized
ThresholdedBinary morphological opsEDM
Watershed segmentation
Thresh = >140
close then open
Count = 19
Original color imageGrayscaleHistogram equalized
ThresholdedBinary morphological opsEDM
Watershed segmentation
Thresh = >100
close then open
Count = 118
Original color imageGrayscaleHistogram equalized
ThresholdedBinary morphological opsEDM
Watershed segmentation
Thresh = >190
close then open
Count = 134
Original color imageGrayscaleHistogram equalized
ThresholdedBinary morphological opsEDM
Watershed segmentation
Thresh = >80
close then open
Count = 51
Original color imageGrayscaleHistogram equalized
ThresholdedBinary morphological opsEDM
Watershed segmentation
Thresh = >70
close then open
Count = 85
Original color imageGrayscaleHistogram equalized
ThresholdedBinary morphological opsEDM
Watershed segmentation
Thresh = >140
close then open
Count = 82
Original color imageGrayscaleHistogram equalized
ThresholdedBinary morphological opsEDM
Watershed segmentation
Thresh = >140
close then open
Count = 94

The results were mediocre. The segmentation seemed to have trouble anywhere there was a concavity in the binary image. The EDM causes concavities to act like a pinch point to divide a segment, and the watershed segmentation brings this out.

It would probably be better to just stick with binary morphology all the way once the image is thresholded. One could erode with a structuring element that mimicked the shape of the objects to count.

The EDM + watershed segmentation method does have application though. For well thresholded images of concave objects, e.g. coffee beans on a white table, the EDM + watershed segmentation operates quite nicely.


2. The Grayscale Morphology-Based Approach

The script for this part is test_2.m.

The basic algorithm is to perform grayscale morphology to create the valley images. In particular, the tophat transform and bothat transform are used. The tophat is added to the image and then the bothat is subtracted from the result. The inversion of this gives us a valley image on which the watershed segmentation is applied. The segments can then be counted.

Original color imageGrayscaleTop-hat
Bot-hatPlus Top-hat Minus Bot-hatInverted
Watershed segmentation
Count = 1285!

Oversegmentation occurs because many local minima are causing the creating of many spurious basins. One way to deal with this is to calculate a set of starting basin pixels and only allow the segmentation to build off of those initial basins.

The structuring element for the tophat and bothat transforms is a 13 x 13 disc.


Extensions and Future Work


Links: