gaussian filter Algorithm

The Gaussian filter algorithm is an essential image processing technique used in computer vision and signal processing applications. It employs a Gaussian function, which is a bell-shaped curve, to perform spatial filtering or averaging of pixel values in an image, thereby reducing noise and smoothing the image. This algorithm is particularly effective in eliminating high-frequency noise while preserving low-frequency features, such as edges and corners, in the image. This is achieved by convolving the Gaussian filter with the input image, where the Gaussian filter is typically represented as a kernel or a matrix of values that are derived from the Gaussian function. The Gaussian filter algorithm is implemented by first defining the Gaussian kernel, which is determined by two parameters: the kernel size and the standard deviation (sigma). The kernel size defines the dimensions of the matrix, while the standard deviation controls the spread of the Gaussian function. A larger kernel size and higher standard deviation will lead to more aggressive smoothing. Once the Gaussian kernel is defined, the algorithm convolves it with the input image by multiplying the kernel's values with the corresponding pixel values in the image and summing the results. This operation is performed iteratively for each pixel in the image, resulting in a new, smoothed image. The Gaussian filter algorithm is widely used in various computer vision tasks, such as image denoising, edge detection, and feature extraction, and forms the basis of many advanced algorithms, such as scale-space theory and the Scale-Invariant Feature Transform (SIFT).
"""
Implementation of gaussian filter algorithm
"""
from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey
from numpy import pi, mgrid, exp, square, zeros, ravel, dot, uint8
from itertools import product


def gen_gaussian_kernel(k_size, sigma):
    center = k_size // 2
    x, y = mgrid[0 - center : k_size - center, 0 - center : k_size - center]
    g = 1 / (2 * pi * sigma) * exp(-(square(x) + square(y)) / (2 * square(sigma)))
    return g


def gaussian_filter(image, k_size, sigma):
    height, width = image.shape[0], image.shape[1]
    # dst image height and width
    dst_height = height - k_size + 1
    dst_width = width - k_size + 1

    # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows
    image_array = zeros((dst_height * dst_width, k_size * k_size))
    row = 0
    for i, j in product(range(dst_height), range(dst_width)):
        window = ravel(image[i : i + k_size, j : j + k_size])
        image_array[row, :] = window
        row += 1

    #  turn the kernel into shape(k*k, 1)
    gaussian_kernel = gen_gaussian_kernel(k_size, sigma)
    filter_array = ravel(gaussian_kernel)

    # reshape and get the dst image
    dst = dot(image_array, filter_array).reshape(dst_height, dst_width).astype(uint8)

    return dst


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

    # get values with two different mask size
    gaussian3x3 = gaussian_filter(gray, 3, sigma=1)
    gaussian5x5 = gaussian_filter(gray, 5, sigma=0.8)

    # show result images
    imshow("gaussian filter with 3x3 mask", gaussian3x3)
    imshow("gaussian filter with 5x5 mask", gaussian5x5)
    waitKey()

LANGUAGE:

DARK MODE: