Image_Filtering.zip dftfilt.m

www.pudn.com > Image_Filtering.zip > dftfilt.m, change:2005-11-20,size:1169b

    %Question No: 6
    %Take a sample image of size 256 by 256. Write a MATLAB function for
    %implementing DFT filtering with (a)padding and (b)without padding.
    %Compare the results.

    function dftfilt(x)
    F=imread(x);
    F=im2double(F);
    [r c]=size(F);
    %without padding
    sig=10;
    DF=DFT(F);
    H=gaussfilt(r,c,sig);
    R=H.*DF;
    G=real(IDFT(R));
    %with padding
    p=r*2;
    q=c*2;
    DFP=fft2(F,p,q);
    HP=gaussfilt(p,q,2*sig);
    RP=HP.*DFP;
    GP=real(ifft2(RP));
    GPC=GP(1:r,1:c);
    figure,imshow(F),title('Original Image');
    figure,imshow(G),title('DFT Filtering - Without Padding');
    figure,imshow(GPC),title('DFT Filtering - With Padding');
    end
    function H=gaussfilt(r,c,sig)
    u=0:(r-1);
    v=0:(c-1);
    id=find(u>r/2);
    u(id)=u(id)-r;
    id=find(v>c/2);
    v(id)=v(id)-c;
    [V,U]=meshgrid(v,u);
    D=sqrt(U.^2+V.^2);
    H=exp(-(D.^2)./(2*(sig^2)));
    end

    function DF=DFT(F)
    [r c]=size(F);
    F=im2double(F);
    N=r;
    for m=1:N
        for n=1:N
            W(m,n)=exp((-j*2*pi*(m-1)*(n-1))/N);
        end
    end
    DF=W*F*W;
    end
    function DF=IDFT(F)
    [r c]=size(F);
    F=im2double(F);
    N=r;
    for m=1:N
        for n=1:N
            W(m,n)=exp((j*2*pi*(m-1)*(n-1))/N);
        end
    end
    DF=W*F*W;
    DF=DF/N^2;
    end