Full search page →

IP 09 · Which shapes does a structuring element keep?

IntermediateIP 09 · Morphological Image ProcessingImage lab

Description

Clean a thresholded photo of coins that carries specks, scratches and holes. Change the operation, the shape and the size of the structuring element, and see what each choice removes, fills or keeps.

Try this: With opening and a disk of size 4, all clutter goes. Now switch the element to a horizontal line of size 4: which pieces survive, and why? Then raise the disk size with opening until a coin disappears. Finally switch to closing: what does it fix that opening cannot?

Predict first

Open the mask with a disk of radius 4. Every speck and scratch is thinner than the disk (at most 7 pixels wide), and every coin is far wider. What happens?

Hints

The chapter section that uses this lab:

Explanation

Answer the prediction first, or open the explanation when you are ready.

Show the explanation

Erosion removes everything the disk does not fit inside, and the dilation that follows grows back exactly what survived, so opening keeps every place where the disk fits. The mask starts with 74 components (24 coins and 50 small ones); after the opening 24 remain and all 50 small pieces are gone. The coins lose only 0.10% of their pixels. The 18 holes are still there: opening removes foreground, it does not fill background.

Lab · IP 09 · Which shapes does a structuring element keep?

Predict first

Open the mask with a disk of radius 4. Every speck and scratch is thinner than the disk (at most 7 pixels wide), and every coin is far wider. What happens?

Loading image…

Input mask: coins with specks, scratches and holes
Output (black stays, red removed, blue added)
The structuring element (each square is one pixel; the orange dot is the origin)

Try this: With opening and a disk of size 4, all clutter goes. Now switch the element to a horizontal line of size 4: which pieces survive, and why? Then raise the disk size with opening until a coin disappears. Finally switch to closing: what does it fix that opening cannot?

The photograph is skimage’s coins image (303 × 384). The clean mask smooths it (Gaussian, σ = 2), subtracts a wide Gaussian background (σ = 50) so the bright corner does not count, and thresholds with Otsu’s method; that gives exactly 24 solid coins. Clutter is then added with a fixed seed so the answer is known: 40 specks (blobs of radius 0 to 3 pixels), 10 thin scratches (5 horizontal, 5 vertical) and 18 round holes of radius 1 to 3 pixels inside the coins (scripts/figures/playground_restore_assets.py). Notation follows the chapter: A is the foreground, B the structuring element, opening = erosion then dilation, closing = dilation then erosion, gradient = dilation minus erosion. Size r means a disk of radius r (all offsets with dx² + dy² ≤ r²), a square of (2r + 1) × (2r + 1) pixels, or a line of 2r + 1 pixels. Pixels outside the image count as background. Components are 8-connected; a hole is a background region that does not touch the border; clutter means a connected piece under 200 pixels (the smallest coin has more than a thousand). In the output picture, black pixels stay foreground, red were foreground and are gone, blue are new.

Method and image sources

Open in the Playground →