通过FFT正确实现高斯模糊

时间:2014-05-18 09:29:55

标签: image-processing filtering fft gaussian gaussianblur

我已经阅读了很多关于高斯模糊和FFT的问题,但是没有回答如何实现它的步骤(但是有一些评论,比如“这是你的功课”)。我想知道,如何正确填充内核并在内核和图像上使用FFT和IFFT。你能用Java,Python等语言提供一些伪代码或实现,如何做到这一点,或至少有一些好的教程如何理解它:

1. FFT the image
2. FFT the kernel, padded to the size of the image
3. multiply the two in the frequency domain (equivalent to convolution in the spatial domain)
4. IFFT (inverse FFT) the result

Gaussian blur and FFT

复制的步骤

1 个答案:

答案 0 :(得分:3)

一个Matlab示例。它应该是一个适合你的好地方。

加载图片:

%Blur Demo

%Import image in matlab default image set.
origimage = imread('cameraman.tif');

%Plot image
figure, imagesc(origimage)
axis square
colormap gray
title('Original Image')
set(gca, 'XTick', [], 'YTick', [])

整个过程:

%Blur Kernel
ksize = 31;
kernel = zeros(ksize);

%Gaussian Blur
s = 3;
m = ksize/2;
[X, Y] = meshgrid(1:ksize);
kernel = (1/(2*pi*s^2))*exp(-((X-m).^2 + (Y-m).^2)/(2*s^2));

%Display Kernel
figure, imagesc(kernel)
axis square
title('Blur Kernel')
colormap gray

%Embed kernel in image that is size of original image
[h, w] = size(origimage);
kernelimage = zeros(h,w);
kernelimage(1:ksize, 1:ksize) = kernel;

%Perform 2D FFTs
fftimage = fft2(double(origimage));
fftkernel = fft2(kernelimage);

%Set all zero values to minimum value
fftkernel(abs(fftkernel)<1e-6) = 1e-6;

%Multiply FFTs
fftblurimage = fftimage.*fftkernel;

%Perform Inverse 2D FFT
blurimage = ifft2(fftblurimage);

%Display Blurred Image
figure, imagesc(blurimage)
axis square
title('Blurred Image')
colormap gray
set(gca, 'XTick', [], 'YTick', [])

之前的图片: Before image, unblurred

图像后: After image, blurred

注意,因为零填充不是将内核置于中心,所以会得到一个偏移量。这个答案解释了包装问题。 gaussian blur with FFT