# How to perform a gaussian blur using FFT?

I am trying to implement a gaussian filter with matlab.Here is my implementation: So far All I get as output is a black image. Any hints?

clear all;
close all;
I=rgb2gray(I);
sigma=1;
[M,N,s]=size(I);
f1=-fix(M/2):ceil(M/2)-1;
f2=-fix(N/2):ceil(N/2)-1;
[fx,fy]=meshgrid(f1,f2);
x=exp(-2*pi*pi*sigma*sigma*(fx/M.^2+fy/N.^2));
If=fft2(I);
If=fftshift(If);
If=If(:,:,1)*x;
If=ifftshift(If)
I=ifft2(If);
imshow(I)


Here is the working code,

clear all; close all; clc;

I = double(imread('Cameraman.tif'))/255; % Divide only if image value in [0-255] range
figure,imshow(I);title('Cameraman original')
%I=rgb2gray(I);  % NOTE: This is only for RGB images, commented otherwise
sigma=3;
[M,N,s]=size(I);
f1=-fix(M/2):ceil(M/2)-1;
f2=-fix(N/2):ceil(N/2)-1;
[fx,fy]=meshgrid(f1,f2);

X=exp(-2*pi*pi*sigma*sigma*((fx/M).^2+(fy/N).^2));
If=fft2(I);
If=fftshift(If);
If=If.*X;
If=ifftshift(If);
I=real(ifft2(If));
figure,imshow(I);title(['filtered image with \sigma =', num2str(sigma)])

• Why do you do a point by point multiplication?Isn't convolution equivalent in the frequency domain a simple matrix multiplication? – ChiPlusPlus Oct 3 '17 at 6:41
• It's a sequence multiplication not a matrix multiplication. – Fat32 Oct 3 '17 at 9:22
• Because sequences are multiplied point wise, like continuous function multiplication $f(x)g(x)$. There is no matrix/vector multiplication defined for them. – Fat32 Oct 4 '17 at 15:25