Extract Video Outlines Using FFmpeg Edgedetect
This article provides a step-by-step guide on how to use the FFmpeg
edgedetect filter to extract outlines and edge-detected
visual effects from video files. You will learn the basic command
syntax, how to adjust parameters like high and low thresholds to
fine-tune the edge detection, and how to utilize different modes to
achieve custom artistic styles.
The edgedetect filter in FFmpeg utilizes the Canny edge
detection algorithm to find and isolate the edges of objects within a
video. By default, it outputs a monochrome video showing white outlines
on a black background.
Basic Syntax
To apply the default edge detection filter to a video, use the following basic command:
ffmpeg -i input.mp4 -vf "edgedetect" output.mp4In this command, -vf flags the video filter graph, and
"edgedetect" applies the filter using its default
settings.
Adjusting Threshold Parameters
You can control which edges are detected by adjusting the
low and high threshold parameters. These
values are defined as float numbers between 0 and
1.
low: The threshold for the lower boundary. Any edge with a gradient value below this is discarded. The default is20/255(approximately0.078).high: The threshold for the upper boundary. Any edge with a gradient value above this is kept. The default is50/255(approximately0.196).
To increase or decrease the sensitivity of the edge detection, define these parameters in the filter argument:
ffmpeg -i input.mp4 -vf "edgedetect=low=0.1:high=0.3" output.mp4Lowering the values will detect fainter, more subtle outlines, while raising them will restrict the detection to only the most prominent, high-contrast edges.
Changing the Output Mode
The edgedetect filter offers three distinct modes of
operation using the mode parameter:
wires: The default mode. It draws white outlines on a solid black background.colormix: Mixes the original colors of the video into the detected edges, creating a stylized, hand-drawn color pencil effect on a black background.canny: Outputs standard Canny edge-detected outlines.
To use the colormix mode, use the following command:
ffmpeg -i input.mp4 -vf "edgedetect=mode=colormix" output.mp4Creating Black Outlines on a White Background
By default, the output is white lines on a black background. If you
want a traditional “sketch” look with black outlines on a white
background, you can chain the negate filter after the
edgedetect filter:
ffmpeg -i input.mp4 -vf "edgedetect,negate" output.mp4The comma separates the filters, applying the edge detection first and then inverting the colors of the final video.