Morphological Transformations: Shaping Images with Kernels

Think of it as sanding or stamping an image with a small shape (kernel) to modify object boundaries. It's used for noise removal or separating touching objects. The footgun: transformations alter object size, so you often need a second operation to compensate.
WHY IT EXISTS Raw image data is often imperfect. To prepare an image for tasks like object counting or measurement, we need a way to clean up geometric artifacts—like noise, small gaps, or unwanted connections—by modifying the shape of features, not just their pixel values.
THE MENTAL MODEL Imagine your image is a black-and-white landscape and you have a small stencil, called a "structuring element" or kernel. You slide this stencil over every pixel. Based on the transformation rule, you either add or remove pixels based on how the stencil overlaps with existing shapes, effectively sanding down or building up the features in the image.
HOW IT WORKS The two fundamental operations are Erosion and Dilation. Erosion shrinks bright regions: for a pixel to remain 'on', the entire kernel, when centered on it, must fit within the bright shape. This removes isolated bright pixels and thins object boundaries. Dilation expands bright regions: a pixel is turned 'on' if any part of the kernel, when centered on it, overlaps with a bright area. This fills small holes and connects nearby objects. More complex operations like Opening (erosion then dilation) and Closing (dilation then erosion) are built from these primitives to perform more nuanced cleaning.
WHEN TO USE IT This technique is a workhorse in binary and grayscale image processing. Use erosion to eliminate small bright noise spots (salt noise) and to separate objects that are weakly connected. Use dilation to fill in small dark holes (pepper noise) and repair breaks in objects. Use Opening (erosion then dilation) to remove salt noise without shrinking the main object, and Closing (dilation then erosion) to fill pepper noise without expanding the main object.
WHEN NOT TO USE IT Applying these transformations directly to multi-channel color images is often not meaningful without first converting to a single channel or binary format. Since all operations modify object geometry, they are unsuitable when the exact, original size and boundary of an object must be preserved perfectly.
ONE CANONICAL EXAMPLE To remove "salt-and-pepper" noise from a binary image, a common strategy is an Opening followed by a Closing. The Opening (erosion, then dilation) removes the small, isolated white "salt" specks. The subsequent Closing (dilation, then erosion) fills in the small, black "pepper" holes. This sequence cleans the image while better preserving the size of the main objects than a single operation would.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.