Monday 11 December 2023

What is Image Median Blurring and How to Use It?

Median blurring is similar to average blurring, but only difference is it replaces the pixel value at a particular location with the median value of the pixels within a defined neighborhood or kernel.

 

Let me explain it with below array and kernel of size 3*3.

[[5 8 7 3 2 1]
 [6 9 8 4 3 2]
 [4 3 2 6 7 6]
 [7 8 9 5 4 3]
 [5 4 5 9 9 5]
 [8 7 6 7 8 4]]

Let’s take the first 3*3 elements from the input array (From top left corner). We need to replace the center pixel value with the median of kernel placed values.

 


 

In the above example, kernel highlight below elements.

 

5, 8, 7, 6, 9, 8, 4, 3, 2

 

Sort the elements : 2, 3, 4, 5, 6, 7, 8, 8, 9

Median is the middle element which is6.

Update the center pixel value by 6. Do the same exercise for remaining pixels.

 

Let’s apply the same logic for all the pixels.

 


 

As you see above image, edge rows and columns are not filled yet.

 

How edge rows, columns are handled here?

There are multiple ways to address corner rows and columns.

 

a.   Do not process corner rows and columns: Apply the average blurring only to interior pixels

b.   Zero padding: Pixels outside the image boundary are considered to have a value of zero.

 

To make the things simple, I am not processing edge rows and columns.

 


 

median_blur_on_arrays.py

import numpy as np

# Create a sample 6x6 NumPy array
image = np.array([[5, 8, 7, 3, 2, 1],
                  [6, 9, 8, 4, 3, 2],
                  [4, 3, 2, 6, 7, 6],
                  [7, 8, 9, 5, 4, 3],
                  [5, 4, 5, 9, 9, 5],
                  [8, 7, 6, 7, 8, 4],
                  ], dtype=np.uint8)

# Define a function to apply median blur using a 3x3 kernel
def median_blur(input_image):
    output_image = np.zeros_like(input_image)

    for i in range(1, input_image.shape[0] - 1):
        for j in range(1, input_image.shape[1] - 1):
            # Extract the 3x3 neighborhood
            neighborhood = input_image[i - 1:i + 2, j - 1:j + 2]

            # Calculate the median of the neighborhood
            median_value = np.median(neighborhood)

            # Set the output pixel value to the median
            output_image[i, j] = median_value

    return output_image

# Apply median blur with a 3x3 kernel
median_blurred_image = median_blur(image)

print("Original Image:")
print(image)

print("\nMedian-Blurred Image:")
print(median_blurred_image)

 

Output

Original Image:
[[5 8 7 3 2 1]
 [6 9 8 4 3 2]
 [4 3 2 6 7 6]
 [7 8 9 5 4 3]
 [5 4 5 9 9 5]
 [8 7 6 7 8 4]]

Median-Blurred Image:
[[0 0 0 0 0 0]
 [0 6 6 4 3 0]
 [0 7 6 5 4 0]
 [0 5 5 6 6 0]
 [0 7 7 7 5 0]
 [0 0 0 0 0 0]]

 

How to perform median blur in OpenCV?

Using 'cv2.medianBlur' method, we can apply median blur on an image.

 

dst = cv2.medianBlur(src, ksize)

 

‘dst’ store the blurred image as NumPy array.

 

Following table summarizes the parameters of medianBlur method.

 

Parameter

Description

src

Source image that you want to apply blur

ksize

Size of kernel. In general, it is an odd integer like 3, 5, 7 etc.,

 

Find the below working application.

 

median_blurring.py

import cv2 as cv

image = cv.imread('gateway_of_india.png')
cv.imshow('Original image', image)

blurred_image = cv.medianBlur(image, 5)
cv.imshow('Blurred image 5*5 kernel size', blurred_image)

cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

 

Output

Original Image

 


 

Blurred image



 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment