Skip to content

Pincushion distortion

Fact Value
Also called Pincushion lens distortion
Status Implemented and tested
Family Artistic and geometric effects
Engine Browser
Type Linear, geometric, radial polynomial mapping
Output Image
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/artistic/distort.ts, pincushionFilter
Operator code web/src/core/filters/artistic/distort.ts, polynomialSource
Channels RGB

Overview

Pincushion distortion bows straight lines inward, toward the center, like the sides of a pincushion. Telephoto lenses often show it. It is the barrel mapping with a negative coefficient.

Two panels: a checkerboard, and the checkerboard with its edges stretched and its lines bowing inward. Two panels: a checkerboard, and the checkerboard with its edges stretched and its lines bowing inward.
A checkerboard, and the pincushion distortion with strength 0.3.

How it works

  1. Measure each output pixel's distance from the center.
  2. Read the source from r (1 - k r^2), scaled so the corners stay.
  3. Interpolate the source bilinearly.

Mathematical foundation

\[ r_s = \frac{r\, (1 - k\, r^2)}{1 - k}, \qquad k = 0.15 \]

The formula shows the default parameter values.

where

  • \(r\): The distance of an output pixel from the image center, as a share of half the diagonal
  • \(r_s\): The distance of the source position it reads, in the same unit
  • \(k\): The strength, the size of the negative radial term

This is the barrel mapping with a negative coefficient. The source radius grows slower than the output radius toward the edges, so the outer part of the image is stretched outward and straight lines bow inward. The strength stops at 0.3, below one third, because the mapping folds back on itself when 3 k r^2 reaches 1 at the corners. Keep corners divides by 1 - k so that the corners stay. Each output pixel reads the source at the mapped position with bilinear interpolation, the inverse mapping of Gonzalez and Woods, so no holes appear.

With a negative coefficient, the source distance grows more slowly than r toward the edges, so the outer part is stretched. The mapping must stay one to one: its slope 1 - 3 k r^2 must stay positive up to the corners, which needs k below one third. The strength therefore stops at 0.3.

Kernel or operator

The Inspector shows the mapping with the current settings, then the bilinear read of the source.

Algorithm

  1. For each output pixel, find its distance r from the center.
  2. Compute the source distance r (1 - k r^2), divided by 1 - k with Keep corners on.
  3. Read the source there with bilinear interpolation, using the border mode outside the image.

Parameters

Parameter Default Values Meaning
Strength strength 0.15 0 to 0.3 The size of the negative coefficient k of the radial term.
Keep corners fit on On or off Scale the mapping so the corners stay where they are.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.

Implementation

pincushionFilter in web/src/core/filters/artistic/distort.ts. It reads the source with sampleBilinear through warpPlane. The filter is global, and it sets movesAlpha, so the runner moves the alpha plane with the same mapping and transparency stays with its colors.

Worked example

r 0 0.2 0.4 0.6 0.8 1
r_s, k = 0.3, keep corners 0 0.282 0.544 0.765 0.923 1

The source distance for strength 0.3. It rises faster than r in the middle and slower near the corner, so the edges are stretched and lines bow inward.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. Each output pixel reads four source pixels.

Artifacts and limitations

Limitations

  • Limited to small strengths, so the mapping stays one to one.

Artifacts

  • Stretched, soft edges at high strengths.

Tests

The reference computes the same mapping with NumPy and reads the source with scipy.ndimage.map_coordinates at order 1, an independent bilinear sampler, for several settings and border modes. Unit tests check that strength 0 changes nothing and that the corners stay with Keep corners on.

References

  • OpenCV documentation, camera calibration and the radial distortion coefficients. https://docs.opencv.org/4.x/d9/d0c/group__calib3d.html
  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 2, geometric transformations and image interpolation.