Exercise: grey-level morphology, top-hat and gradient

Goal

By the end of this exercise you should be able to:

  • compute grey-level erosion, dilation, opening and closing of a short signal with a flat SE, and predict which peaks and valleys survive,
  • use the white and black top-hat to remove an uneven background, and choose the SE size,
  • compute a morphological gradient and compare it with a derivative,
  • compare the rank filters min, median and max on impulse noise.

Do the pen-and-paper part first, then the demos.

Background

Idea What it means
Flat SE of width w a window of w samples (or a disc in 2D), no weights
Erosion / dilation min / max over the window
Opening / closing erode then dilate / dilate then erode; opening \le I \le closing
White top-hat I - I \circ B: bright details narrower than B
Black top-hat I \bullet B - I: dark details narrower than B
Gradient (I \oplus B) - (I \ominus B); inner I - (I \ominus B), outer (I \oplus B) - I
Rank filters sort the window, take the smallest (erosion), middle (median) or largest (dilation)

In 1D the window of width 3 at sample x is \{x - 1, x, x + 1\}; at the two ends only the samples inside the signal count.

For a flat SE, \{I \ominus B \ge t\} = \{I \ge t\} \ominus B for every t (and the same for dilation, opening and closing): grey-level flat morphology is binary morphology on every level set, stacked again. Non-flat SEs s(b): (I \oplus s)(x) = \max_b I(x - b) + s(b), a convolution with (\max, +) in place of (+, \times).

By hand

B1. I = 2\,2\,7\,2\,3\,3\,6\,6\,6\,1\,2\,2 (samples 0–11). With a flat SE of width 3, compute the erosion, the dilation, the opening and the white top-hat. Which bright detail does the top-hat show?

B2. Repeat the opening and the white top-hat with width 5. Which peaks survive now? What is the rule?

B3. With width 3, compute the closing and the black top-hat of I. Which dark detail is found? Why is the dip at sample 3 (value 2 between 7 and 3) only 1 deep in the black top-hat?

B4. Gradient. With width 3, compute the gradient, the inner and the outer gradient of the step 0\,0\,0\,0\,10\,10\,10\,10, and the gradient of the ramp 0\,2\,4\,6\,8\,10\,12\,14. Where is each half of the step’s gradient? What is the gradient of a ramp with slope a and width w, away from the ends?

B5. Impulse noise. J = 5\,5\,9\,5\,5\,0\,5\,5\,5\,9\,0\,5. Compute the min, the max and the median filter (width 3). Then compute the opening, the closing, and the closing of the opening. Which filter removes all impulses?

B6. Choosing the SE. Bright spots have a radius of about 3 pixels; the background brightness changes smoothly over about 60 pixels, and there are bright cells of radius 15 that you want to ignore. Which disc radius do you use for the white top-hat? What goes wrong with radius 1, and with radius 40?

B7. On the raw image of B6, spots on the dark side have a peak brightness of 90 and the background on the bright side is 150. Explain why no threshold on the raw image separates spots from background, and why one does on the top-hat.

B1.

sample 0 1 2 3 4 5 6 7 8 9 10 11
I 2 2 7 2 3 3 6 6 6 1 2 2
erosion 2 2 2 2 2 3 3 6 1 1 1 2
dilation 2 7 7 7 3 6 6 6 6 6 2 2
opening 2 2 2 2 3 3 6 6 6 1 2 2
white top-hat 0 0 5 0 0 0 0 0 0 0 0 0

The one-sample peak at sample 2 (height 5 above its surroundings) is the only bright detail narrower than 3; the plateau 6\,6\,6 is exactly 3 wide and survives.

B2. Width 5: opening = 2\,2\,2\,2\,3\,3\,3\,3\,3\,1\,1\,1, top-hat = 0\,0\,5\,0\,0\,0\,3\,3\,3\,0\,1\,1. Now the plateau 6\,6\,6 is cut down to 3 (the level at which the profile becomes at least 5 wide: samples 4–8 are all \ge 3). The rule: a peak survives up to the level at which it is at least as wide as the SE. (The last two samples, 2\,2 at the end of the signal, are a 2-wide plateau and are cut to 1.)

B3. Closing = 2\,2\,7\,3\,3\,3\,6\,6\,6\,2\,2\,2, black top-hat = 0\,0\,0\,1\,0\,0\,0\,0\,0\,1\,0\,0. The one-sample dip at sample 9 (value 1 between 6 and 2) is filled up to 2, the lower of its two sides, so it shows up as 1. The same holds at sample 3: the closing fills a dip only up to the lower side of the window, here 3 (sample 4), not to the peak at 7.

B4. Step: gradient 0\,0\,0\,10\,10\,0\,0\,0; inner gradient 0\,0\,0\,0\,10\,0\,0\,0 (on the bright side, inside the object); outer gradient 0\,0\,0\,10\,0\,0\,0\,0 (on the dark side). Ramp: gradient 2\,4\,4\,4\,4\,4\,4\,2 (4 = twice the slope; at the ends the window has only 2 samples). In general, (w - 1)\,|a| for a window of w samples.

B5. Min: 5\,5\,5\,5\,0\,0\,0\,5\,5\,0\,0\,0 (the 9s are gone, the 0s are spread). Max: 5\,9\,9\,9\,5\,5\,5\,5\,9\,9\,9\,5 (the opposite). Median: all 5. Opening: 5\,5\,5\,5\,5\,0\,5\,5\,5\,5\,0\,0 (bright impulses gone, dark ones kept, and the last sample 5 at the end is lost too). Closing: 5\,5\,9\,5\,5\,5\,5\,5\,5\,9\,5\,5 (dark impulses filled, bright kept). Closing of the opening: 5\,5\,5\,5\,5\,5\,5\,5\,5\,5\,0\,0: the 0 next to the 9 at the end is not repaired, because the opening first turned “9 0 5” at the end into “5 0 0”. Only the median removes all impulses here.

B6. A radius between the spots and the structures to ignore: about 5–8 (the disc must not fit into a spot, so radius > 3; it must fit into the cells, radius < 15, or they show up in the top-hat as well; and it must follow a background that changes over 60 pixels). With radius 1 the disc fits into the spots: the opening keeps most of each spot and the top-hat only shows its tip. With radius 40 the disc no longer fits into the cells (they appear in the top-hat) and cannot follow the background curvature, so background leaks in.

B7. The dim spots (90) are darker than the background on the bright side (150): any threshold either misses them (above 90) or includes the bright background (below 150). The top-hat subtracts the local background estimate, so every spot becomes “height above its own surroundings” and the background becomes about 0 everywhere; a threshold between the noise level and the smallest spot height then works.

Math questions

B8 (math). Prove the threshold decomposition for the opening: \{I \circ B \ge t\} = \{I \ge t\} \circ B, using the one for erosion and the analogous one for dilation.

B9 (math). Show that the white top-hat is unchanged when a constant is added to I, and is zero on a 1D linear ramp I(x) = ax away from the ends (flat SE of half-width k).

B10 (math). For non-flat SEs s_1, s_2 (defined on finite sets of offsets), show that (I \oplus s_1) \oplus s_2 = I \oplus (s_1 \oplus s_2) with (s_1 \oplus s_2)(c) = \max_{b_1 + b_2 = c} \big(s_1(b_1) + s_2(b_2)\big). What do you get for two flat segments of widths w_1 and w_2?

B11 (math). Show that the gradient with a flat SE that contains the origin is the sum of the inner and outer gradients and that both are \ge 0. Is the gradient of -I the same as that of I?

B8. For dilation: \max_b I(x - b) \ge t \iff I(x - b) \ge t for some b \iff x \in \{I \ge t\} \oplus B. Then \{(I \ominus B) \oplus B \ge t\} = \{I \ominus B \ge t\} \oplus B = (\{I \ge t\} \ominus B) \oplus B = \{I \ge t\} \circ B.

B9. \min_b (I(x + b) + c) = \min_b I(x + b) + c and likewise for max, so (I + c) \circ B = I \circ B + c and (I + c) - (I + c) \circ B = I - I \circ B. For I(x) = ax with a > 0: the erosion is \min_{|b| \le k} a(x + b) = a(x - k), and the dilation of that is \max_{|b| \le k} a(x - b - k) = ax. So I \circ B = I and the top-hat is 0 (for a < 0 the same with the roles of \pm k swapped). Near the ends the window is cut and this fails, as in B4.

B10. \big((I \oplus s_1) \oplus s_2\big)(x) = \max_{b_2}\big(\max_{b_1} I(x - b_2 - b_1) + s_1(b_1) + s_2(b_2)\big) = \max_c \big(I(x - c) + \max_{b_1 + b_2 = c}(s_1(b_1) + s_2(b_2))\big) with c = b_1 + b_2. Two flat segments (all heights 0) of widths w_1 and w_2 give a flat segment of width w_1 + w_2 - 1: a large window can be built from small ones, as with the two lines in the first unit of this day.

B11. (I \oplus B) - (I \ominus B) = \big((I \oplus B) - I\big) + \big(I - (I \ominus B)\big); with 0 \in B, I \ominus B \le I \le I \oplus B, so both parts are \ge 0. For -I: \max(-I) = -\min I, so (-I) \oplus B = -(I \ominus \check B) and (-I) \ominus B = -(I \oplus \check B); the gradient of -I is (I \oplus \check B) - (I \ominus \check B), the same as for I when B is symmetric. The gradient has no sign: it cannot tell a rising from a falling edge.

Explore: rolling the SE along a profile

A profile of 120 samples. The orange bar is the SE at the chosen position, pushed up under the profile as far as it goes (roll under: opening) or down over it (roll over: closing); sweep moves it along and leaves its trace, which is the opening or closing. Tick the traces to show: erosion, dilation, opening, closing, white and black top-hat, gradient, median, and the size of the derivative. parabola replaces the flat bar by a non-flat cap. Drag the round handles to change the profile.

M1. Example narrow peak goes, wide peak stays (w = 11). Press sweep and watch the trace. Then change w: at which widths is the narrow peak (top 200) cut to 165, 130 and 95, and from which width is it gone? Up to which width does the plateau keep its height 190?

M2. Same example, parabola (non-flat) with w = 11. Compare the height of what is left of the narrow peak with the flat bar. Why is it different?

M3. Example peaks on a slope: top-hat. Compare the opening (orange) and the white top-hat (green) with the profile. Is the top-hat baseline flat? What happens to the top-hat if you make w smaller than the peaks (try 3)?

M4. Example noise: min, median, max (w = 5). Describe what each filter does to the step and to the impulses. Which one would you use before thresholding at 125?

M5. Example gradient vs derivative (w = 3). Compare the gradient and |derivative| at the step and on the ramp. Then set w = 9.

M6. Example a line across the cell (closing from above, black top-hat). Which features of the profile give large black top-hat values?

M1. The narrow peak rises from 60 to 200 and back over samples 24–32. With width 3 it is cut to 165, with 5 to 130, with 7 to 95, and from width 9 on it is gone (cut down to 60): its base is 9 samples wide. The plateau (190, samples 72–94, 23 samples at the top) keeps its height up to w = 23; at w = 25 it is cut to 177.

M2. With the parabola (w = 11) about 98 is left of the narrow peak (flat bar: 60, i.e. nothing): the cap is lower at its ends, so it can push further into a narrow peak before its ends touch the sides. The plateau stays at 190. A non-flat SE cuts peaks less abruptly.

M3. The opening follows the slope and the broad background, and the top-hat sits on a flat baseline near 0 with the four narrow peaks on it, whatever the background level under them. With w = 3 the bar fits into the peaks: the opening follows them and the top-hat keeps only their tips.

M4. The min filter removes the bright impulses but widens the dark ones to 5 samples and moves the step edge by 2 samples to the right; the max does the opposite. The median removes both kinds of impulse (none is the middle value of 5) and keeps the step where it is. Before thresholding at 125 the median is the right choice: it gives a clean step at the true position.

M5. At the step (50 → 200 over two samples) the gradient reaches the full step height 150 and is 3 samples wide; |derivative| peaks at 75. On the ramp the gradient is 9.33, twice |derivative| (4.67). With w = 9 the gradient is 9 samples wide at the step and 37.3 on the ramp; the derivative is unchanged: the gradient measures the range in the window, not the slope.

M6. Narrow dark features: the dark granules and the dark membrane lines, which the closing (a bar pushed down from above) bridges over. Wide dark regions (the nucleus interior, which is darker over a long stretch) are followed by the closing and give small values.

Explore: top-hat on an uneven background

Three panels: the input, the result of the chosen operation with a flat disc of radius r (opening/closing shown on the input’s grey scale, the others stretched from 0), and the threshold overlay (orange = above t). The threshold is applied to the input or to the result. For the synthetic image (40 bright spots and 25 dark specks on a sloping, vignetted background with noise) the readout counts how many true spots (or specks, for the black top-hat) are found, and how many objects above the threshold contain none (“false”). Drag the ends of the orange line on the input to see the profile under it below.

T1. Example one threshold fails (threshold on the input, t = 150). How many spots are found, and how many false objects? Try t = 100 and t = 170. Is there a threshold that finds all 40 spots and nothing else?

T2. Example top-hat flattens the background (white top-hat, r = 6, threshold on the result at 28). Read the counts. Drag the profile line across a few spots: what does the opening (orange) do?

T3. Example SE too small: spots shrink (r = 2). How many spots are found? Look at the result panel and the profile: why are some spots lost?

T4. Example SE too large: background leaks in (r = 20). How many false objects? Where are they?

T5. Choose black top-hat on the synthetic image, r = 3, threshold on the result at 28. How many of the 25 dark specks are found?

T6. Example granules in the cell (black top-hat, r = 4, t = 80). What is found? Is the count a count of granules?

T7. Example morphological gradient vs Sobel (nuclei, r = 1). Compare with Sobel, and with the gradient at r = 3. Which outlines are thicker, and which are more uniform around a nucleus?

T1. At t = 150: 29 of 40 spots and 250 false objects (patches of the bright background and its noise). At t = 100 all 40 are found, but with 148 false objects (the bright side of the background is above 100); at t = 170 only 19 spots, 2 false. The background ranges from about 41 to 159 and the spot peaks from about 95 to 226, so the ranges overlap: no single threshold works.

T2. All 40 spots found, 0 false objects. Along the profile the opening runs just under the background and cuts each spot off at its base: the top-hat is the spot alone, on a baseline near 0.

T3. 37 of 40 found, 0 false. The disc of radius 2 fits partly into the spots (Gaussian profiles with \sigma = 1.6), so the opening keeps their lower part and the top-hat only their tips; the three weakest spots stay below 28.

T4. 188 objects: all 40 spots, plus 148 false objects spread over the background. A disc of radius 20 sits lower in the noise (the minimum over a large disc is more negative) and cannot follow the background where it curves (the bright patch and the vignetted corners), so background and noise rise above 28 in the top-hat.

T5. All 25 specks, and no false objects (25 objects): the closing fills the dark dips, the black top-hat shows them as bright spots on a flat background.

T6. 1370 objects: the small dark granules, but also fragments of the dark membranes and the fine texture of the micrograph, which are all dark details narrower than the disc. It is not a count of granules; that would need a size or shape filter (the labelling day) on top.

T7. At r = 1 the gradient is a thin outline, about as thick as Sobel’s, and of the same strength all around each nucleus (Sobel’s magnitude is also isotropic but depends on the slope). At r = 3 the outlines are about 6 pixels thick: the gradient is the range over the disc, so it widens with r.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in morphology/src/morph_grey/exercise.rs.

A flat SE is a list of offsets; discs and the 1D window of w samples are given. At the image border only the pixels inside the image count, as in the exercises. The 1D signals of the by-hand part are images of height 1.

C1. erode(img, se) and dilate(img, se): the min and the max filter.

for every pixel x:
    E(x) = smallest of I(x + b) over the offsets b in B     # only b with x + b inside
    D(x) = largest  of I(x − b) over the offsets b in B

The check is B1.

C2. open, close, white_tophat (I - I \circ B) and black_tophat (I \bullet B - I). The checks are B1, B2, B3, and B9 (a constant and a ramp do not change the top-hat).

C3. gradient(img, se): dilation minus erosion. The check is B4 (also the inner and outer gradient).

C4. median(img, se): sort the values in the window and take the middle one (of an even number, the upper middle one). The check is B5, with the min, max, opening and closing.

Then cargo run -p morphology -- morph-grey and open the pictures in out/morph-grey/. The program makes 40 bright spots and 25 dark specks on a sloping, vignetted background with noise (spots.png). It thresholds the input, and the white top-hat with discs of radius 2, 6 and 20, and counts the spots found (their centre is above the threshold) and the false pixels (above the threshold but more than 4 pixels from every spot). It also adds salt-and-pepper noise to the micrograph and filters it.

C5. Is there a threshold of the input that finds all spots and nothing else? Compare with the top-hat with radius 6 (tophat-r6-threshold.png, and the opening in opening-r6.png).

C6. What goes wrong with radius 2 and with radius 20? Relate it to B6.

C7. Compare cell-min.png, cell-median.png and cell-max.png (filtered with the disc of radius 1, five pixels) with cell-salt-pepper.png. Which filter removes both kinds of impulses? (B5)

The model solution is morphology/src/morph_grey/solution.rs in the download.

C5. No. At 0.40 all 40 spots are found but 39 984 background pixels too; at 0.50, 39 spots and 22 631 false pixels; at 0.60, 27 spots and still 3216 false pixels: the bright side of the background is brighter than the dim spots (B7). The opening with radius 6 is the background without the spots; the white top-hat thresholded at 0.10 finds all 40 spots and no false pixel.

C6. Radius 2: 37 of 40 spots, no false pixels. The disc partly fits into the spots, so the opening keeps their lower part and the top-hat only their tips; the three weakest stay below 0.10. Radius 20: all 40 spots but 289 false pixels, in a few patches (near the top edge and in the top-right and bottom-right corners): the large disc cannot follow the background closely there, so background and noise rise above 0.10 in the top-hat.

C7. The min filter removes the white impulses but turns every black one into a black cross; the max filter does the opposite. The median removes both, and the image looks almost like the original.

Where this leads

Opening and top-hat filter by size, but the survivors are changed: a peak that is kept is still flattened where the SE does not fit, and an object that is removed cannot be brought back. The next unit introduces morphological reconstruction: keep every object (or every peak) that contains a marker, exactly as it was. Its grey-level version, the h-minima, is what the watershed day uses to choose markers. The top-hat and the maximum filter come back on the point-detection day (day 20), to flatten the background and find local maxima.