通过FFT正确实现高斯模糊

6

我在Stack Overflow上读了很多有关高斯模糊和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

以下步骤摘自高斯模糊和FFT

1个回答

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', [])

前图像: 未模糊的前图像

后图像: 已模糊的后图像

注意,由于零填充没有将核心放置在中心,因此会产生偏移。本答案解释了包裹问题。使用FFT进行高斯模糊


很棒的示例代码。我有一些建议可以让它变得更好。(1)展示一个使用我们可以测试的实际图像的示例结果。您正在使用带有本地图像的imread,而我们无法访问。(2)不要简单地使用红色通道转换为灰度。使用rgb2gray来完成。(3)如果您有时间,请扩展到彩色图像,其中您将分别对每个通道执行过滤。(4)使用find将所有零值设置为最小值有点低效。改用逻辑索引:fftkernel(fftkernel == 0) = 1e-6;您在这里不需要find - rayryeng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接