Mahotas 简明教程

Mahotas - 2D Laplacian Filter

拉普拉斯滤波器用于检测图像中边缘和强度变化。从数学上讲,拉普拉斯滤波器定义为图像在 x 和 y 方向的二阶导数之和。

A Laplacian filter is used to detect edges and changes in intensity within an image. Mathematically, the Laplacian filter is defined as the sum of the second derivatives of the image in the x and y directions.

二阶导数提供有关每个像素强度变化率的信息。

The second derivative provides information about the rate of change of intensity of each pixel.

拉普拉斯滤波器强调图像中强度快速变化的区域,例如边缘和角点。它的工作原理是从中心像素中减去周围像素的平均值,从而得出强度二阶导数的度量。

The Laplacian filter emphasizes regions of an image where the intensity changes rapidly, such as edges and corners. It works by subtracting the average of the surrounding pixels from the center pixel, which gives a measure of the second derivative of intensity.

在 2D 中,拉普拉斯滤波器通常由方阵表示,通常为 3×3 或 5×5。以下是 3×3 拉普拉斯滤波器的一个示例:

In 2D, the Laplacian filter is typically represented by a square matrix, often 3×3 or 5×5. Here’s an example of a 3×3 Laplacian filter −

0  1  0
1 -4  1
0  1  0

2D Laplacian Filter in Mahotas

若要在 mahotas 中应用二维拉普拉斯滤波器,可以使用 mahotas.laplacian_2D() 函数。以下是 Mahotas 中二维拉普拉斯滤波器的工作原理概述:

To apply the 2D Laplacian filter in mahotas, we can use the mahotas.laplacian_2D() function. Here is an overview of the working of the 2D Laplacian filter in Mahotas −

Input Image

  1. The filter takes a grayscale input image.

Convolution

  1. The Laplacian filter applies a convolution operation on the input image using a kernel. The kernel determines the weights applied to the neighboring pixels during convolution.

  2. The convolution operation involves sliding the kernel over the entire image. At each pixel position, the Laplacian filter multiplies the corresponding kernel weights with the pixel values in the neighborhood and calculates the sum.

Laplacian Response

  1. The Laplacian response is obtained by applying the Laplacian operator to the image. It represents the intensity changes or discontinuities in the image, which are associated with edges.

The mahotas.laplacian_2D() function

mahotas.laplacian_2D() 函数以灰度图像作为输入,并对其执行 2D 拉普拉斯运算。生成图像高亮显示快速强度变化的区域,例如边缘。

The mahotas.laplacian_2D() function takes a grayscale image as input and performs a 2D laplacian operation on it. The resulting image highlights regions of rapid intensity changes, such as edges.

以下是 mahotas 中 laplacian_2D() 函数的基本语法 −

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

mahotas.laplacian_2D(array, alpha=0.2)

其中,

Where,

  1. array − It is the input image.

  2. alpha (optional) − It is a scalar value between 0 and 1 that controls the shape of Laplacian filter. A larger value of alpha increases the sensitivity of the Laplacian filter to the edges. The default value is 0.2.

以下是使用 laplacian_2D() 函数检测图像中的边缘的基本示例 −

Following is the basic example of detecting edges in an image using the laplacian_2D() function −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image = mh.imread('picture.jpg', as_grey = True)
# Applying a laplacian filter
filtered_image = mh.laplacian_2D(image)
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the laplacian filtered featured image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Laplacian Filtered')
axes[1].axis('off')
mtplt.show()

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

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

2D laplacian filter

Scaling the Laplacian Response

通过改变 alpha 参数,我们可以控制拉普拉斯响应的尺度。较小的 alpha 值(例如 0.2)会产生相对微妙的响应,突出显示更精细的细节和边缘。

By varying the alpha parameter, we can control the scale of the Laplacian response. A smaller alpha value, such as 0.2, produces a relatively subtle response, highlighting finer details and edges.

另一方面,较大的 alpha 值(例如 0.8)会放大响应,使其更加明显,并强调更突出的边缘和结构。

On the other hand, a larger alpha value, like 0.8, amplifies the response, making it more pronounced and emphasizing more prominent edges and structures.

因此,我们可以在拉普拉斯算子中使用不同的 alpha 值来反映边缘检测的变化。

Hence, we can use different alpha values in the laplacian filter to reflect the changes in edge detection.

Example

在此处,我们正在拉普拉斯算子中使用不同的 alpha 值来反映边缘检测的变化 −

In here, we are using different alpha values in the laplacian filter to reflect the changes in edge detection −

import mahotas as mh
import matplotlib.pyplot as plt
# Load an example image
image = mh.imread('pic.jpg', as_grey=True)
# Apply the Laplacian filter with different alpha values
filtered_image_1 = mh.laplacian_2D(image, alpha=0)
filtered_image_2 = mh.laplacian_2D(image, alpha=0.5)
filtered_image_3 = mh.laplacian_2D(image, alpha=1)
# Display the original and filtered images with different scales
fig, axes = plt.subplots(1, 4, figsize=(12, 3))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
axes[1].imshow(filtered_image_1, cmap='gray')
axes[1].set_title('Filtered Image (alpha=0)')
axes[1].axis('off')
axes[2].imshow(filtered_image_2, cmap='gray')
axes[2].set_title('Filtered Image (alpha=0.5)')
axes[2].axis('off')
axes[3].imshow(filtered_image_3, cmap='gray')
axes[3].set_title('Filtered Image (alpha=1)')
axes[3].axis('off')
plt.show()

Followng 是上述代码的输出 −

Followng is the output of the above code −

2D laplacian filter1

Using Randomly Generated Array

我们还可以对随机生成的数组应用拉普拉斯算子。要实现此目的,我们首先需要创建一个随机的 2D 数组。我们可以使用 NumPy 库的 np.random.rand() 函数创建数组。

We can apply laplacian filter on a randomly generated array as well. To achieve this, first we need to create a random 2D array. We can create the array using the np.random.rand() function of the NumPy library.

此函数生成 0 和 1 之间的值,表示数组的像素强度。

This function generates values between 0 and 1, representing the pixel intensities of the array.

接下来,我们把随机生成的数组传递给 mahotas.laplacian_2D() 函数。此函数将拉普拉斯算子应用于输入数组并返回经过滤的数组。

Next, we pass the randomly generated array to the mahotas.laplacian_2D() function. This function applies the Laplacian filter to the input array and returns the filtered array.

Example

现在,我们尝试对随机生成的数组应用拉普拉斯算子 −

Now, we are trying to apply laplacian filter to a randomly generated array −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
# Generate a random 2D array
array = np.random.rand(100, 100)
# Apply the Laplacian filter
filtered_array = mh.laplacian_2D(array)
# Display the original and filtered arrays
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(array, cmap='gray')
plt.title('Original Array')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(filtered_array, cmap='gray')
plt.title('Filtered Array')
plt.axis('off')
plt.show()

上述代码的输出如下:

Output of the above code is as follows −

randomly generated array