2014-05-18 47 views
5

我已經閱讀了很多關於高斯模糊和FFT的問題,但是沒有回答如何實現它的步驟(但有些評論像「這是你的作業」)。我想知道,如何正確地填充內核並在內核和映像上使用FFT和IFFT。你能提供像Java,Python等任何語言的僞代碼或實施如何做到這一點,或至少一些很好的教程,如何去理解它:通過FFT正確實現高斯模糊

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

回答

2

一個Matlab複製例。它應該是一個適合你的好地方。

裝入圖象:

%Blur Demo 

%Import image 
origimage = imread('tonimorrison','jpg'); 

%Reduce image to 2-D 
origimage = origimage(:,:,1); 

%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(find(fftkernel == 0)) = 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', []) 
+0

大示例代碼。我確實有一些建議使這一點變得更好。 (1)使用我們可以測試的實際圖像顯示示例結果。您正在使用帶有我們無法訪問的本地圖像的imread。 (2)不要簡單地使用紅色通道轉換爲灰度。使用'rgb2gray'爲你做。 (3)如果您有時間,可擴展到彩色圖像,您可以在每個通道單獨執行過濾。 (4)使用'find'將所有零值設置爲最小值是有點低效的。改用邏輯索引:'fftkernel(fftkernel == 0)= 1e-6;'這裏你不需要'find'。 – rayryeng