Convolution in spatial domain vs fourier domain

148 views Asked by At

i wrote a function which performs 2d-convolution in the fourier domain. However, when i compare the output of my function to the output of the scipy.ndimage.convolve function without transfering the image and the kernel into the fourier domain the result is completely different. For a test convolution i chose a kernel which i expected would blur my test image, instead my test image just got significantly darker and the edges are highlighted. Using the scipy.ndimage.convolve function, yields the exected result. Does someone know what i do wrong in my function?

def fourier_conv(img:np.ndarray, kernel:np.ndarray) -> np.ndarray:
    #perform 2d discrete fourier transform on image and kernel
    #convolute image and kernel (maybe kernel padding necessary so image and kernel have same size)
    #perform inverse DTF to bring image back to spatial domain
    
    img_2ddft = np.fft.fft2(img)
    kernel_2ddft = np.fft.fft2(kernel)
    
    conv_img_fd = ndimage.convolve(img_2ddft, kernel_2ddft, mode='constant', cval=0, origin=0)

    conv_img = abs(np.fft.ifft2(conv_img_fd))
    return conv_img

Thanks for your help!

1

There are 1 answers

0
David On

This is expected: one of the reasons a Fourier transform is so useful in certain contexts is that it eliminates the need for a convolution. A full continuous convolution is equivalent to a multiplication is frequency space. Obviously an FFT will be slightly different, but it should be fairly close. You'd have to research exactly how you multiply your transformed input by your transformed kernel in 2D.