SciPy - ndimage.white_tophat() Function



The scipy.ndimage.white_tophat() function which is used to perform the White Top-Hat Transform, a morphological operation used to extract small bright regions in an image. It computes the difference between the original image and its opened version i.e., erosion followed by dilation, highlighting features smaller than the structuring element.

The key parameters of this function include input i.e., image, size which is size of a square structuring element, footprint or structure which custom structuring element shape, mode (boundary handling) and cval which is used for padding value for constant mode.

This function is useful for preprocessing, noise removal and feature enhancement in image analysis tasks like detecting small bright objects.

Syntax

Following is the syntax of the function scipy.ndimage.white_tophat() to implement White Top-Hat Operation on image −

scipy.ndimage.white_tophat(
   input, 
   size=None, 
   footprint=None, 
   structure=None, 
   output=None, 
   mode='reflect', 
   cval=0.0, 
   origin=0
)

Parameters

Following are the parameters of the scipy.ndimage.white_tophat() function −

  • input: The input image or array which can be grayscale or binary, on which the white top-hat filter is applied.
  • size(optional): This specifies the size of a square structuring element and overrides footprint and structure if provided.
  • footprint(optional): A binary array defining the shape of the structuring element where Non-zero elements determine the neighborhood.
  • structure(optional): It is an array that explicitly specifies the structuring element. Overrides both size and footprint when provided.
  • output(optional): This is an array where the result will be stored. If not provided then a new array is allocated.
  • mode(optional): It determines how the filter handles image boundaries and this include 'reflect', 'constant', 'nearest', 'mirror' and 'wrap'.
  • cval(optional): The constant value to use when mode='constant' is selected. Default value is 0.0.
  • origin(optional): This parameter controls the placement of the structuring element relative to the pixel being processed. Default value is 0.

Return Value

The scipy.ndimage.white_tophat() function returns with the same shape as the input array and this highlights small, bright regions in the input image.

Basic White Top-Hat Filtering

As we discussed above the white top-hat filter is a morphological operation used to extract small bright features from an image. When using default settings, the filter employs a simple 3x3 square structuring element. This is useful for basic applications such as highlighting small bright regions against a darker background.

Following is the example which shows how to apply the white top-hat filter with default settings to the given input image −

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import white_tophat
from skimage.data import camera

# Load a sample grayscale image
image = camera()

# Apply white top-hat filter with a specified size (e.g., 3x3 square structuring element)
result = white_tophat(image, size=3)

# Display the original and filtered images
plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title("White Top-Hat Filter (Size=3)")
plt.imshow(result, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

Here is the output of the function scipy.ndimage.white_tophat() which is used to implement basic Example of White Top Hat −

White Tophat Basic Example

Custom Structuring Element (Circular Disk)

A circular disk structuring element is often used in morphological operations to match or extract features with circular shapes. In the view of the white top-hat filter by using a disk-shaped structuring element is useful for enhancing circular bright regions in the image such as spots, reflections or circular objects.

Following is the example which implements the Circular Disk structuring element with the help of White Top Hat operation −

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import white_tophat
from skimage.morphology import disk
from skimage.data import coins

# Load a sample grayscale image
image = coins()

# Create a circular disk structuring element with radius 15
structuring_element = disk(15)

# Apply white top-hat filter with the disk as the structuring element
result = white_tophat(image, footprint=structuring_element)

# Display the original and filtered images
plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title("White Top-Hat (Circular Disk)")
plt.imshow(result, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

Here is the output of the function scipy.ndimage.white_tophat() which is used to implement White Top Hat with Circular Disk −

White Tophat Circular Disk Example

Handling Boundaries with mode='constant'

This example shows how the filter behaves when boundary handling is explicitly set and here we are defining the parameter mode = "constant" in the function scipy.ndimage.white_tophat()

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import white_tophat
from skimage.data import coins

# Load the grayscale input image
image = coins()

# Apply the White Top-Hat filter with boundary handling (constant padding)
result = white_tophat(image, size=15, mode='constant', cval=0)

# Plot the original and processed images
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title("White Top-Hat Result (Boundary Padding)")
plt.imshow(result, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

Here is the output of the function scipy.ndimage.white_tophat() by defining mode = "constant"

White Tophat Mode Example
scipy_morphological_operations.htm
Advertisements