Skip to content

Swirl distortion

Fact Value
Also called Twirl, Whirlpool
Status Implemented and tested
Family Artistic and geometric effects
Engine Browser
Type Linear, geometric, swirl mapping
Output Image
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/artistic/distort.ts, swirlFilter
Operator code web/src/core/filters/artistic/distort.ts, swirlMapping
Channels RGB

Overview

The swirl distortion twists the image around its center, most strongly near the center and less further out, like a whirlpool. It uses the mapping of scikit-image's transform.swirl.

Two panels: a checkerboard, and the checkerboard twisted into a whirl around its center. Two panels: a checkerboard, and the checkerboard twisted into a whirl around its center.
A checkerboard, and the swirl distortion with strength 4 and radius 60.

How it works

  1. Measure each output pixel's distance and angle from the center.
  2. Add a turn that decays with the distance.
  3. Read the source at the turned position, at the same distance.

Mathematical foundation

\[ \theta_s = \theta + \phi + s\, e^{-\rho / (R \ln 2 / 5)}, \quad \rho_s = \rho, \qquad s = 3,\ R = 120,\ \phi = 0^{\circ} \]

The formula shows the default parameter values.

where

  • \(\rho, \theta\): The polar coordinates around the image center
  • \(s\): The strength, the turn at the center in radians
  • \(R\): The radius
  • \(\phi\): The rotation added everywhere

rho and theta are the polar coordinates of an output pixel around the image center. It reads the source at the same distance, turned by the rotation phi plus s times an exponential decay. With the decay length R ln 2 / 5, the turn at rho = R has fallen by the factor e^(-5 / ln 2), about 0.0007, as scikit-image intends. This is the mapping of scikit-image's transform.swirl. scikit-image puts its default center at (W / 2, H / 2) in pixel indices, half a pixel from the true center. This filter uses the true center ((W - 1) / 2, (H - 1) / 2). Each output pixel reads the source at the mapped position with bilinear interpolation, the inverse mapping of Gonzalez and Woods, so no holes appear.

Only the angle changes, so circles around the center map onto themselves. The turn decays exponentially, so it halves at a fixed step in distance, about 0.096 times the radius. Neighbors at different distances turn by different angles, which bends straight lines into spirals.

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 and angle from the center.
  2. Add the rotation and the decaying turn to the angle.
  3. Read the source there with bilinear interpolation, using the border mode outside the image.

Parameters

Parameter Default Values Meaning
Strength strength 3 -10 to 10 The turn at the center, in radians. Negative values turn the other way.
Radius radius 120 1 to 2000 px The distance at which the turn has faded to about a thousandth.
Rotation rotation 0 -180 to 180 deg A turn added everywhere, in degrees.
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

swirlFilter 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

Distance from the center 5 10 20 40 60
Turn (degrees) 125.64 68.87 20.70 1.87 0.17

The angle by which an output pixel to the right of the center reads its source, with strength 4 and radius 60. At the center the turn is 4 radians, 229 degrees. It halves about every 6 pixels, and at the radius it is almost gone.

Complexity

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

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

Artifacts and limitations

Limitations

  • Centered on the image. The center cannot be moved.

Artifacts

  • Smeared detail where the twist is strong, since neighbors spread apart.

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. Before the cases are written, the reference is checked against skimage.transform.swirl with the same center for three border modes.

References