1

I am currently learning how to filter images using Fourier transform in Matlab. I managed to apply a low pass filter on an image, the problem is, I cannot do the same with high pass filter. Here are codes and images that I got. Could you help me?

clc

clear

A=imread('3.tif'); % image size is 200 x 200

B=fftshift(fft2(A));

mask=zeros(200);

mask(80:120,80:120)=1;

C=mask.*B;

D=ifft2(C);

D=uint8(D);

imshow(D);

Here the results:

https://i.stack.imgur.com/Y2UaI.png

The problem occures when I try to apply an inverse mask, like so:

clc

clear

A=imread('3.tif'); % image size is 200 x 200

B=fftshift(fft2(A));

mask=zeros(200);

mask=mask+255;

mask(80:120,80:120)=0;

C=mask.*B;

D=ifft2(C);

D=uint8(D);

imshow(D);

Results:

https://i.stack.imgur.com/NzYNG.png

What is wrong?

Cape Code
  • 3,584
  • 3
  • 24
  • 45
user3524911
  • 13
  • 1
  • 3
  • Your D matrix is actually going to be 16 bits in the above code, so when you convert it into a uint8, you are not getting meaningful information back. This is why the first answer below works, and why your first code works. – dustincarr Apr 11 '14 at 19:04
  • See this answer for an example of FFT filtering: http://stackoverflow.com/a/22637290/2777181, replace this line `MaskedFFT=fftshift(fft2(f));.*Mask;` by `MaskedFFT=fftshift(fft2(f));.*(1-Mask);` to turn into high pass. – Cape Code Apr 11 '14 at 19:56

1 Answers1

3

Change this on the second code:

mask=zeros(200);
mask=mask+255;

... to this

mask=ones(200);

You also forgot to call ifftshift on both codes:

D=ifft2(ifftshift(C));

Here is what I got:

  • Lowpass:

enter image description here

  • Highpass:

enter image description here

Rafael Monteiro
  • 4,509
  • 1
  • 16
  • 28