Mahotas 简明教程
Mahotas - Rank Filter
等级滤波器是一种技术,用于通过基于像素相对等级(位置)更改其像素值来修改图像。它关注像素值,而不是它们的实际强度。
对于图像中的每个像素,等级滤波器检查其邻域内所有像素的值,并按升序或降序排列。
然后,它根据排序列表中的位置或等级从排序列表中选择一个特定的像素值。
例如,如果等级滤波器设置为选择中值,则它会从排序列表中选择中间像素值。
如果将其设置为选择最小值或最大值,则它将分别选择第一个或最后一个值。
Rank Filter in Mahotas
我们可以使用 mahotas.rank_filter() 函数在 mahotas 中执行等级滤波器。mahotas 中的等级滤波器比较邻域内像素强度值,并根据其在排序的强度列表中的等级为每个像素分配一个新值。
为了详细说明,让我们看看等级滤波器在 mahotas 中的工作原理:
-
想象一下你有一张由许多像素组成的灰度图像,每个像素都有从黑色到白色的特定强度值。
-
现在,让我们关注图像中的一个特定像素。等级滤波器将检查此像素周围的邻域。
-
在这个邻域内,等级滤波器将比较所有像素的强度值。它将根据滤波器的配置,按升序或降序根据强度值排列这些像素。
-
一旦强度值被排序,等级滤波器将根据其在有序列表中的等级为中心像素分配一个新值。此新值通常是邻域内的中值、最小值或最大值强度值。
通过对图像中的每个像素重复此过程,等级滤波器可以帮助完成各种图像增强任务。
The mahotas.rank_filter() function
mahotas 中的 rank_filter() 函数接受三个参数:要滤波的图像、结构元素和邻域大小。
邻域是每个像素周围的一个矩形区域,其大小由每个维度中的像素数指定。例如,大小为 3×3 的邻域将包含像素的八个相邻点。
rank_filter() 函数返回一个新图像,其维度与原始图像相同。新图像中的值是原始图像中相应像素的等级。
以下是 mahotas 中 rank_filter() 函数的基本语法:
mahotas.rank_filter(f, Bc, rank, mode='reflect', cval=0.0, out=None)
其中,
-
f − 它是将应用归档滤波器的输入图像数组。
-
Bc − 结构元素,它定义了每个像素周围的邻域。
-
rank − 它决定了从邻域内的已排序列表中选择的像素值的秩。秩可以是一个整数或者一个整数列表(如果需要多个秩)。
-
mode (optional) − 决定如何处理输入图像的边界。它可以采用以下值之一:'ignore'、'constant'、'nearest'、'mirror' 或 'wrap'。默认值为 'reflect'。
-
cval (optional) − 在 mode='constant' 时要使用默认值。默认值为 0.0。
-
out (optional) − 一个用于存储归档滤波器输出的数组。如果没有提供,则会新建一个数组。
以下是使用 rank_filter() 函数过滤图像的基本示例:
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Creating a sample grayscale image
image = mh.imread('nature.jpeg', as_grey = True)
# Applying minimum filter
filtered_image = mh.rank_filter(image, mh.disk(6), rank=0)
print(filtered_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 rank filtered featured image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Rank Filtered')
axes[1].axis('off')
mtplt.show()
执行上面的代码后,我们得到以下输出: -
[[193.71 193.71 193.71 ... 206.17 206.17 207.17]
[193.71 193.71 193.71 ... 206.17 206.17 207.17]
[193.71 193.71 193.71 ... 206.17 206.17 207.17]
...
[ 74.59 62.44 53.62 ... 4.85 4.85 4.85]
[ 85.37 74.59 62.44 ... 4.85 4.85 4.85]
[ 90.05 79.3 73.18 ... 4.85 4.85 4.85]]
显示的图像如下所示:
Rank Filter on an RGB Image
彩色图像由三个颜色通道组成:红色、绿色和蓝色 (RGB)。通过分别对每个通道应用归档滤波,我们可以增强每种颜色通道的特定特征。
要在 mahotas 中对 RGB 图像应用归档滤波,我们首先通过指定通道值从 RGB 图像中提取通道。
分别使用索引 0、1 和 2 访问红色、绿色和蓝色通道的通道值。然后,我们在每个通道上应用归档滤波,并将它们堆叠在一起以重建最终的 RGB 图像。
Example
在这里,我们在 RGB 图像的每个通道上分别应用具有不同邻域大小的中值滤波:
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image = mh.imread('nature.jpeg')
# Applying a median filter with different neighborhood sizes on each channel
separately
filtered_image = np.stack([mh.rank_filter(image[:, :, 0], mh.disk(2), rank=4),
mh.rank_filter(image[:, :, 1], mh.disk(1), rank=4),
mh.rank_filter(image[:, :, 2], mh.disk(3), rank=4)],
axis=2)
# 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 rank filtered featured image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Rank Filtered')
axes[1].axis('off')
mtplt.show()
以下是上面代码的输出: -
Using 'Wrap' Mode
对图像执行归档滤波时,边缘或边界上的像素通常缺少足够的相邻像素来准确计算秩。
为了解决此问题,Mahotas 中的 'wrap' 模式将图像值环绕到另一侧。这意味着图像一端的像素值被视为另一端的像素的相邻像素。
这会从一侧到另一侧无缝过渡,从而确保在计算秩值时考虑边界处的像素。
Example
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image = mh.imread('sun.png', as_grey = True)
# Applying maximum filter with 'wrap' mode
filtered_image = mh.rank_filter(image, mh.disk(10), rank=8, mode='wrap')
print(filtered_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 rank filtered featured image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Rank Filtered')
axes[1].axis('off')
mtplt.show()
上述代码的输出如下:
[[49.92 49.92 49.92 ... 49.92 49.92 49.92]
[49.92 49.92 49.92 ... 49.92 49.92 49.92]
[49.92 49.92 49.92 ... 49.92 49.92 49.92]
...
[49.92 49.92 49.92 ... 49.92 49.92 49.92]
[49.92 49.92 49.92 ... 49.92 49.92 49.92]
[49.92 49.92 49.92 ... 49.92 49.92 49.92]]
获得的图像如下所示: