Exercise: erosion and dilation
Before this unit: Day 12: Thresholding.
Goal
By the end of this exercise you should be able to:
- compute an erosion and a dilation of a small binary image by hand, as “where the SE fits / hits” and as a min / max over a window,
- explain the role of the origin and of the reflection of an asymmetric SE,
- check the duality between erosion and dilation on an example,
- extract an inner boundary and predict its connectivity, and handle the image border.
Do the pen-and-paper part first, then the demo.
Background
| Idea | What it means |
|---|---|
| Binary image | the set A of foreground (1) pixels; A^c is the background |
| Structuring element (SE) | a small set B of offsets from its origin |
| Erosion A \ominus B | pixels x where B, placed with its origin at x, lies inside A (“fits”); a min filter |
| Dilation A \oplus B | pixels x where the reflected \check B placed at x meets A (“hits”); equivalently, a copy of B on every pixel of A; a max filter |
| Duality | (A \ominus B)^c = A^c \oplus \check B |
| Inner boundary | A \setminus (A \ominus B) |
| Border convention | the value assumed outside the image (0, or 1 = “does not erode”) |
Coordinates: x to the right, y down; offsets are written (\Delta x, \Delta y). Rows and columns are counted from 0, starting top left.
A \ominus B = \{x : x + b \in A\ \forall b \in B\} = \bigcap_{b \in B} A_{-b} and A \oplus B = \{a + b : a \in A, b \in B\} = \bigcup_{b \in B} A_b, where A_z is A shifted by z. As 0/1 functions: (A \ominus B)(x) = \min_{b \in B} A(x + b) and (A \oplus B)(x) = \max_{b \in B} A(x - b).
By hand
Throughout, B is the L-shaped SE of 3 pixels with the origin in the corner: B = \{(0, 0), (1, 0), (0, 1)\} (the origin, the pixel to its right, the pixel below it), and
A = \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix} \quad (17 \text{ pixels}).
B1. Compute A \ominus B: mark every pixel x such that x, its right neighbour and the pixel below it are all in A. How many pixels are left?
B2. Compute A \oplus B in two ways: (a) put a copy of the L on every pixel of A and take the union; (b) for every pixel x, check whether \check B placed at x (the origin, the pixel to the left, the pixel above) meets A. Do you get the same set? How many pixels?
B3. Dilate a single object pixel by B. Then explain in one sentence why “copy B onto every pixel” (B2a) and “look through the SE at each pixel” (B2b) need the reflection \check B to agree.
B4. Check the duality on this example: compute A^c \oplus \check B, assuming that the background continues outside the image (the outside counts as 1 for A^c), and compare its complement with your answer to B1.
B5. The inner boundary. For the disc-like object D = \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix} \quad (21 \text{ pixels}) compute D \setminus (D \ominus \text{cross}) and D \setminus (D \ominus \text{square}), with the 5-pixel cross and the 3 \times 3 square centred on the origin. How many pixels does each boundary have? Which one is 4-connected and which only 8-connected?
B6. An SE without its origin: B' = \{(1, 0)\}. What are A \ominus B' and A \oplus B'? Is A \ominus B' \subseteq A?
B7. The border. A 6 \times 6 image has a 4 \times 4 block of ones in its top-left corner (rows and columns 0–3). Erode it by the 3 \times 3 square (a) with outside = 0 and (b) with outside = 1. How many pixels are left in each case? Which convention would you choose for a cell that is cut by the image edge?
B8. Cost. A 256 \times 256 image is dilated by a 15 \times 15 square. How many values are compared per pixel (a) directly, (b) as a dilation by a row of 15 followed by a column of 15? (c) What does the van Herk / Gil–Werman running maximum change?
B1. 7 pixels: A \ominus B = \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix} The right column of every part goes (no right neighbour in A), and so does the bottom row (no pixel below). For example, row 1, column 4 is removed because its right neighbour is 0; row 4, column 4 survives because row 4, column 5 and row 5, column 4 are both in A.
B2. Both ways give the same 28 pixels: A \oplus B = \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 1 \\ 0 & 1 & 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 & 1 & 0 \end{bmatrix} Every part grows by one pixel to the right and one pixel down; nothing grows to the left or up.
B3. A single pixel at p dilates to \{p, p + (1, 0), p + (0, 1)\}, a copy of the L. In B2b we look from a pixel x: x is covered if some object pixel a has x = a + b, i.e. a = x - b. The pixels x - b for b \in B form the reflected L at x. The flip is the same as between convolution and correlation.
B4. A^c has 49 - 17 = 32 pixels. \check B = \{(0, 0), (-1, 0), (0, -1)\}, so A^c \oplus \check B grows the background by one pixel to the left and one up (and the outside, which counts as background, grows into row 0 and column 0, which are background already). Result (42 pixels): A^c \oplus \check B = \begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 0 & 0 & 0 & 1 & 1 & 1 \\ 1 & 0 & 0 & 1 & 1 & 1 & 1 \\ 1 & 0 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 0 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 & 1 & 1 \end{bmatrix} Its complement is the 7 zeros, at exactly the positions of B1.
B5. Cross: D \ominus \text{cross} is the central 3 \times 3 block (9 pixels), and the boundary has 21 - 9 = 12 pixels; it steps diagonally at the corners (row 1, column 2 to row 2, column 1), so it is only 8-connected. Square: D \ominus \text{square} keeps only the 5 pixels of the central cross, the boundary has 16 pixels, and its corner pixels (row 2, column 2 and so on) make it 4-connected: \text{cross: } \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix} \qquad \text{square: } \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 1 & 0 \\ 0 & 1 & 0 & 0 & 0 & 1 & 0 \\ 0 & 1 & 1 & 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix}
B6. A \ominus B' = \{x : x + (1, 0) \in A\} is A shifted one pixel to the left; A \oplus B' is A shifted one pixel to the right. A \ominus B' \not\subseteq A: for example row 1, column 0 is in A \ominus B' but not in A. “Erosion shrinks” needs the origin in the SE.
B7. (a) Outside = 0: only the pixels whose whole 3 \times 3 neighbourhood lies in the block and in the image survive: rows and columns 1–2, 4 pixels. (b) Outside = 1: the image edge removes nothing, only the block’s inner edges (row 3 and column 3) are eroded: rows and columns 0–2, 9 pixels. For a cell cut by the image edge, outside = 1 (or “ignored”) is the better guess: the cell continues outside, and its true edge is not at the image edge. Outside = 0 is right when the image really ends there (the object is entirely in view).
B8. (a) 225 values per pixel. (b) 15 + 15 = 30, because A \oplus (\text{row} \oplus \text{column}) = (A \oplus \text{row}) \oplus \text{column} and the row dilated by the column is the square. (c) The running maximum computes a max over a line of any length with about 3 comparisons per pixel, so the cost no longer grows with the SE size: about 6 per pixel for the square.
Math questions
B9 (math). Prove A \ominus B = \bigcap_{b \in B} A_{-b} and A \oplus B = \bigcup_{b \in B} A_b, and use them to prove the duality (A \ominus B)^c = A^c \oplus \check B with De Morgan’s laws.
B10 (math). Prove the adjunction A \oplus B \subseteq C \iff A \subseteq C \ominus B. Deduce that (C \ominus B) \oplus B \subseteq C and A \subseteq (A \oplus B) \ominus B for every SE, also one without its origin.
B11 (math). Show that erosion is increasing (A \subseteq A' \Rightarrow A \ominus B \subseteq A' \ominus B) and that A \ominus (B_1 \oplus B_2) = (A \ominus B_1) \ominus B_2.
B9. x \in A_{-b} \iff x + b \in A. So x is in every A_{-b} exactly when x + b \in A for all b \in B: that is the erosion. x \in A_b \iff x - b \in A \iff x = a + b for some a \in A; the union over b is the set of all sums a + b. Duality: (A \ominus B)^c = \big(\bigcap_b A_{-b}\big)^c = \bigcup_b (A_{-b})^c = \bigcup_b (A^c)_{-b} = \bigcup_{b' \in \check B} (A^c)_{b'} = A^c \oplus \check B, using that complement commutes with translation and b' = -b runs over \check B.
B10. A \oplus B \subseteq C says a + b \in C for all a \in A, b \in B. A \subseteq C \ominus B says: for every a \in A, a + b \in C for all b \in B. These are the same statement. With A = C \ominus B (true: A \subseteq C \ominus B) the left side gives (C \ominus B) \oplus B \subseteq C. With C = A \oplus B the right side gives A \subseteq (A \oplus B) \ominus B. No assumption on the origin was used. (At an image border the pair is adjoint when dilation ignores the outside and erosion treats it as 1.)
B11. If B_x \subseteq A then B_x \subseteq A', so every pixel of A \ominus B is in A' \ominus B. For the decomposition: x \in (A \ominus B_1) \ominus B_2 \iff x + b_2 \in A \ominus B_1\ \forall b_2 \iff x + b_2 + b_1 \in A\ \forall b_1, b_2 \iff x + c \in A\ \forall c \in B_1 \oplus B_2 \iff x \in A \ominus (B_1 \oplus B_2).
Explore
The demo steps through erosion and dilation on a small grid. Left: the SE editor (click cells to add or remove them, set origin then click a cell to move the origin, orange) with SE presets, the border convention, and show duality. Middle: the image; click or drag to paint. Right: the operation, the step controls (step, back, play, end, next stage = next row), the log of decisions and the examples. While a pass scans the image, the SE is drawn at the current pixel with a dot on each SE cell (filled = on the set); decisions so far are outlined (red: will be removed, green: will be added). At the end: dark = kept, red = removed, green = added.
E1. Example erosion: where it fits (cross). Before running it, predict which of the three parts survive and how many pixels are left. Then press end. Step back to a pixel at the end of the thin line and read its decision in the log.
E2. Example dilation adds a layer (square). How many pixels before and after? What happens to the hole and to the single pixel at the bottom right?
E3. Example the L-shape: why reflect. Press end: what does each single pixel become? Now step through and watch the SE drawn at the current pixel. Is it the L or its mirror image? Explain.
E4. Example duality: erosion by the L with show duality on. The second grid is (A^c \oplus \check B)^c, computed from the background. Compare it with the result at the end. Switch the border to = 1 and back: does the duality still hold?
E5. Example two lines make a square: a dilation by a row of 3, then by a column of 3. What does the final note say? Draw the SE that the two steps add up to.
E6. Example border convention (erosion by the square). How many pixels are left with outside = 0, and with outside = 1? Which part of the image does the choice affect?
E7. Example 7 × 7 (exercise): check your answers to B1 (erosion) and B2 (choose dilation) with the L.
E1. 42 pixels become 8: the 5 \times 4 rectangle keeps its 3 \times 2 interior (6), the 3 \times 4 block keeps 2 pixels in its middle column, and the one-pixel-wide line disappears completely, because the cross never fits across it. At a line pixel the log reads, for example, “2 of 5 miss → 0”.
E2. 20 pixels become 49. The block grows by one pixel on every side and its one-pixel hole is filled; the single pixel becomes a 3 \times 3 square, which overlaps the grown block in 2 pixels.
E3. Each single pixel becomes an L (origin, right, down): a copy of B. The window drawn at each pixel during the scan is the mirrored L (origin, left, up): dilation asks whether \check B placed at x hits the set, and x is covered exactly when the object pixel x - b exists. The 4 pixels become 9 (one L of 3, and the small L-shaped object grows to 6).
E4. 39 pixels erode to 23, and the second grid is identical (“The dual … is identical: duality”). With outside = 1 it still holds: the dual is computed with the opposite convention for the background (outside = 0), as in B4.
E5. “Same as A ⊕ (3 × 3 square)”: the row of 3 dilated by the column of 3 is the 3 \times 3 square. Here 20 pixels become 49, as in E2.
E6. 36 pixels: 10 are left with outside = 0 and 16 with outside = 1. Only the block in the top-left corner, which touches the image edge, is affected; the block in the middle gives the same result either way.
E7. Erosion: 7 pixels, as in B1. Dilation: 28 pixels, as in B2.
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_binary/exercise.rs.
A mask is an image of true (object) and false (background); an SE is a list of offsets (\Delta x, \Delta y) from its origin. The cross, the 3 \times 3 square, the L of the exercises and digital discs are given.
C1. erode(a, se, outside): keep the pixels where the SE fits; outside the image the value outside is assumed.
for every pixel x:
fits = true
for every offset b in B:
if A(x + b) = 0: fits = false # outside the image: the value "outside"
out(x) = fits
C2. dilate(a, se, outside): the same loops, but x is object if A(x - b) = 1 for some offset b.
The checks are B1 (7 pixels), B2 (28 pixels), B3, the duality of B4 (also on a random image), B6 and B7 (4 and 9 pixels), and “a row of 3, then a column of 3 = the 3 \times 3 square”.
C3. inner_boundary(a, se): the pixels of A that are not in A \ominus B (outside the image counts as background). The check is B5: 12 pixels with the cross, 16 with the square.
Then cargo run -p morphology -- morph-binary and open the pictures in out/morph-binary/. The program thresholds a noisy image of synthetic nuclei (mask.png: specks in the background, holes in the nuclei), erodes and dilates it with discs of radius 1 and 3, draws the inner boundaries of a clean mask in red, and times a 15 \times 15 square.
C4. Look at erode-r1.png, erode-r3.png, dilate-r1.png and dilate-r3.png, and at the printed pixel counts. What happens to the specks, to the holes and to the nuclei?
C5. The 15 \times 15 square directly and as a row of 15 followed by a column of 15: how many comparisons per pixel does each need (B8)? Compare with the two printed times.
The model solution is morphology/src/morph_binary/solution.rs in the download.
C4. The mask has 10 875 pixels. Erosion leaves 8365 (radius 1) and 4704 (radius 3): the specks disappear, but the holes in the noisy nuclei grow, and with radius 3 the dim, holey nuclei break into fragments or vanish. Dilation gives 13 895 and 22 877 pixels: the holes close, but every speck grows into a copy of the disc, and nuclei that were close merge. Neither alone cleans the mask; the next unit combines them.
C5. 225 against 15 + 15 = 30 values per pixel. In our runs the direct dilation took 30 to 50 ms and the two lines about 7 ms (the times vary from run to run and between computers). The saving is less than 225/30 = 7.5 times because the model solution stops looking as soon as it finds an object pixel, which helps the big square most.
Where this leads
Erosion removes small things but shrinks everything else; dilation fills small gaps but grows everything. The next unit combines them: an opening (erode, then dilate) removes what is smaller than the SE and gives the rest its size back, and a closing does the same for the background. The same unit introduces the distance transform, which turns erosion by a disc of any radius into a single threshold.