Scikit Image − Gaussian Filter



A Gaussian filter is a type of linear filter used in image processing to perform tasks like smoothing and reducing noise in images based on a Gaussian function. It effectively preserves edges while reducing noise. The main parameter of a Gaussian filter is the standard deviation (sigma), which controls the spread. A larger sigma value results in a wider and smoother Gaussian filter, while a smaller sigma value results in a narrower filter with less smoothing.

In the "scikit-image" library, the filters.gaussian() function is provided to apply Gaussian filtering to images.

Using the skimage.filters.gaussian() function

The filters.gaussian() function is used for applying a multi-dimensional Gaussian filter to an input image.

Syntax

Following is the syntax of this function −

skimage.filters.gaussian(image, sigma=1, output=None, mode='nearest', cval=0, preserve_range=False, truncate=4.0, *, channel_axis=<ChannelAxisNotSet>)

Parameters

    The function accepts the following parameters –
  • Image (array-like): the Input grayscale or color image to filter.
  • Sigma (scalar or sequence of scalars, optional): Standard deviation for the Gaussian kernel. It can be a scalar or a sequence of scalars, specifying the standard deviations for each axis (in the case of multi-dimensional images).
  • Output (array, optional): An output array to store the filter output. If not provided, a new array will be allocated.
  • Mode (optional): Determines how the array borders are handled. Options include 'reflect', 'constant', 'nearest', 'mirror', and 'wrap'. The default is 'nearest'.
  • Cval (scalar, optional): Value to fill past the edges of the input image if the mode is set to 'constant'. The default is 0.0.
  • Preserve_range (bool, optional): If True, keep the original range of pixel values. If False, the input image is normalized based on the image data type. The default is False.
  • Truncate (float, optional): Truncate the filter at a specified number of standard deviations. This parameter can help control the extent of the Gaussian filter. The default is 4.0.
  • channel_axis (int or None, optional): Specifies the axis of the array that corresponds to channels in a multi-channel image. If None, the image is assumed to be grayscale. The default is None. This parameter was added in version 0.19 of scikit-image.

Return value

The function returns the filtered image as an ndarray.

Example

The following example demonstrates how to apply the Gaussian filter to a grayscale image using the skimage.filters.gaussian() function −

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

# Load an input image
image = io.imread('Images/Fruits.jpg', as_gray=True)

# Apply the Gaussian filter with standard deviation 1
sigma = 1
filtered_image = gaussian(image, sigma=sigma)

# Plot the original and Gaussian filtered images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

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

# Display the Gaussian filtered image
ax[1].imshow(filtered_image, cmap='gray')
ax[1].axis('off')
ax[1].set_title('Gaussian filtered image \n(sigma={})'.format(sigma))

plt.tight_layout()
plt.show()

Output

Gaussian Filter

Example

The following example demonstrates how to use the skimage.filters.gaussian() function to apply Gaussian filtering to a color image using different standard deviations and boundary handling modes −

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

# Load an input image
image = io.imread('Images/butterfly.jpg')

# Apply the Gaussian filter with different standard deviations
sigma1 = 0.5
filtered_image1 = gaussian(image, sigma=sigma1, channel_axis=-1)

sigma2 = 1
filtered_image2 = gaussian(image, sigma=sigma2, channel_axis=-1)

sigma3 = 1.3
filtered_image3 = gaussian(image, sigma=sigma3, mode='reflect', channel_axis=-1)

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

# Display the  Original Image
ax[0].imshow(image)
ax[0].axis('off')
ax[0].set_title('Original Image')
# Display the Gaussian filtered image
ax[1].imshow(filtered_image1)
ax[1].axis('off')
ax[1].set_title('Gaussian filtered image \n(sigma={})'.format(sigma1))
ax[2].imshow(filtered_image2)
ax[2].axis('off')
ax[2].set_title('Gaussian filtered image \n(sigma={})'.format(sigma2))
ax[3].imshow(filtered_image3)
ax[3].axis('off')
ax[3].set_title('Gaussian filtered image \n(sigma={}, mode=reflect)'.format(sigma3))

plt.tight_layout()
plt.show()

Output

Gaussian Filter
Advertisements