SciPy - Ndimage



The SciPy ndimage module is part of the scipy.ndimage sub-package which provides functions for image processing with focus on multi-dimensional (n-dimensional) arrays.

This module is very useful for scientific and engineering applications where multi-dimensional image or array processing is required. It contains various operations such as filters, transformations, measurements and more that work on n-dimensional arrays.

Features of ndimage module of SciPy

Following are the main features of ndimage module of SciPy −

  • Filtering: Smooth or enhance images with the help of Gaussian filters, median filters, etc.
  • Morphological Operations: Operations such as dilation, erosion, etc which are commonly used in image analysis.
  • Labeling and Measurements: For labeling connected regions in an array and performing region-based measurements.
  • Geometric transformations: We can perform Rotation, zoom, affine transformations, etc.
  • Interpolation: The Ndimage module is used for resampling arrays or images.
  • Convolution and Correlation: This module is helpful for applying custom kernels to the data.

Functions in ndimage Module

The Scipy ndimage module provides different functions which operate on N-dimensional arrays and are widely used for tasks such as filtering, interpolation, transformation and measurement in fields such as image processing, computer vision and scientific computing.

Following are the various functions of the scipy.ndimage module −

S.No. Function & Description
1 scipy.ndimage.convolve()
Convolve an input array with a given kernel (filter).
2 scipy.ndimage.gaussian_filter()
Apply a Gaussian filter for smoothing (blur) an image.
3 scipy.ndimage.median_filter()
Apply a median filter for noise reduction, especially salt-and-pepper noise.
4 scipy.ndimage.rotate()
Rotate an input image or n-dimensional array by a specified angle (in degrees).
5 scipy.ndimage.zoom()
Rescale (zoom) an array by a specified factor.
6 scipy.ndimage.binary_erosion()
Perform erosion on a binary image (shrinking).
7 scipy.ndimage.binary_dilation()
Perform dilation on a binary image (expanding).
8 scipy.ndimage.binary_opening()
Perform binary opening i.e., erosion followed by dilation.
9 scipy.ndimage.binary_closing()
Perform binary closing i.e., dilation followed by erosion.
10 scipy.ndimage.label()
Label connected components in a binary image or multi-dimensional array.
11 scipy.ndimage.distance_transform_edt()
Compute the Euclidean distance transform of a binary image.
12 scipy.ndimage.interpolate()
Perform interpolation when resizing an array or image.
13 scipy.ndimage.fourier_gaussian()
Apply a Gaussian filter in the Fourier domain.
14 scipy.ndimage.fourier_shift()
Apply a shift in the frequency domain.
15 scipy.ndimage.mean()
Compute the mean of values in an array for each labeled region.
16 scipy.ndimage.variance()
Compute the variance for each labeled region in an array.
17 scipy.ndimage.standard_deviation()
Compute the standard deviation for each labeled region in an array.
18 scipy.ndimage.find_objects()
Return slice objects corresponding to the labeled regions in an array.
19 scipy.ndimage.grey_erosion()
Shrinks bright regions in the image.
20 scipy.ndimage.grey_dilation()
Expands bright regions in the image.
21 scipy.ndimage.correlate()
Performs cross-correlation with a user-defined kernel.
22 scipy.ndimage.generic_gradient_magnitude()
Computes the gradient magnitude using a custom gradient function.
23 scipy.ndimage.prewitt()
Computes the Prewitt gradient which is similar to Sobel but with simpler kernels.
24 scipy.ndimage.sobel()
Computes the Sobel gradient of the image along a specified axis.
25 scipy.ndimage.laplace()
Computes the Laplacian of the image to detect edges.

Applications of Ndimage module

The scipy.ndimage module has a wide range of applications across various fields because to its versatile image processing and multi-dimensional data manipulation capabilities. Following are the applications of scipy.ndimage module −

Application Key Functions used Use Cases
Noise Reduction gaussian_filter, median_filter Smoothing noisy images, pre-processing for edge detection.
Edge Detection sobel, prewitt, laplace Object boundary detection in images.
Geometric Transformations rotate, zoom, shift, affine_transform Resizing, aligning or augmenting image data.
Morphological Operations binary_erosion, binary_dilation Removing noise or filling holes in binary images.
Object Segmentation label, distance_transform_edt Isolating objects for measurement or analysis.
Interpolation and Resampling map_coordinates, zoom Resizing images or realigning multi-modal data.
Data Measurement center_of_mass, sum, mean Quantitative analysis of labeled regions.
Advertisements