Custom Morphological Grids in FFmpeg Neighbor Filters

FFmpeg provides morphological processing through its neighborhood filter family, primarily implemented as erosion and dilation. This article explains how to define a custom morphological operations grid by manipulating the 3×3 structuring element using the coordinates bitmask parameter to target specific neighboring pixels.

Understanding the 3×3 Neighborhood Bitmask

In FFmpeg, morphological filters evaluate a 3×3 window centered around each pixel. The shape of this structuring grid is controlled by the coordinates parameter, which accepts an 8-bit integer mask ranging from 0 to 255.

Each bit in the mask represents one of the eight adjacent neighbors surrounding the target pixel:

Position Bit Decimal Value
Top-Left Bit 0 1
Top-Center Bit 1 2
Top-Right Bit 2 4
Center-Left Bit 3 8
Center-Right Bit 4 16
Bottom-Left Bit 5 32
Bottom-Center Bit 6 64
Bottom-Right Bit 7 128

The center pixel itself is the value being evaluated and is not included in the bitmask. By combining the decimal values of the desired positions, you construct a custom structuring element.

Calculating Grid Shapes

To activate specific directions in the morphological grid, sum their corresponding values:

  • Full 3×3 Box (Default): Evaluates all 8 neighbors.
    1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255
  • Cross / Plus Shape: Evaluates only cardinal directions (up, down, left, right).
    2 + 8 + 16 + 64 = 90
  • Diagonal / X Shape: Evaluates only corner neighbors.
    1 + 4 + 32 + 128 = 165
  • Horizontal Line: Evaluates only immediate left and right neighbors.
    8 + 16 = 24
  • Vertical Line: Evaluates only top and bottom neighbors.
    2 + 64 = 66

Applying the Filter in FFmpeg

Pass the calculated integer to the coordinates option of either erosion (local minimum calculation) or dilation (local maximum calculation).

Cross-Shaped Dilation

To dilate bright features strictly horizontally and vertically:

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

Horizontal-Only Erosion

To erode edges strictly along the horizontal axis:

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

Implementing Morphological Opening and Closing

Compound morphological operations can be formed by chaining filters with matching custom coordinate grids.

  • Opening (Erosion followed by Dilation): Removes small bright noise while preserving the general shape determined by the grid.

    ffmpeg -i input.mp4 -vf "erosion=coordinates=90,dilation=coordinates=90" -c:a copy output.mp4
  • Closing (Dilation followed by Erosion): Fills small dark holes and gaps along the orientation defined by the grid.

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

Plane-Specific Thresholding

The neighborhood filters also support fine-tuning sensitivity per plane using the threshold options (threshold0, threshold1, threshold2, and threshold3 for Y, U, V, and Alpha respectively). Setting a threshold limits the maximum pixel difference applied during the morphological pass:

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

In this command, the custom cross-shaped dilation affects only the luminance (Y) channel with a maximum intensity shift of 30, leaving the chroma channels untouched.