FFmpeg Sobel Filter: Edge Detection Guide
The Sobel filter in FFmpeg is a dedicated video filter that applies
the Sobel operator to extract and highlight edges within images and
video streams. This article covers how to implement the
sobel filter using basic command-line syntax, customize its
primary parameters such as scale and delta, and combine it with color
adjustments for optimal edge detection results.
Basic Syntax
To apply the default Sobel edge-detection filter to a video or an
image, pass sobel to the video filter flag
(-vf):
ffmpeg -i input.mp4 -vf "sobel" output.mp4This default command processes all planes (luma and chroma) using standard scaling, highlighting transitions in brightness across horizontal and vertical axes.
Configurable Parameters
The sobel filter accepts three optional parameters to
control how edge values are calculated and rendered:
scale: Multiplies the resulting edge values. A higher scale increases the brightness and sensitivity of detected edges. The default value is1.delta: Adds a fixed offset value to the resulting pixel values, shifting the overall brightness of the output. The default value is0.planes: Specifies which color planes to filter. The value is a bitmask where1processes the first plane (luma/Y),2processes the second (U),4processes the third (V), and8processes the alpha plane. The default value is15, which applies edge detection to all available planes.
Practical Examples
1. High-Contrast Edges
If the default edge lines are too faint, increase the
scale parameter:
ffmpeg -i input.mp4 -vf "sobel=scale=2" output.mp42. Grayscale Edge Detection
The Sobel operator is most effective when analyzing luminance alone. Converting the input to grayscale before applying the filter removes distracting color artifacts:
ffmpeg -i input.mp4 -vf "format=gray,sobel=scale=1.5" output.mp43. Filtering Only the Luma Plane
To detect edges solely on the luminance channel while ignoring chroma
planes, set planes=1:
ffmpeg -i input.mp4 -vf "sobel=planes=1" output.mp44. Bright Background with Dark Edges
By combining sobel with the negate filter,
you can invert the visual output to produce dark sketched lines on a
white background:
ffmpeg -i input.mp4 -vf "format=gray,sobel=scale=1.5,negate" output.mp4