使用高斯滤波器对图像FFT进行卷积

时间:2019-02-07 01:51:20

标签: python python-3.x fft convolution

我正在尝试使用高斯滤波器对图像进行卷积,并且我了解到使用FFT是最快的方法。我已经尝试过用高斯滤波器对图像进行卷积,但是结果还不是很好。.

gaussian=[[0.003, 0.013, 0.022, 0.013, 0.003],\
          [0.013, 0.059, 0.097, 0.059, 0.013],\
          [0.022, 0.097, 0.159, 0.097, 0.022],\
          [0.013, 0.059, 0.097, 0.059, 0.013],\
          [0.003, 0.013, 0.022, 0.013, 0.003]]
gaussian1=np.array(gaussian)
# load in an image, convert to grayscale if needed
image = imageio.imread('input.jpg',as_gray=True)

# take the fourier transform of the image
fft2 = fftpack.fftshift(fftpack.fft2(image))
gaussian_np= fftpack.fftshift(fftpack.fft2(gaussian))

#temp array to save the results
temp_array=np.ones((fft2.shape[0],fft2.shape[1]))

for x in range(fft2.shape[0]-4): #range upto -4 because of 5x5 kernel
    for y in range(fft2.shape[1]-4):

        temp=fft2[x:5+x,y:5+y] #slices 5x5 grid from fft of image
        convolution=np.multiply(temp,gaussian_np)
        temp_array[x:5+x,y:5+y]=convolution
#convert results back
ifft2 = abs(fftpack.ifft2(fftpack.ifftshift(temp_array)))

0 个答案:

没有答案
相关问题