MediumComputer Vision

Morphological Erosion and Dilation

Computer Vision

Medium

Problem

Morphological operations are fundamental tools for processing binary images. Erosion shrinks foreground regions (removing thin protrusions and noise), while dilation expands them (filling small holes and connecting nearby components).

Given a binary image (0s and 1s), a binary structuring element (kernel), and an operation type ("erode" or "dilate"), apply the morphological operation with zero-padding.

Algorithm

Pad the image with zeros using padding = kernel_size // 2 on each side. For each output pixel at position (i, j):

Erosion: the output is 1 only if every position where the kernel is 1 also has a 1 in the corresponding image position. Otherwise the output is 0.

Dilation: the output is 1 if any position where the kernel is 1 has a 1 in the corresponding image position. Otherwise the output is 0.

Theory

Morphological operations are nonlinear filters for binary (black and white) images. They modify shapes based on their local structure using a small template called a structuring element.

The two fundamental operations are:


Binary Image Convention

In binary images:

Morphological operations modify the boundary between foreground and background.


Structuring Element

The structuring element (kernel) defines the neighborhood shape:

Square 3x3 (most common):

1 1 1 1 1 1 1 1 1

Cross 3x3:

0 1 0 1 1 1 0 1 0

Disk (circular, various sizes):

The structuring element is centered on each pixel, and the operation examines all positions where the element has a 1.


Erosion

Erosion shrinks foreground regions. A foreground pixel remains foreground only if ALL positions in the structuring element overlap with foreground.

\text{eroded}[i][j] = \begin{cases} 1 & \text{if all kernel positions match foreground} \\ 0 & \text{otherwise} \end{cases}

Effects of erosion:


Dilation

Dilation expands foreground regions. A pixel becomes foreground if ANY position in the structuring element overlaps with foreground.

\text{dilated}[i][j] = \begin{cases} 1 & \text{if any kernel position matches foreground} \\ 0 & \text{otherwise} \end{cases}

Effects of dilation:


Step-by-Step Erosion Example

Input image:

0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 0 0 0 0

3x3 square structuring element

Center pixel (2,2):

Pixel (1,1):

Result:

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

The 3x3 square shrunk to a single pixel.


Step-by-Step Dilation Example

Input image:

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

3x3 square structuring element

Pixel (1,1):

Result:

0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 0 0 0 0

The single pixel expanded to a 3x3 square.


Opening and Closing

Opening (erosion followed by dilation):

Closing (dilation followed by erosion):

These are more useful than erosion or dilation alone because they remove noise without significantly changing object size.


Boundary Detection

The boundary can be extracted using morphology:

\text{boundary} = \text{image} - \text{eroded(image)}

This gives the outline of all foreground objects.


Applications

Noise removal:

Object separation:

Hole filling:

Skeletonization:

Text processing:


Padding

Morphological operations need to handle boundaries:

Zero padding:

Replication:

This problem uses zero padding.


Multi-Channel Images

For grayscale images, morphology generalizes:

This is useful for noise removal and contrast enhancement on non-binary images.

Examples

Example 1

Input
image = [[0, 0, 0], [0, 1, 0], [0, 0, 0]], kernel = [[1, 1, 1], [1, 1, 1], [1, 1, 1]], operation = "dilate"
Output
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
Explanation
The active center pixel reaches every location covered by the kernel.

Example 2

Input
image = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], kernel = [[1, 1, 1], [1, 1, 1], [1, 1, 1]], operation = "erode"
Output
[[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]

Hints

  1. For erosion, begin with one and clear it when any active-kernel position sees zero.
  2. For dilation, begin with zero and set it when any active-kernel position sees one.

Requirements

Constraints

Starter Code

def morphological_op(image: list, kernel: list, operation: str) -> list:
    """
    Returns the eroded or dilated binary image.
    """
    # Write code here
    pass

Test Cases

CaseMatches
dilate singlepublic
erode solidpublic