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?
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?
Loading image…
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
- Rafael C. Gonzalez & Richard E. Woods, Digital Image Processing, 4th ed., Pearson, 2018 (morphological image processing)
- Nobuyuki Otsu, A Threshold Selection Method from Gray-Level Histograms, IEEE Transactions on Systems, Man, and Cybernetics, 1979
- scikit-image documentation, skimage.morphology (footprints, erosion, dilation, opening, closing)
- scikit-image documentation, skimage.measure.label (connected components)
- scikit-image test images (skimage.data): camera is CC0 by Lav Varshney; coins is from the Brooklyn Museum collection, no known copyright restrictions; astronaut is a NASA photograph in the public domain