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 Horizonatal

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 −

Sobel Filter Vertical

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 −

Sobel Filter constant mode
Advertisements