How to Use the Dilation Filter in FFmpeg

This article provides a quick guide on how to use the dilation filter in FFmpeg to apply morphological dilation to your videos or images. You will learn the basic syntax of the filter, understand its key parameters like thresholds and coordinates, and see practical command-line examples to help you expand bright areas or highlight edges in your visual media.

Understanding the Dilation Filter

The dilation filter in FFmpeg is a morphological operation typically used in image processing. It analyzes each pixel in a frame and replaces its value with the maximum value found within its defined neighborhood. In practice, this effect expands brighter areas (or structures of high intensity) and shrinks darker areas. It is commonly used for edge detection, noise reduction, and artistic video effects.

Basic Syntax

The basic syntax for applying the dilation filter is:

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

By default, this applies the dilation filter to all channels using a 3x3 kernel (all 8 surrounding neighbors).

Key Parameters

You can customize the behavior of the dilation filter using the following parameters:

Practical Examples

1. Vertical Dilation Only

To dilate pixels only vertically (using the top and bottom neighbors), set the coordinates value to 66 (2 for top + 64 for bottom):

ffmpeg -i input.mp4 -vf "dilation=coordinates=66" output.mp4

2. Horizontal Dilation Only

To dilate pixels only horizontally (using the left and right neighbors), set the coordinates value to 24 (8 for left + 16 for right):

ffmpeg -i input.mp4 -vf "dilation=coordinates=24" output.mp4

3. Limiting the Effect on Specific Planes

If you want to apply dilation strongly to the luma (brightness) channel but limit its effect on the chroma (color) channels, you can adjust the threshold values:

ffmpeg -i input.mp4 -vf "dilation=threshold0=65535:threshold1=10:threshold2=10" output.mp4