Mahotas 简明教程
Mahotas - Median Filter
中值滤波器是另一种用于图像降噪的常用技术。它的工作原理是计算相邻像素之间的中间(中值)值,并用该中间值替换原始像素值。
The median filter is another commonly used technique for noise reduction in an image. It works by calculating the middle (median) value among the neighboring pixels and replaces the original pixel value with that middle value.
为了理解中值滤波器,让我们考虑带黑色小圆点的黑白图像场景,这些小圆点表示噪声。图像中的每个像素都具有二进制值——白色(表示感兴趣的对象)或黑色(表示背景)。
To understand the median filter, let’s consider the same black−and−white image scenario with small black dots representing noise. Each pixel in the image has a binary value − white (representing the object of interest) or black (representing the background).
对于每个像素,中值滤波器获取窗口内相邻像素的像素值。然后,它根据这些像素的强度值将它们按升序排列。
For each pixel, the median filter takes the pixel values of its neighboring pixels within the window. It then arranges them in ascending order based on their intensity value.
之后,它选择中间值(中值),并用该中值替换原始像素值。
After that, it selects the middle value, which is the median, and replaces the original pixel value with that median value.
Median Filter in Mahotas
要在 Mahotas 中应用中值滤波器,可以使用 median_filter() 函数。
To apply the median filter in Mahotas, you can use the median_filter() function.
Mahotas 中的中值滤波函数使用结构元素来检查邻域中的像素。
The median filter function in Mahotas uses a structuring element to examine pixels in a neighborhood.
结构元素通过计算其邻域内的中间值来替换每个像素的值。
The structuring element replaces the value of each pixel by calculating the middle value within its neighborhood.
结构元素的大小决定了中值滤波器应用的平滑程度。
The size of the structuring element determines the extent of smoothing applied by the median filter.
较大的邻域将产生更强的平滑效果,同时减少图像的较精细细节。另一方面,较小的邻域将导致较少的平滑效果,但保留更多细节。
A larger neighborhood will result in a stronger smoothing effect, while reducing finer details of the image. On the other hand, a smaller neighborhood will result in less smoothing but maintains more details.
The mahotas.median_filter() function
median_filter() 函数使用指定邻域大小将中值滤波器应用于输入图像。它用在相邻像素之间计算出的中值替换每个像素值。过滤后的图像存储在输出数组中。
The median_filter() function applies the median filter to the input image using the specified neighborhood size. It replaces each pixel value with the median value calculated among its neighbors. The filtered image is stored in the output array.
以下是 mahotas 中中值滤波器函数的基本语法:
Following is the basic syntax of the median filter function in mahotas −
mahotas.median_filter(img, Bc={square}, mode='reflect', cval=0.0,
out={np.empty(f.shape, f.dtype})
其中,
Where,
-
img − It is the input image.
-
Bc − It is the structuring element that defines the neighbourhood. By default, it is a square of side 3.
-
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 'reflect'.
-
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 and data type as the input image.
以下是用 median_filter() 函数过滤图像的基本示例 −
Following is the basic example to filter the image using the median_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.median_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 median filtered image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Median Filtered')
axes[1].axis('off')
mtplt.show()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
Median Filter with Reflect Mode
当我们对图像应用中值滤波器时,我们需要考虑每个像素周围的相邻像素来计算中值。
When we apply the median filter to an image, we need to consider the neighboring pixels around each pixel to calculate the median.
但是,在图像的边缘处,有一些像素在一侧或多侧没有相邻像素。
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 median filter with neighboring pixels even at the edges.
通过反射图像值,中值滤波器现在可以将这些镜像像素视为真实相邻像素。
By reflecting the image values, the medan filter can now consider these mirrored pixels as if they were real neighbors.
它使用这些虚拟相邻像素计算中值,从而在图像边缘产生更准确的平滑过程。
It calculates the median 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 median 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.median_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 median filtered image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Median Filtered')
axes[1].axis('off')
mtplt.show()
上述代码的输出如下:
Output of the above code is as follows −
By Storing Result in an Output Array
我们还可以使用马霍塔斯将中值滤波器结果存储在输出数组中。为了实现这一点,我们首先需要使用 NumPy 库创建一个空数组。
We can store the result of the median 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 and data type as the input image to store the resultant filtered image.
最后,我们将最终的过滤图像存储在输出数组中,方法是将其作为参数传递给 median_filter() 函数。
Finally, we store the resultant filtered image in the output array by passing it as a parameter to the median_filter() function.
Example
现在,我们尝试将中值滤波器应用于灰度图像,并将结果存储在特定的输出数组中 −
Now, we are trying to apply median 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.median_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 median filtered image
axes[1].imshow(output, cmap='gray')
axes[1].set_title('Median Filtered')
axes[1].axis('off')
mtplt.show()
以下是上面代码的输出: -
Following is the output of the above code −