Barrel distortion¶
| Fact | Value |
|---|---|
| Also called | Barrel 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, barrelFilter |
| Operator code | web/src/core/filters/artistic/distort.ts, polynomialSource |
| Channels | RGB |
Overview¶
Barrel distortion bows straight lines outward, like the sides of a barrel. Many wide-angle lenses show it. The filter uses the first term of the radial distortion model of camera calibration.
How it works¶
- Measure each output pixel's distance from the center.
- Read the source from r (1 + k r^2), scaled so the corners stay.
- Interpolate the source bilinearly.
Mathematical foundation¶
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 of the radial term
The source radius grows faster than the output radius toward the edges, so the outer part of the image is squeezed inward and straight lines through it bow outward. The factor 1 + k r^2 is the first radial term of the lens distortion model that camera calibration uses, as in OpenCV. Keep corners divides by 1 + k so that the corners, at r = 1, read from the corners. Without it, the divisor is 1 and the edges read from outside the image. Each output pixel reads the source at the mapped position with bilinear interpolation, the inverse mapping of Gonzalez and Woods, so no holes appear.
Camera calibration describes radial lens distortion by a polynomial in the distance from the center, 1 + k1 r^2 + k2 r^4 and so on. A positive k1 with this filter's direction of mapping makes the outer part read from further out, so the image is pressed toward the center there and lines bow outward. One term is enough to show the effect.
Kernel or operator¶
The Inspector shows the mapping with the current settings, then the bilinear read of the source.
Algorithm¶
- For each output pixel, find its distance r from the center.
- Compute the source distance r (1 + k r^2), divided by 1 + k with Keep corners on.
- Read the source there with bilinear interpolation, using the border mode outside the image.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Strength strength |
0.3 | 0 to 1 | The 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¶
barrelFilter 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.5, keep corners | 0 | 0.136 | 0.288 | 0.472 | 0.704 | 1 |
| r_s, k = 0.5, no fit | 0 | 0.204 | 0.432 | 0.708 | 1.056 | 1.500 |
With Keep corners on, the center reads from closer in and the corner stays, so the middle is enlarged and lines bow outward. With it off, the edges read from up to 1.5 times as far out, beyond the image, where the border mode fills in.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. Each output pixel reads four source pixels.
Artifacts and limitations¶
Limitations
- One radial term only. Real lenses need more terms and a tangential part.
Artifacts
- Soft corners 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.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/artistic/artistic.test.ts
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.