Mahotas 简明教程

Mahotas - Sobel Edge Detection

Sobel 边缘检测是一种用于识别图像中边缘的算法。边缘代表不同区域之间的边界。其工作原理是计算每个像素处图像强度的梯度。

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

在 Mahotas 中,我们可以使用 mahotas.sobel() 函数检测图像中的边缘。

In Mahotas, we can use the mahotas.sobel() function to detect edges in an image.

Sobel 函数使用两个单独的滤波器,一个用于水平变化 (Gx),另一个用于垂直变化 (Gy)。

The Sobel function uses two separate filters, one for horizontal changes (Gx) and another for vertical changes (Gy).

这些滤波器通过将它们 convolving 与图像的像素值来应用于图像。这会计算水平和垂直方向上的梯度。

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.

一旦获得了两个方向上的梯度,Sobel 函数会将它们结合起来计算每个像素处的总体梯度幅度。

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})}}

\mathrm{M\:=\:\sqrt{(Gx{2}\:+\:Gy{2})}}

图像的生成梯度幅度 (M) 表示原始图像中边缘的强度。较高的值表示较强的边缘,而较低的值则对应于较平滑的区域。

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

mahotas.sobel() 函数以灰度图像作为输入并返回一个二值图像作为输出,其中使用 Sobel 边缘检测算法计算边缘。

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.

下面是 mahotas 中 sobel() 函数的基本语法−

Following is the basic syntax of the sobel() function in mahotas −

mahotas.sobel(img, just_filter=False)

其中,

Where,

  1. img − It is the input grayscale image.

  2. just_filter (optional) − It is a flag which specifies whether to threshold the filtered image (default value is false).

在以下示例中,我们使用 Sobel 边缘检测算法使用 mh.sobel() 函数检测边缘。

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()

以下是上面代码的输出: -

Following is the output of the above code −

sobel edge detection

Without Thresholding output Image

我们还可以执行 Sobel 边缘检测算法,而不对输出图像进行阈值处理。阈值处理是指通过将像素分类到前景或背景中来将图像转换为二值图像。

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.

在 mahotas 中,sobel() 函数中的 just_filter 参数确定是否对输出图像进行阈值处理。我们可以将此参数设置为“真”以防止对输出图像进行阈值处理。

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.

如果将 filter 设置为“假”,则将对输出图像进行阈值处理。

If the filter is set to 'False' then the thresholding occurs on the output image.

Example

在下面提到的示例中,我们在使用 Sobel 边缘检测算法时没有对输出图像进行阈值处理。

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()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

thresholding output image

On a Threshold Image

还可以对阈值图像执行 Sobel 边缘检测。阈值图像是一种二值图像,其中像素被分类到前景或背景中。

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.

前景像素为白色,由值 1 表示,而背景像素为黑色,由值 0 表示。

The foreground pixels are white and represented by the value 1, while the background pixels are black and represented by the value 0.

在 mahotas 中,我们首先使用任何阈值处理算法对输入图像进行阈值处理。让我们假设 Bernsen thresholding algorithm 。这可以通过在灰度图像上使用 mh.thresholding.bernsen() 函数来完成。

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.

然后,我们应用 Sobel 边缘检测算法来检测阈值图像的边缘。

Then, we apply the Sobel edge detection algorithm to detect edges of the threshold image.

Example

在这里,我们在阈值图像上使用 Sobel 边缘检测算法检测图像的边缘。

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()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

thresholding image