How to Use the Erosion Filter in FFmpeg
This article explains how to use the erosion video
filter in FFmpeg to apply morphological erosion to your videos. You will
learn the basic syntax, how the filter parameters work, and how to apply
practical command-line examples to reduce bright areas and expand darker
regions in your video frames.
What is the Erosion Filter?
The erosion filter is a morphological operator primarily
used in image processing. For every pixel in a frame, the filter
replaces its value with the minimum value found in its surrounding
neighborhood. In practical terms, this shrinks bright areas (high pixel
values) and expands dark areas (low pixel values). It is commonly used
for noise reduction, edge detection preprocessing, or creating artistic
high-contrast effects.
Basic Syntax
The basic syntax for the erosion filter in FFmpeg is:
ffmpeg -i input.mp4 -vf "erosion=coordinates:threshold0:threshold1:threshold2:threshold3" output.mp4Understanding the Parameters
The filter accepts five optional parameters, which allow you to customize how the erosion is applied:
coordinates: A bitmask that specifies which neighboring pixels are considered when calculating the minimum value. It is an integer from 0 to 255 (default is 255, which checks all 8 surrounding neighbors).- The 8 bits correspond to the 8 neighbors of the center pixel in a
3x3 grid:
- Bit 0: top-left (value 1)
- Bit 1: top (value 2)
- Bit 2: top-right (value 4)
- Bit 3: left (value 8)
- Bit 4: right (value 16)
- Bit 5: bottom-left (value 32)
- Bit 6: bottom (value 64)
- Bit 7: bottom-right (value 128)
- The 8 bits correspond to the 8 neighbors of the center pixel in a
3x3 grid:
threshold0,threshold1,threshold2,threshold3: These parameters limit the maximum pixel value change for each color plane (usually Y, U, V, and Alpha, or R, G, B, and Alpha). The value ranges from 0 to 255. A threshold of 0 means no erosion is applied to that plane, while 255 (the default) allows maximum erosion.
Practical Examples
1. Default Erosion
To apply the erosion filter with its default settings (all neighbors enabled, maximum threshold on all planes), use this command:
ffmpeg -i input.mp4 -vf "erosion" output.mp42. Restricting Erosion to the Luma (Brightness) Plane
If you want to apply the erosion effect only to the brightness of the video while leaving the color planes unaffected, set the threshold for the chroma planes (planes 1 and 2) to 0:
ffmpeg -i input.mp4 -vf "erosion=threshold0=255:threshold1=0:threshold2=0" output.mp43. Horizontal-Only Erosion
To erode pixels only along the horizontal axis, you need to enable only the left (value 8) and right (value 16) neighbors. Adding these values gives a coordinate mask of 24:
ffmpeg -i input.mp4 -vf "erosion=coordinates=24" output.mp44. Subtle Erosion
To apply a milder erosion effect, lower the threshold limit. This prevents the pixel values from changing too drastically:
ffmpeg -i input.mp4 -vf "erosion=threshold0=10:threshold1=10:threshold2=10" output.mp4