Sobel edge detection is an algorithm used to identify the edges in an image. Edges represent boundaries between different regions. It works by calculating the gradient of the image intensity at each pixel.
In simpler terms, it measures the change in pixel values to determine regions of high variation, which correspond to edges in an image.
Sobel Edge Detection in Mahotas
In Mahotas, we can use the mahotas.sobel() function to detect edges in an image.
The Sobel function uses two separate filters, one for horizontal changes (Gx) and another for vertical changes (Gy).
These filters are applied to the image by them with the pixel values of the image. This calculates the gradients in the horizontal and the vertical directions.
Once the gradients in both directions are obtained, the Sobel function combines them to calculate the overall gradient magnitude at each pixel.
This is done using the Pythagorean theorem, which calculates the square root of the sum of the squares of the horizontal and vertical gradients.
$$\mathrm{M:=:sqrt{(Gx^{2}:+:Gy^{2})}}$$
The resulting gradient magnitude (M) of the image represents the strength of the edges in the original image. Higher values indicate stronger edges, while lower values correspond to smoother regions.
The mahotas.sobel() function
The mahotas.sobel() function takes a grayscale image as an input and returns a binary image as output, where the edges are computed using Sobel edge detection algorithm.
The white pixels in the resultant image represent the edges, while the black pixels represent the other areas.
Syntax
Following is the basic syntax of the sobel() function in mahotas −
mahotas.sobel(img, just_filter=False)
Where,
-
img − It is the input grayscale image.
-
just_filter (optional) − It is a flag which specifies whether to threshold the filtered image (default value is false).
Example
In the following example we are using Sobel edge detection algorithm to detect edges using the mh.sobel() function.
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(''sea.bmp'') # Converting it to grayscale image = mh.colors.rgb2gray(image) # Applying sobel gradient to detect edges sobel = mh.sobel(image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(''Original Image'') axes[0].set_axis_off() # Displaying the edges axes[1].imshow(sobel) axes[1].set_title(''Sobel Edge Detection'') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
Output
Following is the output of the above code −
Without Thresholding output Image
We can also perform Sobel edge detection algorithm without thresholding the output image. Thresholding refers to conversion of an image into a binary image by classifying the pixels into the foreground or the background.
The conversion occurs by comparing the intensity value of the pixels with the threshold (fixed) value.
In mahotas, the just_filter parameter in the sobel() function determines whether to threshold the output image. We can set this parameter to ”True” to prevent the thresholding of the output image.
If the filter is set to ”False” then the thresholding occurs on the output image.
Example
In the example mentioned below, we are not thresholding the output image when using the Sobel edge detection algorithm.
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(''sun.png'') # Converting it to grayscale image = mh.colors.rgb2gray(image) # Applying sobel gradient to detect edges sobel = mh.sobel(image, just_filter=True) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(''Original Image'') axes[0].set_axis_off() # Displaying the edges axes[1].imshow(sobel) axes[1].set_title(''Sobel Edge Detection'') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
Output
After executing the above code, we get the following output −
On a Threshold Image
Sobel edge detection can also be performed on a threshold image. A threshold image is a binary image where the pixels are classified into the foreground or the background.
The foreground pixels are white and represented by the value 1, while the background pixels are black and represented by the value 0.
In mahotas, we first threshold the input image using any thresholding algorithm. Let us assume . This can be done by using the mh.thresholding.bernsen() function on a grayscale image.
Then, we apply the Sobel edge detection algorithm to detect edges of the threshold image.
Example
In here, we are detecting edges of an image using Sobel edge detection algorithm on a threshold image.
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread(''tree.tiff'') # Converting it to grayscale image = mh.colors.rgb2gray(image) # Applying threshold on the image threshold_image = mh.thresholding.bernsen(image, 17, 19) # Applying sobel gradient to detect edges sobel = mh.sobel(threshold_image) # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 2) # Displaying the original image axes[0].imshow(image) axes[0].set_title(''Original Image'') axes[0].set_axis_off() # Displaying the edges axes[1].imshow(sobel) axes[1].set_title(''Sobel Edge Detection'') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
Output
After executing the above code, we get the following output −