Scikit Image − Butterworth Filter



The Butterworth filter is a signal processing filter implemented in the frequency domain, designed to have a frequency response with minimal passband or stopband ripple. Named after Stephen Butterworth, who introduced it in 1930, this filter aims to achieve a maximally flat magnitude response in the passband, making it ideal for applications where a flat frequency response is desired. It operates in the Fourier domain, allowing it to enhance specific frequency components in an image.

The scikit image library provides the butterworth()function to perform the butterworth filtering operations on the images.

Using the skimage.filters.butterworth() filter

The filters.butterworth() function is used to apply a Butterworth filter to an input image, enhancing either high or low-frequency features in the image. And it is defined in the Fourier domain.

Syntax

Following is the syntax of this function −

skimage.filters.butterworth(image, cutoff_frequency_ratio=0.005, high_pass=True, order=2.0, channel_axis=None, *, squared_butterworth=True, npad=0)

Parameters

The function accepts the following parameters −

  • Image ((M[, N[, …, P]][, C]) ndarray): This parameter is the input image on which the Butterworth filter will be applied.
  • Cutoff_frequency_ratio (float, optional): This float parameter determines the position of the cut-off frequency relative to the shape of the FFT of the image. It should be in the range [0, 0.5].
  • High_pass (bool, optional): This parameter specifies whether to perform a high-pass filter (True) or a low-pass filter (False).
  • Order (float, optional): The order of the filter, which affects the slope of the filter's response near the cut-off frequency. Higher values result in steeper slopes in the frequency space.
  • Channel_axis (int, optional): If the input image has multiple channels (e.g., color images), you can specify the index of the channel axis using this parameter. If set to None, all axes are assumed to be spatial dimensions.
  • Squared_butterworth (bool, optional): When set to True, the square of a Butterworth filter is used.
  • Npad (int, optional): This parameter determines how much padding should be added to each edge of the image using numpy's pad function with the mode='edge' extension.

Return value

The function returns the Butterworth-filtered image as an ndarray.

Note: Achieving A band-pass filter can be possible by combining a high-pass and low-pass filter of the Butterworth filter, to reduce boundary artifacts, you can increase npad parameter.

Example

The following example demonstrates how to use the filters.butterworth() function to apply both the low-pass and high-pass Butterworth filters to a grayscale image −

import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.filters import butterworth

# Load a color image
input_image = io.imread('Images/group chat.jpg', as_gray=True)

# Apply a low-pass (squared) Butterworth filtering (order=3.0, npad=0)
low_pass_filtered_image = butterworth(input_image, cutoff_frequency_ratio=0.15, high_pass=False, order=3)

# Apply a high-pass (squared) Butterworth filtering (order=3.0, npad=0)
high_pass_filtered_image = butterworth(input_image, cutoff_frequency_ratio=0.01, high_pass=True, order=3)

# Plot the original, low-pass, and high-pass filtered grayscale images
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
ax = axes.ravel()

# Original Grayscale Image
ax[0].imshow(input_image, cmap='gray')
ax[0].set_title('Original Grayscale Image')
ax[0].axis('off')

# Low-Pass Filtered Grayscale Image
ax[1].imshow(low_pass_filtered_image, cmap='gray')
ax[1].set_title('Low-Pass Filtered Grayscale Image')
ax[1].axis('off')

# High-Pass Filtered Grayscale Image
ax[2].imshow(high_pass_filtered_image, cmap='gray')
ax[2].set_title('High-Pass Filtered Grayscale Image')
ax[2].axis('off')

plt.tight_layout()
plt.show()

Output

Butter Worth

Example

The following example demonstrates how to use the filters.butterworth() function to apply both the low-pass and high-pass Butterworth filters to a color image −

import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.filters import butterworth

# Load a color image
input_color_image = io.imread('Images/Yellow Car.jpg')

# Apply a low-pass (squared) Butterworth filter (order=3.0, npad=0) to a color image
low_pass_filtered_image = butterworth(input_color_image, cutoff_frequency_ratio=0.05, order=3, high_pass=False, channel_axis=-1)

# Apply a high-pass (squared) Butterworth filter (order=3.0, npad=0) to a color image
high_pass_filtered_image = butterworth(input_color_image, cutoff_frequency_ratio=0.02, order=3, high_pass=True, channel_axis=-1)

# Plot the original, low-pass, and high-pass filtered color images
fig, axes = plt.subplots(1, 3, figsize=(15, 5)) 
ax = axes.ravel()

# Display the Original Color Image
ax[0].imshow(input_color_image)
ax[0].axis('off')
ax[0].set_title('Original Color Image')

# Display the Low-Pass Filtered Color Image
ax[1].imshow(low_pass_filtered_image)
ax[1].axis('off')
ax[1].set_title('Low-Pass Filtered Color Image')

# Display the High-Pass Filtered Color Image
ax[2].imshow(high_pass_filtered_image)
ax[2].axis('off')
ax[2].set_title('High-Pass Filtered Color Image')

plt.tight_layout()
plt.show()

Output

Butter Worth
Advertisements