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

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

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

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.

Syntax

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

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

Where,

  • img − It is the input image.

  • N − It is the size of the filter. It must be an odd integer. The default value is 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.

Example

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

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

Majority Filter

By Specifying Window Size

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

Output

Following is the output of the above code −

Specifying Window Size

By Storing Result in an Output Array

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.

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.

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

Output of the above code is as follows −

Storing Result Output Array
Advertisements