Configure FFmpeg Lenscorrection Filter k1 and k2

This guide explains how to configure the quadratic (k1) and double-quadratic (k2) correction factors in the FFmpeg lenscorrection filter. You will learn the syntax of the filter, how the parameters affect the image, and how to choose the right values to correct barrel and pincushion lens distortion in your videos.

The lenscorrection filter in FFmpeg corrects radial lens distortion using a polynomial model. This is particularly useful for correcting the “fisheye” effect produced by action cameras, wide-angle lenses, and drones.

Filter Syntax and Parameters

The basic syntax for the filter is:

lenscorrection=cx=cx_val:cy=cy_val:k1=k1_val:k2=k2_val

Understanding the Correction Values

The filter uses a mathematical formula to map pixels from the corrected image back to the distorted source image based on their distance (\(r\)) from the center:

\[\text{Source Radius} = r \cdot (1 + k_1 \cdot r^2 + k_2 \cdot r^4)\]

To configure the coefficients correctly, observe the type of distortion in your footage:

Practical Examples

1. Correcting Moderate Fisheye / Barrel Distortion

For standard action cameras with a wide-angle lens, you will need negative values. Start with a stronger k1 and a smaller k2 to clean up the edges.

ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.222:k2=0.045" output.mp4

2. Correcting Heavy Fisheye Distortion

For extremely wide lenses, both k1 and k2 must be adjusted to prevent the corners of the image from stretching excessively or bending backwards.

ffmpeg -i input.mp4 -vf "lenscorrection=k1=-0.333:k2=-0.125" output.mp4

(Note: If cx and cy are omitted, they default to 0.5.)

How to Find the Right k1 and k2 Values

Because every lens is different, finding the exact values requires a systematic approach:

  1. Use a grid: Record a flat grid, a brick wall, or a door frame with straight horizontal and vertical lines.
  2. Test incrementally: Start by setting k2=0 and adjusting k1 in increments of 0.05 (e.g., -0.05, -0.10, -0.15) until the lines near the center of the frame are straight.
  3. Fine-tune the edges: Once the center is straight, you may notice the corners are still curved or overly stretched. Adjust k2 in small increments (e.g., -0.01 to -0.05) to correct the outermost edges of the frame.