##### Tools

This site is devoted to mathematics and its applications. Created and run by Peter Saveliev.

# Gray scale watershed

Suppose we have a function f(x,y) and a surface that is the graph of f. Next, we flood the valley and build dams so that we don’t allow the water to flow from one valley to another. These dams will break the image into regions each containing a single valley.

In the standard settings when f(x,y) is the gray scale function of the image (in the example on the right f(x,y)=sin(x)sin(y)), what is captured by these dams are the dark spots. Then the light areas are the peaks and the dark areas are the valleys. Then, for example, if you have an image of dark cells on a light background, you’ll have the cells enclosed inside these regions.

What if the cells are light and the background is dark? Then you'll have to turn everything upside down. For example, you can replace the surface given by f(x,y) with the one given by 255-f(x,y). Not a problem but inconvenient. Why? Because you have to tell your program what you are looking for: light on dark or dark on light. It is better if you don’t have to use a priori information so that the analysis is independent of context, as much as possible.

This minor inconvenience becomes a problem when the image contains both kinds on features. The first and second images below are what watershed should be theoretically and the third is the watershed segmentation produced with ImageJ.

Original image.
Watershed segmentation.
Watershed segmentation.
Watershed with ImageJ

One can find other uses of watershed in image analysis by choosing something else for f(x,y).

One is the magnitude of the gradient of the gray level at (x,y). Then the areas with the highest values of f are the ones that have the fastest change of the gray level, i.e., high contrast. This will give you something that resembles the edges of objects. As a result both dark and light objects can be dealt with simultaneously by looking for the maxima of f. Which one is which is however lost and you have to go back to the gray scale function to sort this out. The approach has another drawback. As the derivative is involved, the output is more affected by noise than the original function. For better results, smoothing of the image prior to analysis may be required. To determine the proper amount of smoothing, you need to analyze the image first…

As a reminder, this is how Pixcavator handles the above example.

An example of the output of Pixcavator.
Another example of the output of Pixcavator.

There are some similarities to the watershed algorithm in the sense that both look for maxima and minima of the gray scale function. The difference comes from the main idea of topology which is that there are objects and holes in them, and there is nothing else. In case of gray scale, this means that there are dark objects on light background with light holes in them, or vice versa.

Another example is binary watershed. In a binary image you set: f(x,y) = the distance from (x,y) to the nearest black pixel.

Another choice for f(x,y) is the variance, below.

Many examples of image analysis don't require watershed, but some do.