Sobel operator in digital image processing

In computer vision and digital image processing, the Sobel operator is a popular algorithm for edge detection used to identify and highlight the boundaries within an image. The Sobel operator performs a 2D spatial gradient operation on the image, primarily providing the areas of high spatial frequency corresponding to edges.

Edge detection is critical in many applications, such as object detection and recognition, scene understanding, and feature extraction. By understanding where an object starts and ends, computers can identify and categorize the thing more efficiently.

In this Answer, we will get an overview of the Sobel operator and learn its implementation in Python.

Delving into the Sobel operator

Essentially, the Sobel operator is a mechanism that estimates the changes in the intensity levels of an image. It's like a tool that helps us see where colors in a picture shift suddenly or gently, hinting at the edges or boundaries within the image.

This operator consists of two 3x3 matrices, known as convolution matrices. One is for gauging changes in the horizontal direction (x), while the other does so in the vertical direction (y). Here's a peek at these matrices:

Horizontal (Gx):

-1 0 1
-2 0 2
-1 0 1
Horizontal convolution matrix

Vertical (Gy):

-1 -2 -1
0 0 0
1 2 1
Vertical convolution matrix

These specific arrangements are crafted to detect edges running up and down or side to side relative to the pixels in the image.

Python implementation

Imagine we have a photograph, and we would like to find the edges within it using Python. Here's how we might do that using the OpenCV library.

This is the image we will be using:

Original image
Original image

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Open the picture
img = cv2.imread('eagle.jpeg', cv2.IMREAD_GRAYSCALE)

# Apply the Sobel magic
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)

# Merge the x and y gradient's power
sobel = np.hypot(sobel_x, sobel_y)

# Normalize between 0 and 255
sobel = (sobel / sobel.max()) * 255

# Show the before and after pictures
plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray')
plt.title('Before'), plt.xticks([]), plt.yticks([])

plt.subplot(1, 2, 2), plt.imshow(sobel, cmap='gray')
plt.title('After'), plt.xticks([]), plt.yticks([])

plt.show()

This snippet of code lets us compare our original picture with the edge-detected version, thanks to the Sobel operator.

Output

Output of the code
Output of the code

The output of the code displays two images side by side. The image on the left is the original grayscale image, and the image on the right is the same after applying the Sobel operator, which highlights the edges in the image.

Code explanation

  • Lines 1–3: Import the necessary libraries. cv2 is used for image processing (including reading the image and applying the Sobel operator), np is used for numerical operations, and plt is used for visualizing the images.

  • Line 4: Open the picture 'eagle.jpeg' in grayscale mode using the OpenCV function cv2.imread().

  • Lines 7–8: Apply the Sobel operator in the x-direction and y-direction on the grayscale image to compute the gradient in each direction. The Sobel operator is used to detect edges in the image.

  • Line 11: Compute the combined gradient magnitude by taking the element-wise square root of the sum of the squares of sobel_x and sobel_y. This gives us the magnitude of the gradient at each pixel, combining the x and y directions.

  • Line 14: Normalize the gradient values between 0 and 255. This step ensures that the resulting image can be displayed correctly using 8-bit grayscale values.

  • Lines 17–20: Use Matplotlib to display the original image and the Sobel-filtered image side by side. plt.subplot() is used to create two subplots (1 row, 2 columns) and plt.imshow() displays the images on each subplot. plt.title() sets the titles for the subplots, and plt.xticks([]) and plt.yticks([]) remove the axis ticks.

  • Line 21: Display the plots using plt.show().

Limitations of the Sobel operator

While remarkable in many ways, the Sobel operator has its limitations. It's not always adept at handling edges that don't align neatly with the vertical and horizontal. Also, its sensitivity to image noise can be an issue. Using additional techniques like Gaussian smoothingGaussian smoothing is a technique used in image processing to reduce noise and detail in an image, applying a Gaussian function to create a blurred, 'smoothed' version of the original image. can help, though.

Conclusion

Despite its limitations, the Sobel operator remains a fundamental tool in image processing for edge detection. Its simplicity and performance make it a popular choice for many applications. Understanding how the Sobel operator works and how to implement it forms an essential part of understanding digital image processing.

Test your knowledge

Match The Answer
Select an option from the left-hand side

Edge detection

Sensitive to noise, not effective for non-vertical/horizontal edges

Sobel operator

3x3 matrices for gauging changes in the image intensity in the horizontal and vertical directions

Convolution matrices

Mechanism used to detect boundaries in an image


Free Resources

Copyright ©2024 Educative, Inc. All rights reserved