Learn Mahotas – Rank Filter work project make money

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

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.

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

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

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

  • 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.

  • 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

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

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.

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.

Syntax

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,

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

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

  • 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.

  • 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”.

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

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

Example

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

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

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.

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

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

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

Output

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.

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *