Adjust Center Point in FFmpeg lenscorrection

This article provides a straightforward guide on how to configure the center point and focal coordinates using the lenscorrection filter in FFmpeg. By default, lens correction assumes the optical center of distortion lies exactly at the center of the video frame. By adjusting the normalized center parameters, you can correct barrel or pincushion distortion on off-axis, cropped, or misaligned camera lenses.

Understanding the Center Parameters

The lenscorrection filter in FFmpeg controls the distortion center using two options:

  • cx: The relative horizontal center coordinate of the lens distortion.
  • cy: The relative vertical center coordinate of the lens distortion.

Both parameters accept floating-point values typically ranging from 0.0 to 1.0.

  • cx=0.5 and cy=0.5 represent the exact center of the video frame (the default).
  • cx=0.0, cy=0.0 represents the top-left corner.
  • cx=1.0, cy=1.0 represents the bottom-right corner.

Calculating Pixel-to-Relative Coordinates

If you know the focal optical center in exact pixel values, convert them to relative coordinates by dividing the pixel coordinate by the total dimension:

\[\text{cx} = \frac{\text{X coordinate in pixels}}{\text{Total Width in pixels}}\]

\[\text{cy} = \frac{\text{Y coordinate in pixels}}{\text{Total Height in pixels}}\]

For example, in a 1920x1080 video with an optical center located at \((912, 567)\):

  • cx = 912 / 1920 = 0.475
  • cy = 567 / 1080 = 0.525

FFmpeg Command Syntax

To apply these coordinates, pass cx and cy along with the distortion correction coefficients (k1 for primary distortion, k2 for secondary edge distortion) into the -vf filter argument:

ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.475:cy=0.525:k1=-0.22:k2=0.05" -c:a copy output.mp4

Real-Time Adjustment and Fine-Tuning

Finding the precise optical center often requires trial and error. You can test your adjustments in real-time using ffplay:

ffplay -i input.mp4 -vf "lenscorrection=cx=0.48:cy=0.51:k1=-0.15"

Adjust the values incrementally until straight lines parallel to the frame edges remain straight across the image.