Solution 1.: Matlab Code
Solution 1.: Matlab Code
MATLAB CODE
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);
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);
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');
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;