[Python] Gaussian notch filter in Python

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to design a Gaussian notch filter in Python to remove periodic noise.I tried implementing the following formula:

    Gaussian Notch Filter

    And here is the code:

    import numpy as np

    def gaussian_bandpass_filter(image):
    image_array = np.array(image)
    #Fourier Transform
    fourier_transform = np.fft.fftshift(np.fft.fft2(image_array))

    #Size of Image
    m = np.shape(fourier_transform)[0]
    n = np.shape(fourier_transform)[1]

    u = np.arange(m)
    v = np.arange(n)

    # Find the center
    u0 = int(m/2)
    v0 = int(n/2)

    # Bandwidth
    D0 = 10

    gaussian_filter = np.zeros(np.shape(fourier_transform))

    for x in u:
    for y in v:
    D1 = math.sqrt((x-m/2-u0)**2 + (y-n/2-v0)**2)
    D2 = math.sqrt((x-m/2+u0)**2 + (y-n/2+v0)**2)
    gaussian_filter[x][y] = 1 - math.exp(-0.5 * D1*D2/(D0**2))

    #Apply the filter
    fourier_transform = fourier_transform + gaussian_filter
    image_array = np.fft.ifft2(np.fft.ifftshift(fourier_transform))

    return image_array


    this function is supposed to apply the Gaussian notch filter to an image and return the filtered image but it doesn't seem to work. I don't know where I went wrong with this (maybe I didn't understand the formula correctly?) so if anyone could help me I would really appreciate it.

    Edit:

    As an example, here is a noisy image. Using the existing gaussian_filter function in scipy.ndimage library, I get this, which is acceptable. But my function returns this. (I'm using PIL.Image.fromarray function to convert array to image)

    Continue reading...

Compartilhe esta Página