Sunday, November 17, 2013

Ideal Low Pass Filter Concept in MATLAB

%IDEAL LOW-PASS FILTER

%Part 1
        
        f=imread(X);  % reading an image X
        P=input('Enter the cut-off frequency'); % 10, 20, 40 or 50.
        [M,N]=size(f); % Saving the the rows of X in M and columns in N
        F=fft2(double(f)); % Taking Fourier transform to the input image
%Part 2 % Finding the distance matrix D which is required to create the filter mask
        u=0:(M-1);
        v=0:(N-1);
        idx=find(u>M/2);
        u(idx)=u(idx)-M;
        idy=find(v>N/2);
        v(idy)=v(idy)-N;
        [V,U]=meshgrid(v,u);
        D=sqrt(U.^2+V.^2);

%Part 3
        H=double(D<=P); % Comparing with the cut-off frequency to create filter mask 
        G=H.*F;               % Multiplying the Fourier transformed image with H
        g=real(ifft2(double(G))); % Inverse Fourier transform
        imshow(f),figure,imshow(g,[ ]); % Displaying input and output image
        end
 
 
Demo:
 
Reading an image:
 
Taking fourier transform of the above image, we get:
 Based on the cut-off freq (P) we design the filter function H , Here the cut-off frequency is nothing but radius of the white circle in the below image. The below image is usually referred as filter mask.

Performing filtering by using G=H.*F;% Multiplying the Fourier transformed image with the filter mask H. Please note the convolution in time domain to equal to multiplication in frequency domain.
 To the filtered output, we take inverse fourier transform for the above image and we get:
 This is the Ideal Low pass filtered image. 

1 comment:

  1. i try to use this code, but it didn't work for me.
    matlab says:
    error using .*
    matrix dimension must agree.
    can u tell me what's wrong?

    i use jpg image size around 1200 x 350 pixel

    thanks

    ReplyDelete