Search code examples
matlabfrequency-domain

How can I use a linear filter from the frequency domain to remove noise in an image?


So I have worked with linear filters from the Spatial domain but in the Frequency domain I have troubles understanding how to implement any filter in Matlab. Could anybody explain me how to do that?

I want to see how I can use a filter from the frequency domain to remove noise from an image.


Solution

  • See This Example for Moon Landing Image. Moon Landing

    %=====================
    clc;
    clear all;
    close all;
    %=====================
    im = imread('moonlanding.png');
    %=====================
    FT  = fft2(double(im));
    %finding spectrum
    FTS = fftshift(FT);
    FTSG= log(FTS+1);
    figure;
    imshow(abs(FTSG),[]);
    %imtool(abs(FTS),[]);
    [m,n] = size(im);
    %====================
    t = 0:pi/10:2*pi;
    % point around which we filter image
    xc=(m+150)/2; 
    yc=(n-150)/2;
    %======================
    figure;
    subplot(221)
    imshow(im);
    %======================
    for k = 1:3
    %Radium of circular region of interest(for BRF)
    %r=200;   
    %r1 = 40;
    r = 200;
    r1 = 50*k;
    %------------------------
    xcc  = r*cos(t)+xc;
    ycc  = r*sin(t)+yc;
    xcc1 = r1*cos(t)+xc;
    ycc1 = r1*sin(t)+yc;
    %------------------------
    mask  = poly2mask(double(xcc),double(ycc), m,n);
    %generating mask for filtering
    mask1 = poly2mask(double(xcc1),double(ycc1), m,n);
    
    mask(mask1)=0;
    
    FT2=FTS;
    FT2(mask)=0;%cropping area or bandreject filtering
    %========================
    % Display Results
    %imtool(abs(FT2),[]);
    %subplot(211)
    %imshow(abs(FT2),[]);
    output = ifft2(ifftshift(FT2));
    subplot(2,2,(k+1));
    %imtool(output,[]);
    imshow(abs(output),[]);
    end