How to Use Sobel Filter in FFmpeg for Edge Detection

This guide explains how to use the Sobel edge-detection filter in FFmpeg to highlight edges in your video files. You will learn the basic command-line syntax, understand the available filter options, and see practical examples of applying the Sobel operator to your video processing workflows.

Understanding the Sobel Filter in FFmpeg

The sobel filter in FFmpeg applies the Sobel operator to an input video. This operator computes the gradient of the image intensity at each point, giving the direction of the largest possible increase from light to dark and the rate of change in that direction. The result highlights edges, making it useful for computer vision, motion analysis, or stylized video effects.

Basic Syntax

To apply the default Sobel filter to a video, use the -vf (video filter) flag followed by sobel:

ffmpeg -i input.mp4 -vf "sobel" output.mp4

This command takes input.mp4, applies the Sobel edge-detection filter to all color channels, and saves the result to output.mp4.

Filter Options

The sobel filter accepts three parameters to customize the output: planes, scale, and delta.

The syntax for passing options is:

sobel=planes:scale:delta

Or using named parameters:

sobel=planes=value:scale=value:delta=value

1. Planes (planes)

This option specifies which color planes should be filtered. * It accepts a value from 0 to 15 (interpreted as a bitmap). * The default value is 15, which filters all planes (Luma, Chroma U, Chroma V, and Alpha). * Setting it to 1 filters only the first plane (usually the Luma/brightness channel in YUV videos).

2. Scale (scale)

This option sets the multiplier value for the filtered pixels. * The default value is 1. * Increasing this value makes the detected edges brighter and more pronounced.

3. Delta (delta)

This option specifies a constant value to be added to the filtered pixel results. * The default value is 0. * This can be used to shift the overall brightness of the output video.

Practical Examples

Apply Sobel to Luma Only (Grayscale Edge Detection)

To detect edges on just the brightness channel and ignore color processing, set the planes parameter to 1:

ffmpeg -i input.mp4 -vf "sobel=planes=1" output.mp4

Boost Edge Visibility

If the detected edges are too faint, you can increase the scale value to make them stand out:

ffmpeg -i input.mp4 -vf "sobel=scale=2" output.mp4

Combine Sobel with Other Filters

You can chain the sobel filter with other FFmpeg filters. For example, to convert a video to black and white first and then apply a high-contrast Sobel edge filter:

ffmpeg -i input.mp4 -vf "format=gray,sobel=scale=1.5" output.mp4