Difference between `scipy.ndimage.fourier_gaussian` and own implementation

93 Views Asked by At

I am trying to implement myself a 2D gaussian filter (to debug some C++ code, outside the scope of this question). I focus on the first step: multiplication of the image and the Gaussian kernel in the Fourier space.

The input data and its TF:

import numpy as np
import scipy
sigma = 2
size = 8
np.random.seed(42)
image = np.random.rand(size, size)
imageTf = np.fft.fft2(image)

First version of the multiplication:

imageBluredTfScipy = scipy.ndimage.fourier_gaussian(imageTf, sigma)

Second version with own implementation:

def makeGaussian(size, sigma):
    kernel = np.zeros((size,size))
    sigma2 = sigma**2
    tot = 0.
    for j in range(size):
        y = j - (size - 1.)/2.
        for i in range(size):
            x = i - (size - 1.)/2. 
            kernel[i,j] = np.exp(-(x**2/(2*sigma2) + y**2/(2*sigma2)))
            tot += kernel[i,j]
    kernel /= tot
return kernel

kernel = makeGaussian(size, sigma)
kernelTf = np.fft.fft2(kernel)
imageBluredTf = kernelTf*imageTf

Now, if I compare imageBluredTf and imageBluredTfScipy, it's not the same for both real and imaginary part (expect at [0,0] where both methods give the same value). For this example, the difference of the real part within [:2,:2] is:

array([[-7.10542736e-15,  2.01834288e-01],
   [ 7.12344007e-01,  7.59622995e-02]])

I have searched for the definition of the kernel used by scipy.ndimage.fourier_gaussian without success.

0

There are 0 best solutions below