Morphology: erosion, dilation, opening and closing

Intermediate

Introduction

Thresholding rarely gives a clean mask. There are specks of noise where the background was slightly bright, small holes where the object was slightly dark, and thin bridges joining objects that should be separate. Mathematical morphology fixes these with two simple operations that grow and shrink shapes. Combined, they remove specks, fill holes and smooth outlines, and they are used in almost every vision pipeline that works with masks.

How it works

A structuring element says which neighbours to look at. The most common are the 3 × 3 square (all eight neighbours and the pixel itself) and the cross (the pixel and its four neighbours above, below, left and right).

  • Erosion: a pixel stays 1 only if every pixel under the element is 1. Shapes lose a layer of pixels at their boundary; objects smaller than the element vanish.
  • Dilation: a pixel becomes 1 if any pixel under the element is 1. Shapes gain a layer; small holes and gaps fill in.

With 0 and 1, erosion is the minimum over the window and dilation the maximum. That is why the same code works for greyscale images, where erosion darkens (min filter) and dilation brightens (max filter).

Borders. Something must be assumed outside the image. In this course, outside counts as background (0), so shapes touching the border erode from that side. OpenCV instead ignores the outside for erosion by default, so check what your library does.

Combining the two:

  • Opening = erode, then dilate. Erosion deletes everything smaller than the element; dilation then regrows the surviving shapes to about their old size. Specks and thin lines are gone for good.
  • Closing = dilate, then erode. Dilation fills holes and gaps; erosion then shrinks the shapes back. Holes and narrow gaps stay filled, which also joins objects that were close together.

Other useful combinations: the morphological gradient (dilation minus erosion) outlines shapes, and the top-hat (image minus its opening) keeps bright details smaller than the element while removing an uneven background.

A 3 × 3 square is separable: a 1 × 3 line followed by a 3 × 1 line gives the same result with fewer comparisons, which the example uses.

Example

img = [
    "000000000",
    "011101110",
    "011101010",
    "011101110",
    "000000000",
    "000000001",
]


def square3(img, pick):
    # A 3 x 3 square is a 1 x 3 line followed by a 3 x 1 line. Outside the image counts as 0.
    w = len(img[0])
    padded = ["0" * (w + 2)] + ["0" + row + "0" for row in img] + ["0" * (w + 2)]
    across = ["".join([pick(row[c:c + 3]) for c in range(w)]) for row in padded]
    return ["".join([pick(across[r][c] + across[r + 1][c] + across[r + 2][c]) for c in range(w)]) for r in range(len(img))]


def show(name, img):
    print(name)
    for row in img:
        print("  ", row.replace("1", "#").replace("0", "."))


show("original", img)
eroded = square3(img, min)
show("erosion (min): every neighbour must be 1", eroded)
dilated = square3(img, max)
show("dilation (max): any neighbour 1", dilated)
show("opening = dilate(erode)", square3(eroded, max))
show("closing = erode(dilate)", square3(dilated, min))
Open in compiler

Line by line

  1. The 6 × 9 binary image (rows as strings of 0 and 1) holds a solid 3 × 3 square, a 3 × 3 ring with a hole in its centre, and a single speck in the bottom-right corner.
  2. square3 pads the image with 0s, takes pick (min or max) over each horizontal run of three, then over each vertical run of three: a 3 × 3 square in two passes.
  3. Erosion (min) keeps only the centre of the solid square. The ring is too thin to survive and the speck vanishes.
  4. Dilation (max) grows every shape by one pixel. The gap between the square and the ring is only one pixel wide, so they merge, and the hole fills.
  5. Opening regrows the one surviving pixel into the original square: the speck and the thin ring are removed and the solid shape kept.
  6. Closing erodes the dilated image: the hole and the gap stay filled, so the two shapes have become one block.

Expected result

original
   .........
   .###.###.
   .###.#.#.
   .###.###.
   .........
   ........#
erosion (min): every neighbour must be 1
   .........
   .........
   ..#......
   .........
   .........
   .........
dilation (max): any neighbour 1
   #########
   #########
   #########
   #########
   #########
   .......##
opening = dilate(erode)
   .........
   .###.....
   .###.....
   .###.....
   .........
   .........
closing = erode(dilate)
   .........
   .#######.
   .#######.
   .#######.
   .........
   .........

In the real world

Scanned forms are opened to remove dust and closed to repair broken letters; cell counters open masks to separate touching cells; road-marking detectors close gaps in dashed lines. In OpenCV (shown as text; it does not run on this site):

import cv2
import numpy as np

kernel = np.ones((3, 3), np.uint8)                        # square element
cross = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
eroded = cv2.erode(mask, kernel)
dilated = cv2.dilate(mask, kernel, iterations=2)
opened = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
closed = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
tophat = cv2.morphologyEx(grey, cv2.MORPH_TOPHAT, np.ones((15, 15), np.uint8))

Common mistakes

  • Confusing opening and closing. Opening (erode first) removes; closing (dilate first) fills.
  • Using an element larger than the objects you want to keep, which erases them.
  • Updating the mask in place while eroding, so later pixels see already-eroded neighbours.
  • Forgetting the border rule, which can erode shapes that touch the image edge.
  • Expecting closing to keep objects separate: it joins anything closer than the element.

Best practices

  • Pick the element size from the size of the noise you want to remove, not by trial and error.
  • Use a cross to change shapes less at the corners, a square for stronger effects.
  • Open to remove noise first, then close to fill holes, and look at the mask after each step.
  • For greyscale images with uneven backgrounds, try the top-hat before thresholding.

Summary

Erosion keeps a pixel only if its whole neighbourhood is set (a minimum); dilation sets it if any neighbour is set (a maximum). Opening (erode then dilate) removes specks and thin parts; closing (dilate then erode) fills holes and gaps and can join nearby shapes. The structuring element defines "small", and the same min and max filters work on greyscale images, giving the top-hat for uneven backgrounds.

Quick check

1. Which operation removes isolated specks but keeps large solid shapes?
2. What is binary erosion in terms of min and max?
3. Why can closing merge two separate objects?
4. What does the top-hat transform keep?

Key takeaways

  • Erosion is a minimum over the neighbourhood and dilation a maximum; they work on binary and greyscale images alike.
  • Opening removes specks and thin lines; closing fills small holes and joins nearby shapes.
  • The structuring element's size and shape decide what counts as small.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.