Mahotas 简明教程

Mahotas - Mean Filter

均值滤波器用于平滑图像以降低噪声。它的工作原理是计算指定邻域内所有像素的平均值,然后用平均值替换原始像素的值。

假设我们有一幅灰度图像,其强度值各不相同。因此,一些像素可能比其他像素具有更高的强度值。

因此,均值滤波器通过轻微模糊图片,用于创建像素的均匀外观。

Mean Filter in Mahotas

为了在 mahotas 中应用均值滤波器,我们可以使用 mean_filter() 函数。

Mahotas 中的均值滤波器使用结构元素检查邻域中的像素。

结构元素用其相邻像素的平均值替换每个像素的值。

结构元素的大小决定平滑程度。较大的邻域效果平滑效果更强,同时减少一些更细腻的细节,而较小的邻域平滑程度较低,但保留更多细节。

The mahotas.mean_filter() function

mean_filter() 函数使用指定的邻域大小将均值滤波器应用于输入图片。

它用其邻居中的多数值替换每个像素值。经过滤波处理的图片存储在输出阵列中。

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

mahotas.mean_filter(f, Bc, mode='ignore', cval=0.0, out=None)

其中,

  1. img − 输入图片。

  2. Bc − 定义邻域的结构元素。

  3. mode (optional) − 指定函数如何处理图片的边界。它可以采用不同的值,例如“reflect”、“constant”、“nearest”、“mirror”或“wrap”。默认情况下,它设置为“ignore”,这意味着该滤波器忽略超出图片边界的那些像素。

  4. cval (optional) − 在 mode='constant' 时要使用默认值。默认值为 0.0。

  5. out (optional) − 指定将经过滤波处理的图片在其中存储的输出阵列。它必须和输入图片的形状相同。

以下是使用 mean_filter() 函数过滤图片的基本示例 −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image=mh.imread('tree.tiff', as_grey = True)
structuring_element = mh.disk(12)
filtered_image = mh.mean_filter(image, structuring_element)
# 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 mean filtered image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Mean Filtered')
axes[1].axis('off')
mtplt.show()

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

mean filter

Mean Filter with Reflect Mode

当我们将均值滤波器应用于图片时,我们需要考虑每个像素周围的那些相邻像素来计算平均值。但是,在图片的边缘,有些像素在一侧或多侧没有邻居。

为了解决这个问题,我们使用 'reflect' 模式。Reflect 模式在图片的边缘创建镜面效果。它允许我们通过镜像的方式复制图片的像素,从而虚拟扩展图片。

这样一来,我们即使在边缘也能为均值滤波器提供相邻像素。

通过镜像图片值,均值滤波器现在可以将这些镜像像素视为真实的邻居。

它使用这些虚拟邻居计算平均值,从而在图片边缘产生更准确的平滑过程。

Example

在这里,我们尝试使用反射模式计算均值滤波器 −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image=mh.imread('nature.jpeg', as_grey = True)
structuring_element = mh.morph.dilate(mh.disk(12), Bc=mh.disk(12))
filtered_image = mh.mean_filter(image, structuring_element, mode='reflect')
# 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 mean filtered image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Mean Filtered')
axes[1].axis('off')
mtplt.show()

上述代码的输出如下:

mean filter reflect mode

By Storing Result in an Output Array

我们也可以使用 Mahotas 将均值滤波的结果存储到输出数组中。为此,我们首先需要使用 NumPy 库创建一个空数组。

该数组使用与输入图像相同的形状进行初始化,以存储结果滤波图像。数组的数据类型指定为浮点(默认)。

最后,我们通过将结果滤波图像作为参数传递给 mean_filter() 函数来将其存储在输出数组中。

Example

现在,我们尝试将均值滤波应用于灰度图像,并将结果存储在特定的输出数组中 −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image=mh.imread('pic.jpg', as_grey = True)
# Create an output array for the filtered image
output = np.empty(image.shape)
# store the result in the output array
mh.mean_filter(image, Bc=mh.disk(12), out=output)
# 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 mean filtered image
axes[1].imshow(output, cmap='gray')
axes[1].set_title('Mean Filtered')
axes[1].axis('off')
mtplt.show()

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

result output array