![Mahotas](/mahotas/images/mahotas-mini-logo.jpg)
- Mahotas Tutorial
- Mahotas - Home
- Mahotas - Introduction
- Mahotas - Computer Vision
- Mahotas - History
- Mahotas - Features
- Mahotas - Installation
- Mahotas Handling Images
- Mahotas - Handling Images
- Mahotas - Loading an Image
- Mahotas - Loading Image as Grey
- Mahotas - Displaying an Image
- Mahotas - Displaying Shape of an Image
- Mahotas - Saving an Image
- Mahotas - Centre of Mass of an Image
- Mahotas - Convolution of Image
- Mahotas - Creating RGB Image
- Mahotas - Euler Number of an Image
- Mahotas - Fraction of Zeros in an Image
- Mahotas - Getting Image Moments
- Mahotas - Local Maxima in an Image
- Mahotas - Image Ellipse Axes
- Mahotas - Image Stretch RGB
- Mahotas Color-Space Conversion
- Mahotas - Color-Space Conversion
- Mahotas - RGB to Gray Conversion
- Mahotas - RGB to LAB Conversion
- Mahotas - RGB to Sepia
- Mahotas - RGB to XYZ Conversion
- Mahotas - XYZ to LAB Conversion
- Mahotas - XYZ to RGB Conversion
- Mahotas - Increase Gamma Correction
- Mahotas - Stretching Gamma Correction
- Mahotas Labeled Image Functions
- Mahotas - Labeled Image Functions
- Mahotas - Labeling Images
- Mahotas - Filtering Regions
- Mahotas - Border Pixels
- Mahotas - Morphological Operations
- Mahotas - Morphological Operators
- Mahotas - Finding Image Mean
- Mahotas - Cropping an Image
- Mahotas - Eccentricity of an Image
- Mahotas - Overlaying Image
- Mahotas - Roundness of Image
- Mahotas - Resizing an Image
- Mahotas - Histogram of Image
- Mahotas - Dilating an Image
- Mahotas - Eroding Image
- Mahotas - Watershed
- Mahotas - Opening Process on Image
- Mahotas - Closing Process on Image
- Mahotas - Closing Holes in an Image
- Mahotas - Conditional Dilating Image
- Mahotas - Conditional Eroding Image
- Mahotas - Conditional Watershed of Image
- Mahotas - Local Minima in Image
- Mahotas - Regional Maxima of Image
- Mahotas - Regional Minima of Image
- Mahotas - Advanced Concepts
- Mahotas - Image Thresholding
- Mahotas - Setting Threshold
- Mahotas - Soft Threshold
- Mahotas - Bernsen Local Thresholding
- Mahotas - Wavelet Transforms
- Making Image Wavelet Center
- Mahotas - Distance Transform
- Mahotas - Polygon Utilities
- Mahotas - Local Binary Patterns
- Threshold Adjacency Statistics
- Mahotas - Haralic Features
- Weight of Labeled Region
- Mahotas - Zernike Features
- Mahotas - Zernike Moments
- Mahotas - Rank Filter
- Mahotas - 2D Laplacian Filter
- Mahotas - Majority Filter
- Mahotas - Mean Filter
- Mahotas - Median Filter
- Mahotas - Otsu's Method
- Mahotas - Gaussian Filtering
- Mahotas - Hit & Miss Transform
- Mahotas - Labeled Max Array
- Mahotas - Mean Value of Image
- Mahotas - SURF Dense Points
- Mahotas - SURF Integral
- Mahotas - Haar Transform
- Highlighting Image Maxima
- Computing Linear Binary Patterns
- Getting Border of Labels
- Reversing Haar Transform
- Riddler-Calvard Method
- Sizes of Labelled Region
- Mahotas - Template Matching
- Speeded-Up Robust Features
- Removing Bordered Labelled
- Mahotas - Daubechies Wavelet
- Mahotas - Sobel Edge Detection
Mahotas - Sobel Edge Detection
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 convolving 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 −
![Sobel Edge Detection](/mahotas/images/sobel_edge_detection.jpg)
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 −
![Thresholding Output Image](/mahotas/images/thresholding_output_image.jpg)
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 Bernsen thresholding algorithm. 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 −
![Thresholding Image](/mahotas/images/thresholding_image.jpg)