Joshu Leonardy
形 · 態 · 学

Morphology Lab

Earlier labs treated an image as a grid of numbers to multiply and add. Mathematical morphology treats a binary image as a set of foreground pixels, and asks one question over and over: does a small probe shape, the structuring element, fit inside it here? That single question gives you erosion and dilation. From those two operations you can build noise removal, outlines, hole filling, object counting and skeletons. This lab follows Chapter 9 of Gonzalez & Woods.

— 01 —
収 · 縮 · と · 膨 · 張

Erosion and dilation

Gonzalez & Woods, 4th ed. §9.1 Preliminaries (p. 636); §9.2: Erosion (p. 639), Dilation (p. 641), Duality (p. 644), eqs. 9-1 to 9-9

Paint a set A on the left grid, and click cells of the structuring element B to switch them on or off. Its origin is the centre cell, marked with a dot. Then hover over the result grid. You will see B placed at that point on A, with each of its cells marked green if it sits on foreground and red if it sits on background.

Erosion keeps a point only if B, moved there, fits entirely inside A. Dilation keeps a point if the reflected B̂, moved there, touches A anywhere. With a symmetric B the reflection changes nothing. Make B lopsided and you will see which way dilation pushes. The two are duals (eq. 9-8): eroding the foreground gives the same result as dilating the background with B̂, then taking the complement.

Operation
Paint A
B size
A · click or drag to paint
Result
Hover over the result.
B

The same operations on a real image

First the image is thresholded into a binary set, using Otsu's method from lab 02, and you choose whether dark or bright pixels are the foreground. This binary image carries through to sections 02–04. Try the structuring element's shape as well as its size. A horizontal line erases vertical strokes but leaves horizontal ones alone.

Operation
Structuring element
Radius r
— 02 —
開 · と · 閉

Opening and closing

Gonzalez & Woods, 4th ed. §9.3 Opening and Closing (p. 644), eqs. 9-10 to 9-15

Erosion removes small specks, but it shrinks everything else as well. Opening fixes that by dilating afterwards, which grows back whatever survived. Picture B rolling around inside A: everything it can reach is kept, and anything too thin for it to enter is lost. Closing is the reverse and fills in gaps too small for B to enter from outside. The book's fingerprint example uses both: open to remove the specks, then close to rejoin the ridges.

A ∘ B = (A ⊖ B) ⊕ B     A • B = (A ⊕ B) ⊖ B

Both operations are idempotent: opening an image that has already been opened changes nothing, and the same goes for closing. The scoreboard checks this for you. The score here is the fraction of pixels that disagree with the noise-free binary image, so lower is better.

Noise
Operation
Structuring element
Radius r
— 03 —
基 · 本 · 算 · 法

Boundaries, holes, components, skeletons

Gonzalez & Woods, 4th ed. §9.5: Boundary Extraction, Hole Filling (p. 653), Extraction of Connected Components (p. 655), Skeletons (p. 662), eqs. 9-18 to 9-20 and 9-28 to 9-30

Each of these algorithms is made only of erosions, dilations and set operations. Hole filling and component extraction share one idea, conditional dilation: grow from a seed one step at a time, but never beyond a mask. Click on the image to place a seed, then step through the iterations and watch the region spread.

Algorithm
Thickness r
— 04 —
再 · 構 · 成

Morphological reconstruction

Gonzalez & Woods, 4th ed. §9.6: Geodesic Dilation and Erosion (p. 667), Morphological Reconstruction by Dilation and by Erosion (p. 668), Sample Applications (p. 669), eqs. 9-38 to 9-48

Section 03 needed you to click a seed. Reconstruction removes that step. It takes a marker image and grows it by conditional dilation inside a mask until nothing more changes. With the right marker, three jobs become fully automatic: keeping only objects that contain a given feature, filling every hole, and removing everything that touches the border.

Application
Vertical line length
Mask
Marker
Ordinary opening
Result
— 05 —
濃 · 淡 · 形 · 態

Grayscale morphology and the top-hat

Gonzalez & Woods, 4th ed. §9.8: Grayscale Erosion and Dilation (p. 674), Grayscale Opening and Closing (p. 680), Some Basic Grayscale Morphological Algorithms (p. 682), eqs. 9-49, 9-50, 9-56, 9-57, 9-60 to 9-62

For a grayscale image, "fits inside" turns into a minimum and "touches" into a maximum. With a flat structuring element, erosion is the min filter and dilation is the max filter from lab 03. Opening and closing then remove bright or dark details that are smaller than B. Subtracting the opening from the image leaves only those small bright details, and that is the top-hat. The book uses it to correct uneven lighting before thresholding. Try it on Uneven page, where lab 02's Otsu threshold failed.

Point at the image to see the window.
Operation
Structuring element
Radius r
Then