Scikit Image - Swirl Transform



The Swirl Transform is a non-linear image deformation technique that creates a whirlpool or vortex-like effect in an image. It is a geometric image transformation, commonly used in image processing and computer graphics to create visually appealing effects, such as in art, special effects, and animations.

Mathematically, the Swirl Transform involves converting the coordinates (x, y) of each pixel in the image into polar coordinates (, ) relative to a center point (x0, y0). The degree of swirling is controlled by parameters such as rotation, strength, and radius.

The Scikit-image library provides a function called skimage.transform.swirl() to perform this swirling transformation on images.

Using the skimage.transform.swirl() function

The transform.swirl() function is used to perform a swirl transformation on an input image.

Syntax

Following is the syntax of this function −

skimage.transform.swirl(image, center=None, strength=1, radius=100, rotation=0, output_shape=None, order=None, mode='reflect', cval=0, clip=True, preserve_range=False)

Parameters

  • image (ndarray): Input image.
  • center (optional): (column, row) tuple or (2,) ndarray - The center coordinate of the transformation.
  • strength (optional): float − Specifies the intensity of the swirling effect applied.
  • radius (optional): float − Determines the extent of the swirl in pixels. The effect diminishes rapidly beyond this radius.
  • rotation (optional): float − Additional rotation applied to the image during the transformation.
  • output_shape (optional): tuple (rows, cols) − Specifies the shape of the output image. The default preserves the input image's shape.
  • order (optional): The order of spline interpolation. Defaults to 0 for boolean images and 1 otherwise. Valid range: 0-5. See skimage.transform.warp for details.
  • mode (optional): Determines how points outside the input image's boundaries are filled. Default mode is 'constant', other modes include: 'edge', 'symmetric', 'reflect', and 'wrap'. Modes correspond to numpy.pad behavior.
  • cval (optional): Used with mode 'constant' to specify the value for areas outside the image boundaries.
  • clip (optional): Specifies whether to clip the output to the input image's value range. Enabled by default, useful for higher-order interpolations that might produce values outside the input range.
  • preserve_range (optional): Indicates whether to retain the original value range. If False, the input image is converted as per the conventions of img_as_float.

Return Value

It returns a ndarray it represents the resulting image after applying the swirl transformation.

Example

The following example applies the swirl transformation on an image using the transform.swirl() function.

import matplotlib.pyplot as plt
from skimage import transform
from skimage import io

# Load the input image
image = io.imread('Images/test_img.png')

# Center of the image
center = (image.shape[1] / 2, image.shape[0] / 2) 

# Apply the swirl transformation
swirled_image = transform.swirl(image, center=center, strength=10, radius=150)

# Display the original and swirled images side by side
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(10, 5))
ax0.imshow(image, cmap=plt.cm.gray)
ax0.set_title('Original Image')
ax0.axis('off')

ax1.imshow(swirled_image, cmap=plt.cm.gray)
ax1.set_title('Swirled Image')
ax1.axis('off')

plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Example

Here's another example using a different image and different parameter values to perform the swirl transformation.

import matplotlib.pyplot as plt
import numpy as np
from skimage import data, transform

# Create an image 
image_size = (200, 200)
image = np.zeros(image_size)
image[60:140, 60:140] = np.random.randint(0, 255, image[60:140, 60:140].shape)

# Center of the image
center = (image.shape[1] / 2, image.shape[0] / 2)  

# Apply the swirl transformation
swirled_image = transform.swirl(image, center=center, strength=20, radius=100, rotation=30)

# Display the original and swirled images side by side
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(10, 5))
ax0.imshow(image, cmap=plt.cm.gray)
ax0.set_title('Original Image')
ax0.axis('off')

ax1.imshow(swirled_image, cmap=plt.cm.gray)
ax1.set_title('Swirled Image')
ax1.axis('off')

plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Advertisements