Use Sobel and Scharr Filters in FFmpeg

This article explains how to apply the Sobel and Scharr edge detection operators in FFmpeg. While FFmpeg’s default edgedetect filter uses the Canny algorithm—which internally utilizes the Sobel operator—you can apply Sobel and Scharr directly using dedicated video filters or custom matrices via the convolution filter. Below, you will find the precise commands and parameters required to implement these edge-detection techniques.

Dedicated Sobel and Scharr Filters

FFmpeg includes built-in, dedicated filters for both the Sobel and Scharr operators. These filters are highly optimized and are the easiest way to perform edge detection without manual matrix configuration.

Applying the Sobel Filter

The Sobel operator highlights horizontal and vertical edges. To apply the Sobel filter to a video, use the sobel filter:

ffmpeg -i input.mp4 -vf "sobel=planes=15:scale=1:delta=0" output.mp4

Applying the Scharr Filter

The Scharr operator is a variation of the Sobel operator designed to provide better rotational symmetry and more accurate edge detection for diagonal lines. To apply it, use the scharr filter:

ffmpeg -i input.mp4 -vf "scharr=planes=15:scale=1:delta=0" output.mp4

Custom Edge Detection Using the Convolution Filter

If you require precise control over the mathematical kernel or want to isolate horizontal or vertical edges individually, you can manually define Sobel and Scharr kernels using FFmpeg’s convolution filter.

To apply a horizontal Sobel filter:

ffmpeg -i input.mp4 -vf "convolution='-1 0 1 -2 0 2 -1 0 1:0 0 0:0 0 0:0 0 0':9:0" output.mp4

To apply a vertical Sobel filter:

ffmpeg -i input.mp4 -vf "convolution='-1 -2 -1 0 0 0 1 2 1:0 0 0:0 0 0:0 0 0':9:0" output.mp4

The Standard edgedetect Filter (Canny Edge Detection)

The native edgedetect filter in FFmpeg implements the Canny edge detection algorithm. This algorithm uses the Sobel operator internally to calculate image gradients before applying non-maximum suppression and hysteresis thresholding to produce thin, clean edge lines.

To use the Canny edge detector with custom thresholds, use the following syntax:

ffmpeg -i input.mp4 -vf "edgedetect=low=0.1:high=0.2" output.mp4

You can also change the output format using the mode parameter:

ffmpeg -i input.mp4 -vf "edgedetect=low=0.1:high=0.2:mode=colormix" output.mp4