1
$\begingroup$

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 = double(imread('Put your path here'))/255;
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)
$\endgroup$
2
$\begingroup$

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)])
$\endgroup$
  • $\begingroup$ Why do you do a point by point multiplication?Isn't convolution equivalent in the frequency domain a simple matrix multiplication? $\endgroup$ – ChiPlusPlus Oct 3 '17 at 6:41
  • $\begingroup$ It's a sequence multiplication not a matrix multiplication. $\endgroup$ – Fat32 Oct 3 '17 at 9:22
  • $\begingroup$ Can you please provide a link about that? I wish to read more about it. $\endgroup$ – ChiPlusPlus Oct 3 '17 at 11:30
  • $\begingroup$ The following page is an example for MATLAB. $\endgroup$ – Fat32 Oct 3 '17 at 13:16
  • 1
    $\begingroup$ Because sequences are multiplied point wise, like continuous function multiplication $f(x)g(x)$. There is no matrix/vector multiplication defined for them. $\endgroup$ – Fat32 Oct 4 '17 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.