Search code examples
matlabfrequencygaussian

Gaussian Circularly Symmetric Filter Applied to a Frequency Domain Image


I'm having trouble applying a Gaussian filter to an Image and one of the requirements is to make sure it's a circularly symmetric filter. I am not sure if I need to add a zero-padding sequence but please take a look at my code below and let me know if you can help me out at all! Thank you!

Please Note: I am not able to use any toolbox functions otherwise this would be pretty simple and straightforward.

L=256;
L1 = 255;
%load 'iptest_im.mat'
%ipFreq(iptest01)



[ImageX,ImageY] = size(A);


load 'iptest_im.mat';
A = iptest01;
A=double(A);  % Just in Case
Fourier=fft2(A);  
ABS_Fourier=abs(Fourier);

%Gaussian 
Gaussian=zeros(ImageX,ImageY);
sigma=5; % Sigma Values for Gaussian Filter **** WHERE YOU CHANGE
for i = 1:ImageX
    for j = 1:ImageY
            D = (i-ImageX/2)^2 + (j-ImageY/2)^2;
            Gaussian(i,j) = L1*exp(-(D)/(2*(sigma)^2));      
    end
end

GaussianFilt = fft2(Gaussian);
FourierFilt = Fourier .* GaussianFilt;


%Display for No shift
FFT_NoShift = abs((log10(FourierFilt+1)));
minA=min(min(FFT_NoShift));
FFT_NoShift = FFT_NoShift-minA; % shift
maxA = max(max(FFT_NoShift));
if maxA~=0
    FFT_NoShift=FFT_NoShift*L1/maxA; % compress or expand
end

%Display for Shift
FFT_Shifted = fftshift(abs(log10(FourierFilt+1)));
minA=min(min(FFT_Shifted));
FFT_Shifted = FFT_Shifted-minA; % shift
maxA = max(max(FFT_Shifted));
if maxA~=0
    FFT_Shifted=FFT_Shifted*L1/maxA; % compress or expand
end

InvFFT = abs(ifft2(FourierFilt));
%InvFFT = abs(ifft2(FFT_Shifted1));
%InvFFT = abs(ifft2(FFT_NoShift1));
 

Solution

  • Normalizing Fourier Transform of Gaussian Filter to 1 Before Multiplying Spectral Components

    Normalizing the Gaussian kernel before multiplying the frequency components will help with appropriate scaling. Here I used the maximum of the Gaussian kernel to normalizes all its components to 1. You could also apply fftshift() to all Fourier frequency components if wanted but the same result is achieved. Either shift all spectral components or shift none. You could probably apply some of that logarithmic scaling you have but I'm not totally sure of the implementation details surrounding that.

    Guassian Blurred Image

    Greyscale_Image = rgb2gray(imread("peppers.png"));
    [ImageX,ImageY] = size(Greyscale_Image);
    
    Fourier_Transform = fft2(Greyscale_Image);
    
    Gaussian = zeros(ImageX, ImageY);
    sigma = 5; 
    for i = 1: ImageX
        for j = 1:ImageY
                D = (i-ImageX/2)^2 + (j-ImageY/2)^2;
                Gaussian(i,j) = exp(-(D)/(2*(sigma)^2));      
        end
    end
    
    GaussianFilt = round(abs(fft2(Gaussian)));
    %Normalizes the Guassian filter%
    GaussianFilt = GaussianFilt./max(GaussianFilt,[],'all');
    
    
    Reconstructed = uint8(ifft2(Fourier_Transform.*GaussianFilt));
    imshow(Reconstructed);
    

    Ran using MATLAB R2019b