Mahotas 简明教程

Mahotas - Majority Filter

多数滤波器用于去除图像中的噪声。它的工作原理是查看图像中的像素并考虑其相邻像素。

The majority filter is used to remove the noise from an image. It works by looking at a pixel in an image and considering its neighboring pixels.

多数滤波器计算其相邻像素中最常见的像素值,并将原始像素值替换为该常见值。

The majority filter calculates the most common pixel value among its neighbors and replaces the original pixel value with that common value.

想象一下你有一幅黑白图像,其中白色代表你感兴趣的对象,而黑色代表背景。

Imagine you have a black−and−white image where white represents the object you’re interested in and black represents the background.

但由于各种原因,对象周围可能散落着一些小的黑点(噪声)。

However, due to various reasons, there might be some small black dots (noise) scattered around the object.

因此,为了降低噪声,它统计有多少相邻像素为黑色,有多少为白色。然后,用其相邻像素中出现频率最高的颜色(黑色或白色)替换原始像素值。

So to reduce the noise, it counts how many neighboring pixels are black and how many are white. Then, it replaces the original pixel value with the color (black or white) that appears most frequently among its neighbors.

Majority Filter in Mahotas

要应用 Mahotas 中的大多数滤波器,我们可以使用 majority_filter() 函数。

To apply the majority filter in mahotas, we can use the majority_filter() function.

Mahotas 中的大多数滤波器使用结构元素来检查邻域中的像素。

The majority filter in Mahotas uses a structuring element to examine pixels in a neighborhood.

结构元素统计邻域中的像素值,然后用最常见的数值替换每个像素的值来减少噪声。

The structuring element counts the pixel values within the neightborhood and replaces the value of each pixel with the most common value to reduce noise.

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

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.majority_filter() function

majority_filter() 函数利用指定邻域大小对输入图像应用大多数滤波器。

The majority_filter() function applies the majority 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 中大多数滤波器的基本语法 −

Following is the basic syntax of the majority filter in mahotas −

mahotas.majority_filter(img, N=3, out={np.empty(img.shape, bool)})

其中,

Where,

  1. img − It is the input image.

  2. N − It is the size of the filter. It must be an odd integer. The default value is 3.

  3. Out (optional) − It specifies the output array where the filtered image will be stored. It must be an empty boolean array with the same size as the input image.

下面是使用 majority_filter() 函数滤波图像的基本示例 −

Following is the basic example to filter the image using the majority_filter() function −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image=mh.imread('picture.jpg', as_grey = True)
filtered_image = mh.majority_filter(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 majority filtered image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Majority Filtered')
axes[1].axis('off')
mtplt.show()

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

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

majority filter

By Specifying Window Size

要指定 Mahotas 中的窗口尺寸,需要将它作为参数传递给 majority_filter() 函数。

To specify the window size in Mahotas, we need to pass it as a parameter to the majority_filter() function.

窗口尺寸是要用于决定图像中每个像素大多数值的像素数量。

The window size is the number of pixels that will be used to determine the majority value for each pixel in the image.

窗口的尺寸必须是奇数。这是因为大多数滤波器通过找出像素邻域中最常见的值来工作。

The size of window must be an odd integer. This is because the majority filter works by finding the most common value in a neighborhood of pixels.

如果窗口尺寸是偶数,窗口中央会有两个像素具有相同的值,而大多数滤波器将无法确定哪个值最常见。

If the window size is even, there will be two pixels with the same value in the center of the window, and the majority filter will not be able to determine which value is the most common.

Example

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image=mh.imread('picture.jpg', as_grey = True)
# Specify a filter size
filter_size = 19
# Apply majority filter with the specified filter size
filtered_image = mh.majority_filter(image, N=filter_size)
# 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 majority filtered image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Majority Filtered')
axes[1].axis('off')
mtplt.show()

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

Following is the output of the above code −

specifying window size

By Storing Result in an Output Array

在 Mahotas 中,我们还可以将大多数滤波器的结果存储在输出数组中。要做到这一点,我们首先需要使用 NumPy 库创建一个空数组。

We can store the result of the majority filter in an output array as well using Mahotas. To achieve this, we first need to create an empty array using the NumPy library.

这个数组的初始化形状与输入图像一致,以存储最终的滤波图像。数组的数据类型指定为 bool,假设为布尔图像。

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 bool, assuming a Boolean image.

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

Finally, we store the resultant filtered image in the output array by passing it as a parameter to the majority_filter() function.

Example

在这里,我们尝试将大多数滤波器应用于灰度图像,并将结果存储在特定的输出数组中 −

In here, we are trying to apply majority 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, dtype=bool)
# Apply majority filter with a 3x3 neighborhood
# store the result in the output array
mh.majority_filter(image, N=3, 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 majority filtered image
axes[1].imshow(output, cmap='gray')
axes[1].set_title('Majority Filtered')
axes[1].axis('off')
mtplt.show()

上述代码的输出如下:

Output of the above code is as follows −

storing result output array