Mahotas 简明教程
Mahotas - Mean Filter
均值滤波器用于平滑图像以降低噪声。它的工作原理是计算指定邻域内所有像素的平均值,然后用平均值替换原始像素的值。
The mean filter is used to smooth out an image to reduce noise. It works by calculating the average value of all the pixels within a specified neighborhood and then, replaces the value of the original pixel with the average value.
假设我们有一幅灰度图像,其强度值各不相同。因此,一些像素可能比其他像素具有更高的强度值。
Let’s imagine we have a grayscale image with varying intensity values. Hence, some pixels may have a higher intensity value than other pixels.
因此,均值滤波器通过轻微模糊图片,用于创建像素的均匀外观。
Thus, the mean filter is used to create a uniform appearance of the pixels by slightly blurring the image.
Mean Filter in Mahotas
为了在 mahotas 中应用均值滤波器,我们可以使用 mean_filter() 函数。
To apply the mean filter in mahotas, we can use the mean_filter() function.
Mahotas 中的均值滤波器使用结构元素检查邻域中的像素。
The mean filter in Mahotas uses a structuring element to examine pixels in a neighborhood.
结构元素用其相邻像素的平均值替换每个像素的值。
The structuring element replaces each value of pixel with the average value of its neighbouring pixels.
结构元素的大小决定平滑程度。较大的邻域效果平滑效果更强,同时减少一些更细腻的细节,而较小的邻域平滑程度较低,但保留更多细节。
The size of the structuring element determines the extent of smoothing. A larger neighborhood results in stronger smoothing effect, while reducing some finer details, whereas a smaller neightborhood results in less smoothing but maintains more details.
The mahotas.mean_filter() function
mean_filter() 函数使用指定的邻域大小将均值滤波器应用于输入图片。
The mean_filter() function applies the mean filter to the input image using the specified neighborhood size.
它用其邻居中的多数值替换每个像素值。经过滤波处理的图片存储在输出阵列中。
It replaces each pixel value with the majority value among its neighbors. The filtered image is stored in the output array.
以下是 mahotas 中 mean filter() 函数的基本语法 −
Following is the basic syntax of the mean filter() function in mahotas −
mahotas.mean_filter(f, Bc, mode='ignore', cval=0.0, out=None)
其中,
Where,
-
img − It is the input image.
-
Bc − It is the structuring element that defines the neighbourhood.
-
mode (optional) − It specifies how the function handles the borders of the image. It can take different values such as 'reflect', 'constant', 'nearest', 'mirror' or 'wrap'. By default, it is set to 'ignore', which means the filter ignores pixels beyond the image’s borders.
-
cval (optional) − The value to be used when mode='constant'. The default value is 0.0.
-
out (optional) − It specifies the output array where the filtered image will be stored. It must be of the same shape as the input image.
以下是使用 mean_filter() 函数过滤图片的基本示例 −
Following is the basic example to filter the image using the mean_filter() function −
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
Mean Filter with Reflect Mode
当我们将均值滤波器应用于图片时,我们需要考虑每个像素周围的那些相邻像素来计算平均值。但是,在图片的边缘,有些像素在一侧或多侧没有邻居。
When we apply the mean filter to an image, we need to consider the neighboring pixels around each pixel to calculate the average. However, at the edges of the image, there are pixels that don’t have neighbors on one or more sides.
为了解决这个问题,我们使用 'reflect' 模式。Reflect 模式在图片的边缘创建镜面效果。它允许我们通过镜像的方式复制图片的像素,从而虚拟扩展图片。
To address this issue, we use the 'reflect' mode. Reflect mode creates a mirror−like effect along the edges of the image. It allows us to virtually extend the image by duplicating its pixels in a mirrored manner.
这样一来,我们即使在边缘也能为均值滤波器提供相邻像素。
This way, we can provide the mean filter with neighboring pixels even at the edges.
通过镜像图片值,均值滤波器现在可以将这些镜像像素视为真实的邻居。
By reflecting the image values, the mean filter can now consider these mirrored pixels as if they were real neighbors.
它使用这些虚拟邻居计算平均值,从而在图片边缘产生更准确的平滑过程。
It calculates the average value using these virtual neighbors, resulting in a more accurate smoothing process at the image edges.
Example
在这里,我们尝试使用反射模式计算均值滤波器 −
In here, we are trying to calculate the mean filter with the reflect mode −
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()
上述代码的输出如下:
Output of the above code is as follows −
By Storing Result in an Output Array
我们也可以使用 Mahotas 将均值滤波的结果存储到输出数组中。为此,我们首先需要使用 NumPy 库创建一个空数组。
We can store the result of the mean filter in an output array as well using Mahotas. To achieve this, we first need to create an empty array using the NumPy library.
该数组使用与输入图像相同的形状进行初始化,以存储结果滤波图像。数组的数据类型指定为浮点(默认)。
This array is initialized with the same shape as the input image to store the resultant filtered image. The data type of the array is specified as float (default).
最后,我们通过将结果滤波图像作为参数传递给 mean_filter() 函数来将其存储在输出数组中。
Finally, we store the resultant filtered image in the output array by passing it as a parameter to the mean_filter() function.
Example
现在,我们尝试将均值滤波应用于灰度图像,并将结果存储在特定的输出数组中 −
Now, we are trying to apply mean filter to a grayscale image and store the result in a specific output array −
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()
以下是上面代码的输出: -
Following is the output of the above code −