
- 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 - Top-Hat Filters
The Top-Hat filter in SciPy is a morphological operation designed to highlight specific intensity features in an image. It is particularly useful for detecting and enhancing small, localized features that are either brighter or darker than their surroundings. There are two primary types of top-hat filters as follows −
- White Top-Hat Filter
- Black Top-Hat Filter
Now, let's see each type of the Top-Hat Filters in detail −
White Top-Hat Filter
The White Top-Hat Filter is a morphological operation that extracts small bright features from an image that are smaller than the structuring element.
It is often used to enhance bright details, remove uneven background illumination or highlight specific regions of interest. The White Top-Hat filter is mathematically defined as follows −
WhiteTop-HatImage = OriginalImageOpenedImage
A morphological operation that consists of erosion followed by dilation. It smooths the image by removing small bright regions that do not fit within the structuring element.
Subtracting the opened image from the original image enhances the bright regions that were removed during the opening process.
Creating/Implementing White Top-Hat Filter
The structuring element i.e., a kernel defines the size and shape of the features to extract. The bright regions smaller than the structuring element are isolated because the opening operation removes bright regions smaller than the structuring element and subtraction restores these regions while suppressing the larger-scale structures.
In SciPy the White Top-Hat Filter can be implemented by the function scipy.ndimage.white_tophat().
Syntax
Following is the syntax of the function scipy.ndimage.white_tophat() which is used to extract small bright features from an image −
scipy.ndimage.white_tophat( input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0 )
Following are the parameters of the scipy.ndimage.white_tophat() function −
- input: The input image or array i.e., grayscale or binary.
- size: This parameter specifies the size of a square structuring element.
- footprint: A binary array that defines the shape of the structuring element.
- structure: It specifies the exact structuring element and overrides size and footprint.
- mode: This determines how image boundaries are handled.
- cval: The constant value used when mode='constant'.
- origin: This parameter controls the position of the structuring element relative to the current pixel.
White Top-Hat Filter with Default Structuring Element
Following is the basic example of the function scipy.ndimage.white_tophat(), in which we will apply the white top-hat filter using the default structuring element i.e., a 3x3 square to a sample image (camera) from skimage.
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import white_tophat from skimage.data import camera # Load the sample image image = camera() # Apply the white top-hat filter with the default structuring element (3x3 square) result = white_tophat(image, size=3) # Plot the original and filtered images side by side plt.figure(figsize=(12, 6)) # Original Image plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') # White Top-Hat Filter Result plt.subplot(1, 2, 2) plt.title("White Top-Hat Filter Result") plt.imshow(result, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.white_tophat() −

White Top-Hat Filter with Varying the Size Parameter
The size parameter in the white top-hat filter controls the size of the structuring element used for the morphological opening operation. By changing this parameter we can adjust the scale of the features that we want to enhance i.e., the size of the small bright features where we want to isolate. Here is the example which varies the size parameter as per requirement −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import white_tophat from skimage.data import camera # Load the example image image = camera() # Apply white top-hat filter with small structuring element (size=5) result_small = white_tophat(image, size=5) # Display results 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 (size=5)") plt.imshow(result_small, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.white_tophat() which varies the size −

Black Top-Hat Filter
The Black Top-Hat Filter also called as Bottom Hat Filter, which is a morphological operation that is used to extract small dark features from an image. It works in the opposite manner of the White Top-Hat Filter, in which it highlights small bright regions.
The Black Top-Hat filter helps to isolate small dark regions i.e., dark spots against a brighter background. The mathematical definition for the Black Top-Hat Filter can be given as follows −
BlackTop-Hat = ClosedImage OriginalImage
Where −
- Closing: The closing operation consists of dilation followed by erosion which smoothens the image by filling small holes in bright regions.
- Subtracting the original image from the closed image helps to isolate small dark regions that were enhanced by the closing operation.
In Scipy we have a function namely, scipy.ndimage.black_tophat() to implement the Black Top-Hat Operation −
Syntax
Following is the syntax of the function scipy.ndimage.black_tophat() to perform the Black Top-Hat Operation on the image −
scipy.ndimage.black_tophat( input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0 )
Here are the parameters of scipy.ndimage.black_tophat() function −
- input: The input image or array, i.e., grayscale or binary.
- size: This parameter specifies the size of a square structuring element.
- footprint: A binary array that defines the shape of the structuring element.
- structure: It specifies the exact structuring element and overrides 'size' and 'footprint'.
- output: The array to store the result of the filter. If not specified then a new array will be created.
- mode: This determines how image boundaries are handled and the modes are such as 'reflect', 'constant', 'nearest', etc.
- cval: The constant value used when mode='constant' to pad the boundaries.
- origin: This parameter controls the position of the structuring element relative to the current pixel.
Black Top-Hat Filter with Default Structuring Element
Following is the example of the function scipy.ndimage.black_tophat() in which we'll apply the Black Top-Hat filter using the default structuring element i.e., a 3x3 square to the sample image (camera from skimage) −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import black_tophat from skimage.data import camera # Load the sample image image = camera() # Apply the black top-hat filter with the default structuring element (3x3 square) result = black_tophat(image, size=3) # Plot the original and filtered images side by side plt.figure(figsize=(12, 6)) # Original Image plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') # Black Top-Hat Filter Result plt.subplot(1, 2, 2) plt.title("Black Top-Hat Filter Result") plt.imshow(result, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Following is the output of the basic example implemented using scipy.ndimage.black_tophat() function −

Black Top-Hat Filter with a Custom Structuring Element
In this example we'll use a disk-shaped structuring element with a radius of 10 pixels to extract dark spots using a custom shape −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import black_tophat from skimage.morphology import disk from skimage.data import camera # Load the sample image image = camera() # Define a disk-shaped structuring element with radius 10 structuring_element = disk(10) # Apply the black top-hat filter with the custom structuring element result = black_tophat(image, structure=structuring_element) # Plot the original and filtered images side by side plt.figure(figsize=(12, 6)) # Original Image plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') # Black Top-Hat Filter Result plt.subplot(1, 2, 2) plt.title("Black Top-Hat Filter Result") plt.imshow(result, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Following is the output of the example implemented using scipy.ndimage.black_tophat() function with disk structuring element −
