median filter Algorithm

The median filter algorithm is a popular non-linear digital filtering technique used extensively in image and signal processing applications. This algorithm is designed to preserve the edges and reduce noise in an image or a signal by replacing each pixel or data point with the median of the neighboring pixels or data points within a predefined window. The window, usually a square or rectangular matrix, slides over the entire image or signal, and the central pixel or data point is updated with the median value of the surrounding pixels or data points within the window. This process results in the smoothing of the image or signal without causing significant blurring of edges or the introduction of artifacts. The primary advantage of the median filter algorithm over other linear filters, such as the mean or Gaussian filter, lies in its robustness against impulsive noise or salt-and-pepper noise. By using the median instead of the mean, the algorithm inherently discards outlier values, ensuring that only the relevant information is used for filtering. This makes median filters highly effective in removing noise while preserving essential features like edges and textures. Additionally, the median filter algorithm is simple to implement and computationally efficient, making it an attractive choice for various applications in image processing, computer vision, and signal processing domains.
"""
Implementation of median filter algorithm
"""

from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey
from numpy import zeros_like, ravel, sort, multiply, divide, int8


def median_filter(gray_img, mask=3):
    """
    :param gray_img: gray image
    :param mask: mask size
    :return: image with median filter
    """
    # set image borders
    bd = int(mask / 2)
    # copy image size
    median_img = zeros_like(gray_img)
    for i in range(bd, gray_img.shape[0] - bd):
        for j in range(bd, gray_img.shape[1] - bd):
            # get mask according with mask
            kernel = ravel(gray_img[i - bd : i + bd + 1, j - bd : j + bd + 1])
            # calculate mask median
            median = sort(kernel)[int8(divide((multiply(mask, mask)), 2) + 1)]
            median_img[i, j] = median
    return median_img


if __name__ == "__main__":
    # read original image
    img = imread("../image_data/lena.jpg")
    # turn image in gray scale value
    gray = cvtColor(img, COLOR_BGR2GRAY)

    # get values with two different mask size
    median3x3 = median_filter(gray, 3)
    median5x5 = median_filter(gray, 5)

    # show result images
    imshow("median filter with 3x3 mask", median3x3)
    imshow("median filter with 5x5 mask", median5x5)
    waitKey(0)

LANGUAGE:

DARK MODE: