Correct Lens Distortion with FFmpeg Lenscorrection
Wide-angle lenses and action cameras often introduce barrel or
pincushion distortion, making straight lines appear curved. This article
provides a straightforward guide on how to use the FFmpeg
lenscorrection filter to correct these optical distortions,
explaining the key parameters and providing practical command-line
examples to help you achieve flat, natural-looking video footage.
Understanding the lenscorrection Filter
The lenscorrection filter in FFmpeg uses a mathematical
model to correct radial distortion. It adjusts pixels based on their
distance from the optical center of the image. The filter is controlled
by four primary parameters:
cx: The relative horizontal center of the correction. The value ranges from0.0to1.0, with0.5representing the exact center of the frame (default is0.5).cy: The relative vertical center of the correction. The value ranges from0.0to1.0, with0.5representing the exact center of the frame (default is0.5).k1: The coefficient for the correction factor nearest the center of the image.k2: The coefficient for the correction factor furthest from the center of the image.
Adjusting k1 and k2
- For Barrel Distortion (fisheye effect, where lines curve
outward): Use negative values for
k1andk2. - For Pincushion Distortion (where lines curve
inward): Use positive values for
k1andk2.
Basic Command Syntax
The basic syntax for applying the lenscorrection filter
in an FFmpeg command is as follows:
ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.1:k2=-0.05" output.mp4Practical Examples
Because every camera lens is unique, finding the perfect values requires some trial and error. Below are common starting points for different correction needs.
1. Correcting Mild GoPro / Action Camera Barrel Distortion
Action cameras typically suffer from significant barrel distortion. To flatten the image, start with small negative values for both coefficients:
ffmpeg -i input.mp4 -vf "lenscorrection=k1=-0.15:k2=-0.05" output.mp42. Correcting Strong Fisheye Distortion
For extreme wide-angle or circular fisheye lenses, you will need more aggressive negative values:
ffmpeg -i input.mp4 -vf "lenscorrection=k1=-0.22:k2=-0.12" output.mp43. Correcting Pincushion Distortion
If your lens zooms in and causes straight lines to bend inward toward the center, use positive values:
ffmpeg -i input.mp4 -vf "lenscorrection=k1=0.1:k2=0.05" output.mp44. Adjusting an Off-Center Optical Lens
If your camera sensor is slightly misaligned with the lens, you can shift the correction center away from the default center (0.5, 0.5):
ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.48:cy=0.52:k1=-0.1:k2=-0.05" output.mp4Tips for Finding the Right Values
Test on a Still Frame: Instead of rendering the whole video, export a single frame containing straight lines (like a wall, doorway, or grid) to test your values quickly:
ffmpeg -i input.mp4 -ss 00:00:05 -vframes 1 -vf "lenscorrection=k1=-0.12:k2=-0.03" test_frame.pngFocus on k1 First: Set
k2to0and adjustk1until the center of the image looks straight. Once the center is aligned, adjustk2to fix the distortion near the outer edges of the frame.