Image segmentation: binary watershed
According the ImageJ site: “Watershed segmentation is a way of automatically separating or cutting apart particles that touch”.
Suppose black is the particles and white is the background. The the procedure is fairly simple.
First, for each pixel compute the distance to the nearest white pixel. This is called the distance function. It’s a scalar function of two variables.
Next, find the maximum points of this function. Each of these pixels will become the center of a particle.
You carry out multiple rounds of dilation that gradually grow these particles. The dilation has two restrictions. First, the particles aren’t allowed to grow beyond the original set of black pixels. This way we guarantee that we end up with the same set of pixels except it has been “cut” into pieces. Second, a new pixel isn’t added if it’s adjacent to a pixel that belongs to another particle. This way the particles start to “push” onto each other but never overlap.
The tricky part of the last restriction is that the growth rate will have to be different for particles of different sizes. Otherwise, two particles will always be separated by a cut exactly half way between their centers. That wouldn’t make sense if one is significantly larger than the other. Roughly, the dilation rate should be proportional to the value of the distance function.
Some questions remain. For example, how does one efficiently find the maxima? Everything is discrete, so forget about partial derivatives etc. You have to visit every point.
How does one deal with particles that are simply noise? If you remove all small particles, you may have nothing left. One answer is to discard the maxima with low values of the distance function.
Another issue is typical for many image analysis techniques. Once again to quote the ImageJ site, “Watershed segmentation works best for smooth convex objects that don’t overlap too much.” Basically, you have to view (analyze!) the image yourself and decide ahead of time whether the method is appropriate. There is a good reason to be cautious – non-convex particles may cause the watershed method to produce undesirable results.
You have to choose ahead of time whether you have white or black particles. If you don’t do it correctly, you end up with non-convex black “particles”. The result of watershed segmentation isn’t what you expect:
It is also easy to think of an image (rings) that can’t possibly be analyzed correctly by watershed regardless of the black/white choice:
Needless to say that the topological method produces the correct segmentation here:
It can’t however separate particles yet (the stuff will appear in the wiki under Robustness of topology).