You are on page 1of 9

Solution 1.

MATLAB CODE

clc; % clear you command window


clear all; % clear workspace
close all; %close all the files
r=imread('moonlanding.png'); % Read image store in varliable r
subplot(1,2,1);
imshow(r) % Diaply Original Figure
F=fft2(r); %Convert image from spatial domain to % Frequency Domain
FS=fftshift(log(1+abs(F))); %Calculate the DFT. Notice how there are real and
imaginary parts to F. You must use abs to compute the magnitude (square root
of the sum of the squares of the real and imaginary parts).
FSmax = max(FS(:))%Find the maximum frequency in S

subplot(1,2,2);
imshow(FS,[]) % Display The figure in frequency domain

Output

FSmax =

16.9609510582468
Solution 2.
clc; % clear you command window
clear all; % clear workspace
close all; %close all the files
r=imread('moonlanding.png'); % Read image store in varliable r
subplot(1,2,1);
imshow(r) % Diaply Original Figure
F=fft2(r); %Convert image from spatial domain to % Frequency Domain
S=fftshift(log(1+abs(F))); %Calculate the DFT. Notice how there are real and
imaginary parts to F. You must use abs to compute the magnitude (square root
of the sum of the squares of the real and imaginary parts).
FSmax = max(S(:)); %Find the maximum frequency in S
subplot(1,2,2);
plot(S) % Frequency Spectrum of Figure
Solution 3
clc;
clear all;
close all;
im = imread('moonlanding.png');
figure,imshow(im);
FT = fft2(double(im));
FT1 = fftshift(FT);%finding spectrum
imtool(abs(FT1),[]);
m = size(im,1);
n = size(im,2);
t = 0:pi/5:2*pi;
xc=(m+100)/2; % point around which we filter image
yc=(n-100)/2;
r=200; %Radium of circular region of interest(for BRF)
r1 = 100;
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);

mask1 = poly2mask(double(xcc1),double(ycc1), m,n);%generating mask for noise


mask(mask1)=0;
FT2=FT1;
FT2(mask)=0;

imtool(abs(FT2),[]);
output = ifft2(ifftshift(FT2));
imtool(output,[]);
4.

MATLAB Code
IM=imread('moonlanding.png'); % Read in a image
% Display image
subplot(1,3,1);imshow(IM)
FF = fft(IM); % Take FFT
IFF = ifft(FF); % take IFFT
subplot(1,3,2);imshow(FF)
FINAL_IM = uint8(real(IFF)); % Take real part and convert back to UINT8
subplot(1,3,3);
imshow(FINAL_IM) % Get back original image.
5.

clc;
clear all;
close all;
im = imread('moonlanding.png');
imshow(im);
FT = fft2(double(im));
FT1 = fftshift(FT);%finding spectrum
imtool(abs(FT1),[]);
m = size(im,1);
n = size(im,2);
t = 0:pi/5:2*pi;
xc=(m+100)/2; % point around which we filter image
yc=(n-100)/2;
r=200; %Radium of circular region of interest(for BRF)
r1 = 100;
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);

mask1 = poly2mask(double(xcc1),double(ycc1), m,n);%generating mask for


filtering
mask(mask1)=0;
FT2=FT1;
FT2(mask)=0;%cropping area or bandreject filtering

imtool(abs(FT2),[]);
output = ifft2(ifftshift(FT2));
imtool(output,[]);
plot(output)
6.

a) Ideal Filter
clc
clear all
close all
ima=imread('moonlanding.png');
ima = double(ima);
figure(1);
imshow(ima,[]);
title('Original image');
imafft = fftshift(fft2(fftshift(ima)));
% Fourier Spectrum of Image
imafft2 = fft2(ima);
imafft3 = fftshift(imafft2);
s = size(ima); ma=max(max((imafft)));
maxr = 0.5*sqrt(s(1)^2+s(2)^2); cutoff1 = maxr*30;
cutoff2 = maxr*120;
c=1;
for i = 1 : s(1)
for j = 1 : s(2) r = sqrt((i-1-s(1)/2)^2+(j-1-s(2)/2)^2);
if( r < 30) z(i,j) = 0;
else if( r > 120) z(i,j) = 0;
else z(i,j) =511;
end
end
end
end
% Plots
imafft=imafft.*z/255;
ima_out = fftshift(ifft2(fftshift(imafft)));
ima_out =ima_out-ima; ima_out = 1-ima_out;
figure(2); imshow(ima_out,[]);
title('Filtered image (Ideal)');
figure(3); imshow(imafft3,[]);
title('Fourier Spectrum of Image')
imshow(z,[]);
title('Filtered');

b) Butterworth Band Pass Filter

function filtered_image = butterworthbpf(I,d0,d1,n)


% Butterworth Bandpass Filter
% This simple function was written for my Digital Image Processing course
% at Eastern Mediterranean University taught by
% Assoc. Prof. Dr. Hasan Demirel
% for the 2010-2011 Spring Semester
% for the complete report:
% http://www.scribd.com/doc/51981950/HW4-Frequency-Domain-Bandpass-Filtering
%
% Written By:
% Leonardo O. Iheme (leonardo.iheme@cc.emu.edu.tr)
% 23rd of March 2011
%
% I = The input grey scale image
% d0 = Lower cut off frequency
% d1 = Higher cut off frequency
% n = order of the filter
%
% The function makes use of the simple principle that a bandpass filter
% can be obtained by multiplying a lowpass filter with a highpass filter
% where the lowpass filter has a higher cut off frquency than the high pass
filter.
%
% Usage BUTTERWORTHBPF(I,DO,D1,N)
% Example
% ima = imread('grass.jpg');
% ima = rgb2gray(ima);
% filtered_image = butterworthbpf(ima,30,120,4);
f = double(I);
[nx ny] = size(f);
f = uint8(f);
fftI = fft2(f,2*nx-1,2*ny-1);
fftI = fftshift(fftI);
subplot(2,2,1)
imshow(f,[]);
title('Original Image')
subplot(2,2,2)
fftshow(fftI,'log')
title('Image in Fourier Domain')
% Initialize filter.
filter1 = ones(2*nx-1,2*ny-1);
filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);
for i = 1:2*nx-1
for j =1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
% Create Butterworth filter.
filter1(i,j)= 1/(1 + (dist/d1)^(2*n));
filter2(i,j) = 1/(1 + (dist/d0)^(2*n));
filter3(i,j)= 1.0 - filter2(i,j);
filter3(i,j) = filter1(i,j).*filter3(i,j);
end
end
% Update image with passed frequencies.
filtered_image = fftI + filter3.*fftI;
subplot(2,2,3)
fftshow(filter3,'log')
title('Filter Image')
filtered_image = ifftshift(filtered_image);
filtered_image = ifft2(filtered_image,2*nx-1,2*ny-1);
filtered_image = real(filtered_image(1:nx,1:ny));
filtered_image = uint8(filtered_image);
subplot(2,2,4)
imshow(filtered_image,[])
title('Filtered Image')
c) Gaussian Band Pass Filter

function fftshow(f,type)
% Usage: FFTSHOW(F,TYPE)
%
% Displays the fft matrix F using imshow, where TYPE must be one of
% 'abs' or 'log'. If TYPE='abs', then then abs(f) is displayed; if
% TYPE='log' then log(1+abs(f)) is displayed. If TYPE is omitted, then
% 'log' is chosen as a default.
%
% Example:
% c=imread('cameraman.tif');
% cf=fftshift(fft2(c));
% fftshow(cf,'abs')
%
if nargin<2,
type='log';
end
if (type=='log')
fl = log(1+abs(f));
fm = max(fl(:));
imshow(im2uint8(fl/fm))
elseif (type=='abs')
fa=abs(f);
fm=max(fa(:));
imshow(fa/fm)
else
error('TYPE must be abs or log.');
end;

function filtered_image = gaussianbpf(I,d0,d1)


% Butterworth Bandpass Filter
% This simple function was written for my Digital Image Processing course
% at Eastern Mediterranean University taught by
% Assoc. Prof. Dr. Hasan Demirel
% for the 2010-2011 Spring Semester
% for the complete report:
% http://www.scribd.com/doc/51981950/HW4-Frequency-Domain-Bandpass-Filtering
%
% Written By:
% Leonardo O. Iheme (leonardo.iheme@cc.emu.edu.tr)
% 24th of March 2011
%
% I = The input grey scale image
% d0 = Lower cut off frequency
% d1 = Higher cut off frequency
%
% The function makes use of the simple principle that a bandpass filter
% can be obtained by multiplying a lowpass filter with a highpass filter
% where the lowpass filter has a higher cut off frquency than the high pass
filter.
%
% Usage GAUSSIANBPF(I,DO,D1)
% Example
% ima = imread('grass.jpg');
% ima = rgb2gray(ima);
% filtered_image = gaussianbpf(ima,30,120);
% Gaussian Bandpass Filter
f = double(I);
[nx ny] = size(f);
f = uint8(f);
fftI = fft2(f,2*nx-1,2*ny-1);
fftI = fftshift(fftI);
subplot(2,2,1)
imshow(f,[]);
title('Original Image')
subplot(2,2,2)
fftshow(fftI,'log')
title('Fourier Spectrum of Image')
% Initialize filter.
filter1 = ones(2*nx-1,2*ny-1);
filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);
for i = 1:2*nx-1
for j =1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
% Use Gaussian filter.
filter1(i,j) = exp(-dist^2/(2*d1^2));
filter2(i,j) = exp(-dist^2/(2*d0^2));
filter3(i,j) = 1.0 - filter2(i,j);
filter3(i,j) = filter1(i,j).*filter3(i,j);
end
end
% Update image with passed frequencies
filtered_image = fftI + filter3.*fftI;
subplot(2,2,3)
fftshow(filter3,'log')
title('Frequency Domain Filter Function Image')
filtered_image = ifftshift(filtered_image);
filtered_image = ifft2(filtered_image,2*nx-1,2*ny-1);
filtered_image = real(filtered_image(1:nx,1:ny));
filtered_image = uint8(filtered_image);
subplot(2,2,4)
imshow(filtered_image,[])
title('Bandpass Filtered Image')

You might also like