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