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.mp4

Understanding the Parameters

The filter accepts five optional parameters, which allow you to customize how the erosion is applied:

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.mp4

2. 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.mp4

3. 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.mp4

4. 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