Scikit Image - Pyramid Gaussian Transform



The Pyramid Gaussian Transform is refers to the construction of a Gaussian Pyramid, and it is a type of image pyramid where each level of the pyramid is created by applying Gaussian smoothing (blurring) and downsampling (reducing the image size) to the original image iteratively.

Each level of the pyramid represents the image at a different scale, with each level being progressively smaller and smoother than the previous one. Gaussian pyramids are often used for tasks like image blending, image compression, and object detection at multiple scales.

In scikit-image, you can use the skimage.transform.pyramid_gaussian() function to create a Gaussian Pyramid from an input image.

Using the skimage.transform.pyramid_gaussian() function

The transform.pyramid_gaussian() function is used to generate a sequence of images in the Gaussian pyramid created from the input image. It recursively applies the pyramid_reduce function to the image and produces downscaled images at each step.

It's important to note that the first image in the pyramid will be the original, unscaled image. The total number of images in the pyramid is determined by max_layer + 1. If all layers are computed, the last image could either be a one-pixel image or an image where further reduction does not alter its dimensions.

Syntax

Following is the syntax of this function −

skimage.transform.pyramid_gaussian(image, max_layer=-1, downscale=2, sigma=None, order=1, mode='reflect', cval=0, preserve_range=False, *, channel_axis=None)

Parameters

  • image (ndarray): Input image.
  • max_layer (int, optional): The number of layers in the pyramid. The 0th layer corresponds to the original image. Default is -1, which generates all possible layers.
  • sigma (float, optional): Sigma value for the Gaussian filter. Default is calculated as 2 * downscale / 6.0, which covers more than 99% of the Gaussian distribution.
  • mode (str, optional): Determines how the array borders are handled. Options are: 'reflect', 'constant', 'edge', 'symmetric', 'wrap'. The cval parameter is the value used when mode is 'constant'.
  • cval (float, optional): Value to fill beyond the edges of the input image if mode is 'constant'.
  • preserve_range (bool, optional): Whether to maintain the original value range. If False, the input image is converted according to img_as_float conventions.
  • channel_axis (int or None, optional): Indicates the axis of the array that corresponds to channels. If None, the image is assumed to be grayscale (single channel). This parameter was added in version 0.19.

Return Value

It returns a generator (pyramid) that yields pyramid layers as float images.

Example

Here is an example of building an image pyramid using the pyramid_gaussian() function.

import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import pyramid_gaussian
from skimage import io

# Load the input image
image = io.imread('Images/compass.jpg')

# Build the Gaussian pyramid
pyramid = tuple(pyramid_gaussian(image, downscale=2, channel_axis=-1))

# Determine composite image dimensions
rows, cols, dim = image.shape
composite_rows = max(rows, sum(p.shape[0] for p in pyramid[1:]))
composite_cols = cols + pyramid[1].shape[1]
composite_image = np.zeros((composite_rows, composite_cols, 3), dtype=np.double)

# Place original image on the left
composite_image[:rows, :cols, :] = pyramid[0]

# Stack downsampled images to the right
i_row = 0
for p in pyramid[1:]:
    n_rows, n_cols = p.shape[:2]
    composite_image[i_row:i_row + n_rows, cols:cols + n_cols] = p
    i_row += n_rows

# Display the composite image
plt.imshow(composite_image, cmap='gray')
plt.show()

Output

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

Example

The following example demonstrates how to build an image pyramid of an input gray scale image using the pyramid_gaussian() function.

import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import pyramid_gaussian
from skimage import io

# Load the input image
image = io.imread('Images/black rose.jpg', as_gray=True)

# Construct a Gaussian pyramid
pyramid = tuple(pyramid_gaussian(image, downscale=2))

# Determine dimensions for the composite image
original_rows, original_cols = image.shape
composite_rows = max(original_rows, sum(p.shape[0] for p in pyramid[1:]))
composite_cols = original_cols + pyramid[1].shape[1]
composite_image = np.zeros((composite_rows, composite_cols), dtype=np.double)

# Place the original image on the left side
composite_image[:original_rows, :original_cols] = image

# Arrange downsampled images to the right
current_row = 0
for p in pyramid[1:]:
    num_rows, num_cols = p.shape[:2]
    composite_image[current_row:current_row + num_rows, original_cols:original_cols + num_cols] = p
    current_row += num_rows

# Display the composite image with the pyramid
plt.imshow(composite_image, cmap='gray')
plt.show()

Output

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

Advertisements