Scikit Image − Window Functions With Images



Window functions with images, refer to a technique used in image processing when applying Fast Fourier Transforms (FFTs). FFTs are mathematical operations that analyze the frequency components of a signal, including images also. When using FFTs with images, it's important to consider that FFTs assume the data represents one period of a periodic signal, causing discontinuities at signal endpoints. These discontinuities distort the output of the FFT and can lead to energy from "real" frequency components spreading into wider frequency ranges.

To reduce this effect of spectral leakage can be done by multiplying the signal or image with a window function. Window functions gently reduce the signal's amplitude near its edges, smoothing out the artificial discontinuities caused by FFT.

The scikit image library provides a function called window() within its filters module to generate an n-dimensional window of a specified size and dimension.

Using the skimage.filters.window() function

The window() function is used to generate an n-dimensional window of a specified size and dimensionality.

Syntax

Following is the syntax of this function −

skimage.filters.window(window_type, shape, warp_kwargs=None)

Parameters

Here are the details of the parameters −

  • Window_type (string, float, or tuple): Specifies the type of window to be created. It allows any window types available in SciPy's signal.get_window function.
  • Shape (tuple of int or int): Specifies the shape of the window along each axis. If an integer is provided, it generates a 1D window.
  • Warp_kwargs (dict): Keyword arguments that can be passed to skimage.transform.warp. These arguments affect the interpolation method used for warping the window—for example, warp_kwargs={'order': 3}.

Return value

The function returns a generated window represented as the ndarray of the specified shape. The data type (dtype) of the window is np.float64.

Example

The following example applies the "Hann", "Tukey", and "Hamming windows" to the input image −

import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float, color, io
from skimage.filters import window

# Load an example image and convert it to grayscale
image = img_as_float(color.rgb2gray(io.imread('Images/butterfly.jpg')))

# Apply a Hann window to the image
windowed_image_Hann = image * window('hann', image.shape)

# Apply a Tukey window with an alpha parameter of 0.8
windowed_image_Tukey = image * window(('tukey', 0.8),  image.shape)

# Apply a Hamming window to the image
windowed_image_Hamming = image * window('hamming', image.shape)

# Plot the original and windowed images
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
ax = axes.ravel()

ax[0].set_title("Original Image")
ax[0].imshow(image, cmap='gray')
ax[0].axis('off')

ax[1].set_title("Hann Windowed Image")
ax[1].imshow(windowed_image_Hann, cmap='gray')
ax[1].axis('off')

ax[2].set_title("Tukey Windowed Image")
ax[2].imshow(windowed_image_Tukey, cmap='gray')
ax[2].axis('off')

ax[3].set_title("Hamming Windowed Image")
ax[3].imshow(windowed_image_Hamming, cmap='gray')
ax[3].axis('off')

plt.tight_layout()
plt.show()

Output

Scikit Window

Example

In this example, we can see the strong spectral leakage along the x and y axes (observe the vertical and horizontal lines in the "Original FFT" figure ) during the FFT analysis of a standard image. However, the application of a two-dimensional Hann window effectively reduces this spectral leakage issue, resulting in a clearer representation of the "real" frequency information within the FFT's frequency component plot −

import matplotlib.pyplot as plt
import numpy as np
from scipy.fft import fft2, fftshift
from skimage import img_as_float, color, io
from skimage.filters import window

# Load an example image and convert it to grayscale
image = img_as_float(color.rgb2gray(io.imread('Images/rose.jpg')))

# Apply a Hann window to the image
windowed_image_Hann = image * window('hann', image.shape)

# Compute FFTs of the original and windowed image
image_fft = np.abs(fftshift(fft2(image)))
windowed_fft_Hann = np.abs(fftshift(fft2(windowed_image_Hann)))

# Plot the original image, windowed images, and their FFTs
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
ax = axes.ravel()
ax[0].set_title("Original Image")
ax[0].imshow(image, cmap='gray')
ax[0].axis('off')

ax[1].set_title("Hann Windowed Image")
ax[1].imshow(windowed_image_Hann, cmap='gray')
ax[1].axis('off')

ax[2].set_title("Original FFT (Frequency)")
ax[2].imshow(np.log(image_fft), cmap='magma')
ax[2].axis('off')

ax[3].set_title("Hann Window + FFT (Frequency)")
ax[3].imshow(np.log(windowed_fft_Hann), cmap='magma')
ax[3].axis('off')

plt.tight_layout()
plt.show()

Output

Scikit Window
Advertisements