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.mp4By 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:
threshold0,threshold1,threshold2,threshold3: Sets the activation threshold for the first, second, third, and fourth planes (such as Y, U, V, and Alpha channels). The default value is 65535. Lowering this limit restricts the dilation effect on specific color or brightness planes.coordinates: A bitmask that controls which of the 8 neighboring pixels in the 3x3 grid are used to calculate the maximum value. The default is255(all neighbors). The mapping of the 8-bit integer corresponds to the pixels surrounding the center pixel:- 1: Top-left
- 2: Top
- 4: Top-right
- 8: Left
- 16: Right
- 32: Bottom-left
- 64: Bottom
- 128: Bottom-right
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.mp42. 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.mp43. 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