Using FFmpeg Erosion Filter to Expand Dark Areas

This guide demonstrates how to apply the morphological erosion filter in FFmpeg to expand dark regions across video frames. You will learn the underlying mechanics of morphological erosion, the standard command-line syntax, key configuration parameters to control the effect, and techniques for strengthening the dark expansion across multiple passes.

How Morphological Erosion Works

In image processing, an erosion filter replaces each target pixel with the minimum value found among its immediate neighboring pixels. Because pixel values represent brightness on a scale where zero is black and higher values are white, selecting the minimum value naturally replaces lighter pixels with adjacent darker ones. This causes dark boundaries to expand outward, shrinking or "eroding" brighter highlights.

Basic Command Syntax

To apply default erosion across all color planes, use the erosion filter inside the video filter (-vf) argument:

ffmpeg -i input.mp4 -vf "erosion" -c:a copy output.mp4

In this default state, FFmpeg inspects a 3x3 pixel grid around every pixel and replaces the center pixel with the darkest value present in that grid. The -c:a copy flag ensures the audio stream is passed through without re-encoding.

Customizing Filter Parameters

The erosion filter accepts parameters that allow you to control which neighbors are sampled and how aggressively values are altered.

Restricting Neighbor Coordinates

The coordinates parameter sets an 8-bit integer mask (0 to 255) defining which surrounding pixels in the 3x3 matrix are evaluated:

  • 1: Top-left
  • 2: Top
  • 4: Top-right
  • 8: Left
  • 16: Right
  • 32: Bottom-left
  • 64: Bottom
  • 128: Bottom-right

The default value is 255, which samples all eight surrounding pixels. To expand dark regions only horizontally (left and right neighbors, 8 + 16 = 24), run:

ffmpeg -i input.mp4 -vf "erosion=coordinates=24" -c:a copy output.mp4

To expand dark regions only vertically (top and bottom neighbors, 2 + 64 = 66), run:

ffmpeg -i input.mp4 -vf "erosion=coordinates=66" -c:a copy output.mp4

Limiting Brightness Shift with Thresholds

You can restrict the maximum change allowed for individual color planes using threshold0, threshold1, threshold2, and threshold3 (corresponding to Y, U, V, and Alpha in standard YUV video).

The default is 65535, permitting complete change. To expand dark areas only on the luminance plane while preserving original color chrominance, set the chroma thresholds to 0:

ffmpeg -i input.mp4 -vf "erosion=threshold1=0:threshold2=0" -c:a copy output.mp4

To limit the intensity of the darkening on the luminance plane so the transition is subtle, lower threshold0:

ffmpeg -i input.mp4 -vf "erosion=threshold0=20:threshold1=0:threshold2=0" -c:a copy output.mp4

Increasing the Expansion Radius

A single pass of the erosion filter expands dark regions by a radius of one pixel. For a more pronounced expansion, chain multiple erosion filters sequentially within the filtergraph:

ffmpeg -i input.mp4 -vf "erosion,erosion,erosion" -c:a copy output.mp4

Each comma-separated erosion instance processes the output of the previous pass, expanding dark regions outward by an additional pixel per stage.