Mahotas 简明教程

Mahotas - Rank Filter

等级滤波器是一种技术,用于通过基于像素相对等级(位置)更改其像素值来修改图像。它关注像素值,而不是它们的实际强度。

A rank filter is a technique used to modify an image by changing the values of its pixels based on their relative ranks (positions). It focuses on the pixel values rather than their actual intensities.

对于图像中的每个像素,等级滤波器检查其邻域内所有像素的值,并按升序或降序排列。

For each pixel in the image, the rank filter examines the values of all the pixels within its neighborhood and arranges them in ascending or descending order.

然后,它根据排序列表中的位置或等级从排序列表中选择一个特定的像素值。

It then selects a specific pixel value from the sorted list based on its position or rank.

例如,如果等级滤波器设置为选择中值,则它会从排序列表中选择中间像素值。

For example, if the rank filter is set to select the median value, it would choose the middle pixel value from the sorted list.

如果将其设置为选择最小值或最大值,则它将分别选择第一个或最后一个值。

If it is set to select the minimum or maximum value, it would choose the first or last value, respectively.

Rank Filter in Mahotas

我们可以使用 mahotas.rank_filter() 函数在 mahotas 中执行等级滤波器。mahotas 中的等级滤波器比较邻域内像素强度值,并根据其在排序的强度列表中的等级为每个像素分配一个新值。

We can perform rank filter in mahotas using the mahotas.rank_filter() function. A rank filter in mahotas compares pixel intensity values within a neighborhood and assigns a new value to each pixel based on its rank in the sorted list of intensities.

为了详细说明,让我们看看等级滤波器在 mahotas 中的工作原理:

To elaborate, let’s see how a rank filter works in mahotas −

  1. Imagine you have a grayscale image made up of many pixels, each with a certain intensity value ranging from black to white.

  2. Now, let’s focus on a particular pixel in the image. The rank filter will examine a neighborhood around this pixel.

  3. Within this neighborhood, the rank filter will compare the intensity values of all the pixels. It will arrange these pixels based on their intensity values in ascending or descending order, depending on the filter’s configuration.

  4. Once the intensity values are ordered, the rank filter will assign a new value to the central pixel based on its rank in the ordered list. This new value is usually the median, minimum, or maximum intensity value within the neighborhood.

通过对图像中的每个像素重复此过程,等级滤波器可以帮助完成各种图像增强任务。

By repeating this process for each pixel in the image, the rank filter can help in various image enhancement tasks.

The mahotas.rank_filter() function

mahotas 中的 rank_filter() 函数接受三个参数:要滤波的图像、结构元素和邻域大小。

The rank_filter() function in mahotas accepts three arguments: the image to be filtered, the structuring element and the size of the neighborhood.

邻域是每个像素周围的一个矩形区域,其大小由每个维度中的像素数指定。例如,大小为 3×3 的邻域将包含像素的八个相邻点。

The neighborhood is a rectangular region around each pixel, and its size is specified by the number of pixels in each dimension. For example, a neighborhood of size 3×3 would include the eight neighbors of the pixels.

rank_filter() 函数返回一个新图像,其维度与原始图像相同。新图像中的值是原始图像中相应像素的等级。

The rank_filter() function returns a new image with the same dimensions as the original image. The values in the new image are the ranks of the corresponding pixels in the original image.

以下是 mahotas 中 rank_filter() 函数的基本语法:

Following is the basic syntax of the rank_filter() function in mahotas −

mahotas.rank_filter(f, Bc, rank, mode='reflect', cval=0.0, out=None)

其中,

Where,

  1. f − It is the input image array to which the rank filter will be applied.

  2. Bc − The structuring element that defines the neighborhood around each pixel.

  3. rank − It determines the rank of the pixel value to be selected from the sorted list within the neighborhood. The rank can be an integer or a list of integers if multiple ranks are desired.

  4. mode (optional) − Determines how the boundaries of the input image are handled. It can take one of the following values: 'ignore', 'constant', 'nearest', 'mirror', or 'wrap'. The default value is 'reflect'.

  5. cval (optional) − The value to be used when mode='constant'. The default value is 0.0.

  6. out (optional) − An array to store the output of the rank filter. If not provided, a new array is created.

以下是使用 rank_filter() 函数过滤图像的基本示例:

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

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()

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

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

[[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]]

显示的图像如下所示:

The image displayed is as shown below −

rank filter

Rank Filter on an RGB Image

彩色图像由三个颜色通道组成:红色、绿色和蓝色 (RGB)。通过分别对每个通道应用归档滤波,我们可以增强每种颜色通道的特定特征。

Color images are composed of three color channels: red, green, and blue (RGB). By applying rank filtering to each channel separately, we can enhance the features specific to each color channel.

要在 mahotas 中对 RGB 图像应用归档滤波,我们首先通过指定通道值从 RGB 图像中提取通道。

To apply rank filter on an RGB image in mahotas, we first extract the channels from the RGB image by specifying the channel value.

分别使用索引 0、1 和 2 访问红色、绿色和蓝色通道的通道值。然后,我们在每个通道上应用归档滤波,并将它们堆叠在一起以重建最终的 RGB 图像。

The channel values are accessed using the indices 0, 1, and 2 for red, green, and blue channels, respectively. Then, we apply the rank filter on each channel and stack them together to reconstruct the final RGB image.

Example

在这里,我们在 RGB 图像的每个通道上分别应用具有不同邻域大小的中值滤波:

In here, we are applying a median filter with different neighborhood sizes on each channel separately of an RGB image −

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()

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

Following is the output of the above code −

rank filter rgb image

Using 'Wrap' Mode

对图像执行归档滤波时,边缘或边界上的像素通常缺少足够的相邻像素来准确计算秩。

When performing rank filtering on an image, the pixels at the edges or boundaries often lack sufficient neighboring pixels to calculate the rank accurately.

为了解决此问题,Mahotas 中的 'wrap' 模式将图像值环绕到另一侧。这意味着图像一端的像素值被视为另一端的像素的相邻像素。

To address this issue, the 'wrap' mode in Mahotas wraps the image values around to the opposite side. This means that the pixel values from one end of the image are considered as the neighbors for the pixels at the opposite end.

这会从一侧到另一侧无缝过渡,从而确保在计算秩值时考虑边界处的像素。

This creates a seamless transition from one side to the other, ensuring that pixels at the boundaries are considered when computing the rank value.

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()

上述代码的输出如下:

Output of the above code is as follows −

[[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]]

获得的图像如下所示:

The image obtained is as follows −

wrap mode