Scikit Image − Mean Filters



Mean filters belong to a category of image processing filters that are used for the smoothing or blurring of an image. They achieve this by replacing each pixel's value with the average (mean) value of the pixel values in its neighborhood. The scikit-image library offers three mean filters within its filters.rank module which are −

  • Local Mean (mean()): This filter computes the average gray level by considering all pixels that belong to the structuring element.
  • Percentile Mean (mean_percentile()): It selectively uses pixel values falling within the specified percentiles p0 and p1, often set to 10% and 90%.
  • Bilateral Mean (mean_bilateral()): This filter only includes pixels within the structuring element whose gray levels fall within the range of g-s0 to g+s1, often defined as g-500 to g+500.

Local mean and percentile mean filters are typically employed for general image smoothing, whereas bilateral mean filters are a preferred choice when the preservation of edges and important image features is critical. Let us discuss about each mean filter below.

Using the skimage.filters.rank.mean function

The filters.rank.mean() function is used to compute the local mean of an image using a specified neighborhood or footprint.

Syntax

Following is the syntax of this function −

skimage.filters.rank.mean(image, footprint, out=None, mask=None, shift_x=False, shift_y=False, shift_z=False)

Parameters

Here are the details of the parameters −

  • image: Input image to compute the local mean. It should be a NumPy ndarray with dimensions ([P,] M, N) and dtype (uint8, uint16).
  • footprint: The neighborhood or footprint is expressed as a NumPy ndarray of 1's and 0's. This defines the region around each pixel that is used to compute the local mean.
  • out (optional): If provided, it should be a NumPy array with the same dimensions as the input image (image). The local mean will be computed and stored in this output array. If not provided (None), a new array will be allocated for the output.
  • mask (optional): A mask array that can be used to define the area of the image included in the local neighborhood. It is of type integer or float. Pixels with values greater than 0 in the mask are included in the neighborhood calculation. If not provided (None), the complete image is used by default.
  • shift_x, shift_y, shift_z (optional): These parameters specify an offset added to the center point of the footprint. The shift is bounded to the footprint sizes, meaning the center must remain inside the given footprint.

Return value

The function returns the output image, which is a NumPy ndarray of the same data type as the input image (image).

Example

The following example applies the mean filter to the input image using the mean() function −

import matplotlib.pyplot as plt
from skimage import io, color
from skimage.filters.rank import mean
from skimage.morphology import disk
from skimage.util import img_as_ubyte

# Load the input image
image = color.rgb2gray(io.imread('Images/Tajmahal_2.jpg'))
image = img_as_ubyte(image)

# Define a circular footprint
footprint = disk(5)

# Apply the mean filter to the image
output = mean(image, footprint=footprint)

# Plot the original and filtered images side by side
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
ax = axes.ravel()

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

ax[1].imshow(output, cmap=plt.cm.gray)
ax[1].set_title('Mean Filtered Image')
ax[1].axis('off')

plt.show()

Output

Using the skimage.filters.rank.mean_percentile() function

The filters.rank.mean_percentile() function is used to compute the local mean of an image while considering only the gray values within a specified percentile interval [p0, p1].

Syntax

Following is the syntax of this function −

skimage.filters.rank.mean_percentile(image, footprint, out=None, mask=None, shift_x=False, shift_y=False, p0=0, p1=1)

Parameters

Here are the details of the parameters −

  • Image (2-D array (uint8, uint16)): Input image on which the local mean is to be computed.
  • Footprint (2-D array): The neighborhood or structuring element expressed as a 2-D array of 1s and 0s.
  • out (optional, 2-D array (same dtype as input)): If provided, it should be a NumPy array of the same shape and data type as the input image. The filtered image will be stored in this output array. If not provided (None), a new array will be allocated for the output.
  • mask (optional, ndarray): it is a mask array that can be used to define the area of the image included in the local neighborhood. If not provided (None), the complete input image is used by default.
  • shift_x, shift_y (int): Offset added to the footprint's center point. The shift is bounded to the footprint sizes, meaning the center must remain inside the given footprint.
  • p0, p1 (float in [0, 1]): These parameters define the percentile interval [p0, p1] to be considered for computing the value.

Return value

The function returns an output image, which is a 2-D NumPy array of the same data type as the input image (image). It contains the result of applying the mean percentile filter to the input image.

Example

This example applies the mean percentile filter with specific percentile values (p0 and p1) using the mean_percentile() function −

import matplotlib.pyplot as plt
from skimage import io, color
from skimage.filters.rank import mean_percentile  # Importing the mean_percentile function
from skimage.morphology import disk
from skimage.util import img_as_ubyte

# Load the input image and convert it to grayscale
image = color.rgb2gray(io.imread('Images/Tajmahal_2.jpg')) 

# Convert image to 8-bit unsigned integer format
image = img_as_ubyte(image)  

# Define a circular footprint 
footprint = disk(5)

# Apply the mean percentile filter to the image with specific percentile values (p0 and p1)
output = mean_percentile(image, footprint=footprint, p0=0.1, p1=1)

# Plot the original and filtered images side by side
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
ax = axes.ravel()

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

# Display the Mean Percentile Filtered Image
ax[1].imshow(output, cmap=plt.cm.gray)
ax[1].set_title('Mean Percentile Filtered Image')
ax[1].axis('off')

plt.show()

Output

Mean Percentile Filtered

Using the skimage.filters.rank.mean_bilateral() function

The filters.rank.mean_bilateral() function is used to apply the flat kernel bilateral filter to images. This filter is used for edge-preserving and noise-reducing image denoising. It averages pixel values based on both spatial closeness and radiometric similarity.

Spatial closeness is determined by the local pixel neighborhood defined by the footprint or structuring element. Radiometric similarity refers to the similarity in gray level. In the context of the bilateral filter, it's defined by the gray level interval [g-s0, g+s1], where g is the gray level of the current pixel.

Only pixels within this gray level interval are considered for averaging.

Syntax

Following is the syntax of this function −

skimage.filters.rank.mean_bilateral(image, footprint, out=None, mask=None, shift_x=False, shift_y=False, s0=10, s1=10)

Here are the details of the parameters −

  • Image (2-D array (uint8, uint16)): Input image to be filtered.
  • Footprint (2-D array): The neighborhood, expressed as a 2-D array of 1s and 0s, which defines the shape of the local neighborhood or structuring element used in the filter.
  • out (optional, 2-D array (same dtype as input)): If provided, it should be a NumPy array of the same shape and data type as the input image. The filtered image will be stored in this output array. If not provided (None), a new array will be allocated for the output.
  • mask (optional, ndarray): Mask array that can be used to define the area of the image included in the local neighborhood. If not provided (None), the complete input image is used by default.
  • shift_x, shift_y (int): Offset added to the footprint's center point. The shift is bound to the footprint sizes, meaning the center must remain inside the given footprint.
  • s0, s1 (int): These parameters define the radiometric similarity interval around the gray value of the center pixel to be considered for computing the filtered value.

Return value

The function returns the output image, which is a 2-D NumPy array of the same data type as the input image (image). It contains the result of applying the mean bilateral filter to the input image.

Example

This example applies the mean bilateral filter with specific radiometric similarity parameters (s0 and s1) using the mean_bilateral() function −

import matplotlib.pyplot as plt
from skimage import io, color
from skimage.filters.rank import mean_bilateral 
from skimage.morphology import disk
from skimage.util import img_as_ubyte

# Load the input image (convert it to grayscale)
image = color.rgb2gray(io.imread('Images/Tajmahal_2.jpg')) 

# Convert image to 8-bit unsigned integer format
image = img_as_ubyte(image)  

# Define a circular footprint (structuring element)
footprint = disk(5)

# Apply the mean bilateral filter to the image with specific radiometric similarity parameters (s0 and s1)
output = mean_bilateral(image, footprint=footprint, s0=500, s1=500)  

# Plot the original and filtered images side by side
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
ax = axes.ravel()

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

# Display the Mean Bilateral Filtered Image
ax[1].imshow(output, cmap=plt.cm.gray)
ax[1].set_title('Mean Bilateral Filtered Image')
ax[1].axis('off')

plt.show()

Output

Mean Bilateral Filtered
Advertisements