How to Use the FFmpeg Dilation Filter

The dilation filter in FFmpeg is a morphological video filter that replaces each pixel with the maximum value found in its surrounding neighborhood, effectively expanding bright regions and shrinking dark areas. This guide provides a straightforward overview of how the filter functions, its primary configuration options, and practical command-line examples to apply single-pass, multi-pass, or selective plane dilation to your video files.

Understanding the Dilation Filter

In image processing and morphological analysis, dilation acts on local pixel neighborhoods (a 3x3 grid by default). When applied to an image or video, the filter evaluates each pixel alongside its adjacent neighbors and assigns the highest value to the target pixel. As a result, highlights, white text, lines, and bright areas grow outward.

Basic Syntax

To apply the default 3x3 dilation across all color channels, run:

ffmpeg -i input.mp4 -vf "dilation" output.mp4

By default, this command evaluates all eight surrounding pixels and applies the maximum luminance/color change possible.

Filter Parameters

The dilation filter accepts two primary sets of parameters:

  1. threshold0, threshold1, threshold2, threshold3
    Sets the maximum change allowed for each plane (plane 0 is Luma/Red, plane 1 is Chroma-U/Green, plane 2 is Chroma-V/Blue, and plane 3 is Alpha).
    • Range: 0 to 65535
    • Default: 65535 (unlimited expansion)
    • Setting a threshold to 0 leaves that plane completely untouched.
  2. coordinates
    An 8-bit integer mask controlling which neighboring pixels in the 3x3 grid are evaluated.
    • Default: 255 (all 8 neighbors: top-left, top, top-right, left, right, bottom-left, bottom, bottom-right).
    • Each bit corresponds to a specific neighbor position.

Practical Examples

1. Expand Brightness on Luminance Only

To expand bright highlights without distorting chroma (color balance) in YUV videos, limit the filter to plane 0:

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

2. Subtle Dilation with a Threshold Limit

To prevent bright areas from overwhelming neighboring details, restrict the maximum amount a pixel value can increase:

ffmpeg -i input.mp4 -vf "dilation=threshold0=30:threshold1=15:threshold2=15" -c:a copy output.mp4

3. Chaining Passes for a Stronger Expansion

Because the filter operates on a fixed 3x3 matrix per pass, chaining multiple instances increases the radius of the effect:

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

4. Directional Dilation

You can adjust the coordinates parameter to limit expansion to specific directions. For instance, using horizontal-only neighbors (left and right, which correspond to bit flags 8 and 16, totaling 24):

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