
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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 −

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 −

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" −
