
- 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.sobel() Function
The scipy.ndimage.sobel() function in SciPy applies the Sobel filter to an image to compute its gradient along a specified axis. It is used for edge detection by highlighting areas of high intensity change.
This function computes the derivative of an image using convolution with Sobel kernels in either the horizontal (axis=0) or vertical (axis=1) direction. The result is an image that emphasizes edges in the chosen direction.
This function offers various options for handling boundaries (mode) and padding values (cval). It returns an array containing the filtered result of the gradient operation.
Syntax
Following is the syntax of the function scipy.ndimage.sobel() to apply sobel filter on an image −
scipy.ndimage.sobel(input, axis=-1, output=None, mode='reflect', cval=0.0)
Parameters
Below are the parameters of the scipy.ndimage.sobel() function −
- input (array_like): The input image or array on which the Sobel filter will be applied.
-
axis (int, optional): This specifies the axis along which to compute the derivative (gradient). This parameter takes the below values −
- 0: When 0 is defined then the Sobel filter applied to detect edges in the vertical direction i.e., horizontal gradient.
- 1: When 1 is defined then the Sobel filter applied to detect edges in the horizontal direction i.e., vertical gradient.
- -1: This is the default value which applies the Sobel filter to the last axis of the input i.e., works for multi-dimensional data.
- output (array or dtype, optional): The array in which to store the result. If None (default) then a new array will be created to store the output.
-
mode (str, optional): This parameter determines how the input array is extended beyond its boundaries. This affects how the Sobel filter handles edge pixels. Following are the modes we can use −
- reflect: This is the default value which reflects the input array at the borders for example the first pixel value is mirrored.
- constant: Pads with a constant value (specified by cval).
- nearest: Pads with the nearest boundary value.
- mirror: Mirrors the values at the boundary like 'reflect'.
- wrap: Wraps the array from the opposite side.
- cval (scalar, optional):: The constant value used when mode='constant'. This value will be used to pad the edges of the image when it goes out of bounds.
Return Value
The scipy.ndimage.sobel() function returns the gradient of the image along the specified axis.
Sobel filter applied on Horizontal Direction
The Sobel filter is a gradient-based edge detection method that highlights changes in intensity in specific directions. By applying the Sobel filter in the horizontal direction we can detect vertical edges in an image where significant intensity changes occur from left to right or right to left. Below is the example of using the scipy.ndimage.sobel() function in the Horizontal direction −
from scipy.ndimage import sobel from skimage import data import matplotlib.pyplot as plt # Load a sample grayscale image image = data.camera() # Apply Sobel filter to detect edges in the horizontal direction sobel_horizontal = sobel(image, axis=1) # Horizontal gradient (detects vertical edges) # Display the original image and the result 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("Sobel - Horizontal Direction") plt.imshow(sobel_horizontal, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Here is the output of the scipy.ndimage.sobel() function applied on Horizontal Direction −

Sobel filter applied on Vertical Direction
When applied in the vertical direction then the Sobel filter highlights horizontal edges where the intensity changes across the rows of the image. Following is the example of using the scipy.ndimage.sobel() function on the vertical direction −
from scipy.ndimage import sobel from skimage import data import matplotlib.pyplot as plt # Load a sample grayscale image image = data.camera() # Apply Sobel filter to detect edges in the vertical direction sobel_vertical = sobel(image, axis=0) # Vertical gradient (detects horizontal edges) # Display the original image and the Sobel filter result plt.figure(figsize=(10, 5)) # Original Image plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') # Sobel Filter Result (Vertical Edges) plt.subplot(1, 2, 2) plt.title("Sobel - Vertical Direction") plt.imshow(sobel_vertical, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Below is the output of the scipy.ndimage.sobel() function applied on vertical Direction −

Applying Sobel Filter Using a Different Mode
In image processing when applying filters like Sobel for handling image boundaries is important because the filter needs to operate on pixels that may not have neighboring pixels outside the image boundaries. The mode parameter in scipy.ndimage.sobel() function defines how the image is extended beyond its boundaries.
Here in this example let's apply the Sobel filter using a different boundary handling mode such as 'constant' which pads the image with a constant value at the boundaries −
from scipy.ndimage import sobel from skimage import data import matplotlib.pyplot as plt # Load a sample grayscale image image = data.camera() # Apply Sobel filter to detect edges in the horizontal direction with 'constant' mode padding sobel_constant = sobel(image, axis=1, mode='constant', cval=255) # Pad edges with 255 (white) # Display the original image and the Sobel filter result plt.figure(figsize=(10, 5)) # Original Image plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') # Sobel Filter Result with 'constant' mode padding plt.subplot(1, 2, 2) plt.title("Sobel - 'constant' Mode Padding") plt.imshow(sobel_constant, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Below is the output of the scipy.ndimage.sobel() function applied in constant mode −
