Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 46 ยท Filtering and Edges

Morphological Operations

Learn erosion, dilation, opening, closing, morphological gradient and top-hat with structuring elements, and use them to clean binary masks from thresholding.

A threshold or an edge detector rarely hands you a perfect mask. There are stray specks of noise, holes inside objects that should be solid, thin bridges that glue two objects together, and tiny cracks that cut one object in two. Morphological operations are the standard tools for fixing these problems. They treat the mask as a shape and reshape it: grow it, shrink it, remove small pieces, fill small gaps.

Everything in this lesson comes from two simple operations, erosion and dilation, applied with a small shape called a structuring element. Combining them in different orders gives opening, closing, the morphological gradient and the top-hat transform. You will write erosion and dilation yourself on a small mask, check them against scipy.ndimage, and finish by cleaning a noisy threshold mask and counting the objects in it. Masks come from Thresholding, and True/False arrays work as described in Boolean Masks.

Structuring elements

A structuring element (SE) is a tiny boolean array that defines a neighbourhood: which pixels around the current one matter. Typical choices are a 3x3 square, a 3x3 "plus" (cross) and a disk. Its size sets how much the mask changes; its shape sets which directions the change favours.

flowchart LR
    A["mask pixel (r, c)"] --> B["place the structuring<br/>element centre here"]
    B --> C["look at the mask pixels<br/>under the True entries"]
    C --> D["erosion: all of them True?"]
    C --> E["dilation: any of them True?"]

Erosion and dilation differ only in the question asked about the neighbourhood: all, or any.

Erosion and dilation

Erosion keeps a pixel only if the structuring element, centred there, fits entirely inside the object: every pixel under the SE must be True. The effect is that objects shrink by the SE radius, thin parts vanish, and small objects disappear completely.

Dilation sets a pixel to True if the SE touches the object at that position: at least one pixel under the SE is True. Objects grow, gaps close, and small holes fill in.

Both are a sliding window, just like the filters of the previous lessons, with all and any instead of a weighted sum. The code below pads the mask with False, writes both operations as short loops, and compares them with ndimage.binary_erosion and ndimage.binary_dilation. The test mask has a 6x6 block, a 2-pixel-thick bridge to its right, and two isolated specks.

Python Erosion and dilation from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Erosion removed both specks and the two-pixel bridge in one step, because a 3x3 square cannot fit inside a shape that is only 2 pixels thick. The block shrank from 6x6 (36 pixels) to 4x4 (16 pixels in total). Dilation did the opposite: the 48 pixels became 102 and the specks turned into 3x3 squares.

Note: Both operations change the size of objects. Erosion followed by dilation (or the reverse) mostly restores the size while keeping the cleaning effect. That is why the two compound operations below are so useful.

Opening and closing

  • Opening = erosion, then dilation (with the same SE). It removes objects and protrusions that are smaller than the SE, and restores the size of what remains. Use it to delete specks and thin bridges.
  • Closing = dilation, then erosion. It fills holes and cracks smaller than the SE and joins nearby pieces, without changing the overall size much. Use it to repair objects.

An easy way to remember the difference: opening works on the foreground (it opens up the gaps between objects), closing works on the background specks inside the foreground.

flowchart TD
    M["mask with noise"] --> O["opening: erode then dilate"]
    O --> R1["specks and thin bridges gone,<br/>big objects keep their size"]
    R1 --> C["closing: dilate then erode"]
    C --> R2["small holes and cracks filled"]

A common cleaning recipe applies opening first and closing second.

Python Opening removes, closing fills

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Opening keeps only the 6x6 block (36 pixels), so specks and bridge are gone while the block is exactly as large as before. Closing returns a solid 8x12 slab of 96 pixels; the hole and the crack are gone because no 3x3 square can fit inside a gap that narrow.

Warning: Choose the SE size from the features you want to remove, not from the whole image. An SE that is bigger than a real, wanted detail (a thin pen stroke, a narrow gap between two objects) will delete or merge it just as happily as it removes noise.

Shape of the structuring element

The SE shape shapes the result. A square SE grows and shrinks objects equally in all eight directions, which makes round objects drift toward square outlines. A cross is gentler: it only looks at the four direct neighbours. A disk is the closest to isotropic and suits round objects best. You can build any of them by hand:

Python Square, cross and disk structuring elements

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The bigger the SE, the larger the effect: the radius-2 disk removes the most (197 down to 113 pixels) and adds the most (to 297). The cross changes the least of the three (153 and 245). In practice, start with a 3x3 square or cross for small noise and a disk for larger, rounder features, and increase the size one step at a time.

Gradient and top-hat

Subtracting results gives extra tools.

  • Morphological gradient = dilation minus erosion. It is a band along the object boundary, about as wide as the SE. The external gradient (dilation minus the original) lies just outside the object, and the internal gradient (original minus erosion) just inside. They give clean outlines without any derivative kernel, which is handy on binary masks.
  • Top-hat (white top-hat) = the original minus its opening. It keeps only the small bright things that were removed by the opening: bright spots narrower than the SE. The black-hat is the mirror image: closing minus the original, which picks out small dark features.

Top-hat is a classic answer to the uneven-lighting problem from Thresholding. Opening with an SE larger than the spots estimates the slowly changing background; subtracting that background leaves only the spots. Grayscale versions of the operations replace all and any with the minimum and maximum of the neighbourhood (ndimage.grey_erosion, grey_dilation, grey_opening, white_tophat).

Python Outlines and a top-hat on a sloping background

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The gradient of the 6x8 object is a band of 56 pixels, split into 32 outside and 24 inside. The global threshold at 140 is hopeless on the sloping background: it selects all but 17 of the 48 columns, because everything on the right-hand side is brighter than 140 anyway. The top-hat first removes the slope (the 9-pixel opening cannot follow a 3-pixel spot but follows the gentle slope), so a simple threshold of 20 then finds exactly the nine columns of the three spots.

Cleaning a real-looking mask

Now put the recipe together. The scene has three bright discs on a darker background with heavy noise (standard deviation 45), so the threshold mask is full of specks and holes. The pipeline is threshold, opening, closing, fill any remaining holes, and label the objects with ndimage.label, which numbers each connected group of True pixels (see Connected Components and Labeling).

Python Threshold, clean up, count

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The raw mask has 35 separate components, mostly noise. A small opening (a plus-shaped radius-1 disk) leaves just the three real objects. Closing with a larger disk repairs the cracks the noise cut into them, and filling the holes finishes the job. The three objects have 73, 115 and 44 pixels: a pattern with sizes in the ratio you would expect for discs of radius 5, 6 and 4 (about 79, 113 and 50 pixels), because the noise nibbles at the borders.

The ordering matters. Opening first removes the specks, so closing cannot grow them into larger blobs. If you close first, a speck next to an object can fuse with it.

Tip: After cleaning, filter the objects by size. Labelling plus ndi.sum gives every object's area, and you can drop everything below a minimum size without using morphology at all. This is often simpler than a large opening, and it is the first step of the counting project later in the course.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which description matches erosion with a 3x3 square structuring element?

    Erosion keeps a pixel when the structuring element fits entirely inside the object, meaning every pixel under it is True. Dilation asks for any.
  2. 2. You have a mask full of isolated single-pixel specks, but the real objects are all larger than 5x5. Which operation removes the specks while keeping the objects' size?

    Opening erodes first, which deletes anything smaller than the element, and then dilates, which restores the size of the survivors.
  3. 3. Which operation fills small holes inside objects?

    Closing is dilation followed by erosion. The dilation fills holes and cracks narrower than the element, and the erosion restores the outer boundary.
  4. 4. What does the white top-hat transform (image minus its opening) highlight?

    The opening removes bright features smaller than the element and keeps the broad background, so subtracting it leaves exactly those small bright features.
  5. 5. How do you compute the morphological gradient of a binary mask?

    The dilated mask is larger and the eroded mask is smaller, so their difference is a band around the object boundary.

Summary

  • A structuring element is a small boolean shape (square, cross, disk) that defines the neighbourhood for morphological operations.
  • Erosion keeps a pixel when the element fits entirely inside the object (all); it shrinks objects and deletes thin parts. Dilation turns a pixel on when the element touches the object (any); it grows objects and closes small gaps.
  • Opening (erode, then dilate) removes specks, thin bridges and small protrusions. Closing (dilate, then erode) fills small holes and cracks.
  • Dilation minus erosion is the morphological gradient (a boundary band); the white top-hat (image minus opening) isolates small bright features on an uneven background.
  • Choose the element size from the features to remove. Typical cleaning order: opening, closing, fill holes, then label and filter by size.
  • scipy.ndimage provides binary_erosion, binary_dilation, binary_opening, binary_closing, binary_fill_holes, label, and the grayscale family.

Next up: build features that survive a change of scale in Image Pyramids and Scale Space.