Getting to grips with the watershed

Learn how to performs basic watershed segmentation with Pink

Problem 1 Uranium oxide

  1. Segment all the cells of the attached uranium oxide image.
  2. Remove the cells that touch the frame of the image.
  3. Give some statistics about the cells (number, average size, etc.).

Tools

- pgm2GA - GAwatershed
- GA2khalimsky - frame
- nbcomp - area

Hint: you can use the How to Watershed with PINK section below to find some useful pink commands :wink:

Problem 2 Segmentation of red blood cells

  1. Segment all the red blood cells in the attached bloodCells image.
  2. Remove the ones that touch the edge of the image.
  3. Separate the cells that are touching each other.
  4. Give some statistics about the globules (number, average size).

Hint: step 3 is tricky. It needs the notion of a distance map of a binary object.

How to Watershed with PINK

Key instructions

# To transform an image into a weighted edge graph:
pgm2GA im.pgm 0 relief.ga
TIP

The second argument (here 0) indicates how we proceed to weight the edges (maximum / minimum or intensity difference between pixels at the ends of each edge).

Is the choice of one weighting function more relevant than another? In which cases? What difference could it make to the output?

# To calculate a Watershed Line of a weighted edge graph
GAwatershed relief.ga watershed.ga

# To visualize the result, we switch to the Khalimsky grid
GA2khalimsky watershed.ga 0 watershed.pgm
TIP

To superimpose a segmentation (in the form of a binary image) on a grayscale image, we can use surimp be careful the two images must be the same size.

To resize an image to the desired size we can use zoom or make the image img.pgm undergo a transition to a weighted graph and then to the Khalimsky grid.

pgm2GA img.pgm 0 img.ga
GA2khalimsky img.ga 0  imgRightSize.pgm

Distance map

Basically, the distance map to a subset \(X\) of the space \(E\) is the image \(D_X\) on \(E\) defined, for every pixel \(x\) in \(E\) by \(D_X(x) = \min \{ \, d(x,y) \, | \, y \in X \, \}\) where \(d(x,y)\) denotes the (Euclidean) distance between \(x\) and \(y\).

The the distance map to \(X\) is an image where the value of each pixel \(x\) is the distance from this point to the closest point which belongs to the set \(X\).

To obtain the distance map of an object in Pink you can use the command dist for example with the command:

dist X.pgm 1 distMapToX.pgm

If needed you can convert the resulting image to a byte image (i.e., an image with values between 0 and 255) with the command:

long2byte distMapToX.pgm  1 distMapToX_Byte.pgm
WARNING

If you calculate the map distance on an image that is too large, then the byte conversion will not work. The distances will be too large, resulting in an integer overflow.

Threshold an image, interactively

Remind of the fantastic interactive seuil.tcl.

Filter an image…

Compute statistics

To obtain a black image with a white border

Try frame.