Professional Documents
Culture Documents
Where F(k,l) is the input image in the Fourier domain, H (k,l) the
filter function and G(k,l) is the filtered image. To obtain the resulting
image in the spatial domain,
G ( k , l ) has to be re-transformed using the inverse Fourier
Transform. Since the multiplication in the Fourier space is identical
to convolution in the spatial domain, all frequency filters can in
theory be implemented as a spatial filter. However, in practice, the
Fourier domain filter function can only be approximated by the
filtering kernel in spatial domain.
I
Original Image
Filtered Image
Frequency Spectrum
of Image
As expected, the ideal filter has the effect of ringing and blurring on
the original image. The associated Matlab code is given below:
clear all;
close all;
clc
ima=imread('lena.bmp');
ima = double(ima);
figure (1)
imshow(ima,[]);
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*80;
cutoff2 = maxr*180;
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 < 80)
z(i,j) = 0;
else
if( r > 180)
z(i,j) = 0;
else
z(i,j) =255;
end
end
end
end
% Plots
figure (2)
imafft=imafft.*z/255;
ima_out = fftshift(ifft2(fftshift(imafft)));
ima_out =ima_out-ima;
fftshow(ima_out,'abs');
figure (3)
fftshow(imafft3,'log');
figure (4)
fftshow(z,'log');
2. Butterworth Bandpass Filter
This filter can be derived mathematically by multiplying the transfer
functions of a a low andhigh pass filter. The low pass filter will have
the higher cut off frequency.
Where Dl and DH are the cut frequencies of the low and high pass
filters respectively; n is the order of the filter and D(u,v) is the
distance from the origin.
The Butterworth filter has a smooth transfer function without any
discontinuity or clear cut off frequency. The range of frequencies
that the filter allows is largely dependent on the order of the filter. In
the selection of n there has to be a compromise between the
demands
of
the
frequency
domain
(sharp
cutoff)
and
the
Original Image
Filtered Image
Frequency Spectrum
of Image
The result shows that the ringing and blurring effect that was
observed with the ideal bandpass filter does not exist for
the Butterworth filter. The Matlab code is presented below:
clear all;
close all;
clc
micro = imread('lena.bmp');
micro = double(micro);
[nx ny] = size(micro);
u = micro;
micro = uint8(u);
imwrite(micro,'lena1.bmp');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
figure(1)
imshow(micro,[]);
figure(2)
fftshow(fftu,'log')
% 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);
n = 4;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
% Use Butterworth filter.
filter1(i,j)= 1/(1 + (dist/180)^(2*n));
filter2(i,j) = 1/(1 + (dist/80)^(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.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
3. Gaussian Bandpass Filter
The Gaussian filter out-performs the previously discussed filter types
because when a Gaussian is transformed between the frequency
and spatial domains, it remains a Gaussian; and therefore does not
incur the ringing effect in the spatial domain of the filtered image.
Again the derivation of a Gaussian bandpass filter starts from the
low pass filter.
Where DL ,DH are the cut frequencies of the low and high pass
filters respectively; D(u,v) is the distance from the origin. The
example below shows that the Gaussian bandpass filter is the best
among the three discussed filters
Original Image
Filtered Image
Frequency Spectrum
of Image
filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);
n = 4;
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*180^2));
filter2(i,j) = exp(-dist^2/(2*80^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.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
II
Low
frequencies
represent
the
gray-level
D(u,v) is the distance from the origin, D0 is the cutoff frequency. The
following example demonstrates the effect of applying an ideal
lowpass filter with cutoff frequency 80 a grey scale 512512 image.
Original Image
Filtered Image
Frequency Spectrum
of Image
for i=1:M
for j=1:N
D=sqrt(i^2+j^2);
if D<D0
H(i,j)=1;
else
H(i,j)=0;
end
G(i,j)=Imf(i,j)*H(i,j);
end
end
F=ifft2(G);
% Plots
figure (2)
imafft=imafft.*G/255;
ima_out = fftshift(ifft2(fftshift(imafft)));
imshow(F,[]);
figure (3)
fftshow(imafft3,'log');
figure (4)
fftshow(G,'log');
H (u , v)
2.
1
1 [ D(u , v) / D0 ]2 n
Original Image
Filtered Image
Frequency Spectrum
of Image
filter3 = ones(2*nx-1,2*ny-1);
n = 2;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;.
filter1(i,j)= 1/(1 + (dist/80)^(2*n));
filter3(i,j) = filter1(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
H (u , v) e D
3.
( u , v ) / 2 D02
D(u,v) is the distance from the origin, D0 is the cutoff frequency. The
following example demonstrates the effect of applying an ideal
lowpass filter with cutoff frequency 80 a grey scale 512512 image.
Original Image
Filtered Image
Frequency Spectrum
of Image
filter1 = ones(2*nx-1,2*ny-1);
filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);
n = 4;
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*80^2));
filter3(i,j) = filter1(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
III
0
1
H (u , v )
if D (u, v) D0
if D(u , v) D0
D(u,v) is the distance from the origin, D0 is the cutoff frequency. The
following example demonstrates the effect of applying an ideal
lowpass filter with cutoff frequency 80 a grey scale 512512 image.
Original Image
Filtered Image
Frequency Spectrum
of Image
for j = 1 : s(2)
r = sqrt((i-1-s(1)/2)^2+(j-1-s(2)/2)^2);
if(r>=80)
z(i,j) = 1;
else
z(i,j) = 0;
end
end
end
figure (2)
imafft=imafft.*z/255;
F=ifft2(imafft);
imshow(F);
ima_out = fftshift(ifft2(fftshift(imafft)));
ima_out =ima_out-ima;
figure (3)
fftshow(imafft3,'log');
figure (4)
fftshow(z,'log');
2. Butterworth highpass Filter
H (u, v)
1
1 [ D0 / D(u , v)]2 n
Original Image
Frequency Spectrum
of Image
The associated
Matlab code is given below:
clear all;
close all;
clc
Filtered Image
micro = imread('lena.bmp');
micro = double(micro);
[nx ny] = size(micro);
u = micro;
micro = uint8(u);
imwrite(micro,'lena1.bmp');
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
figure(1)
imshow(micro,[]);
figure(2)
fftshow(fftu,'log')
% 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);
n = 2;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
% Use Butterworth filter.
filter1(i,j)= 1/(1 + (80/dist)^(2*n));
filter3(i,j) = filter1(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])
3. Gaussian highpass Filter
H (u , v) 1 e D
( u , v ) / 2 D02
D(u,v) is the distance from the origin, D0 is the cutoff frequency. The
following example demonstrates the effect of applying an ideal
lowpass filter with cutoff frequency 80 a grey scale 512512 image.
Original Image
Filtered Image
Frequency Spectrum
of Image
filter3 = ones(2*nx-1,2*ny-1);
n = 4;
for i = 1:2*nx-1
for j=1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
filter2(i,j) = 1-exp(-dist^2/(2*80^2));
filter3(i,j) = 1.0 - filter2(i,j);
end
end
% Update image with passed frequencies.
fil_micro = fftu + filter3.*fftu;
figure(3)
fftshow(filter3,'log')
fil_micro = ifftshift(fil_micro);
fil_micro = ifft2(fil_micro,2*nx-1,2*ny-1);
fil_micro = real(fil_micro(1:nx,1:ny));
fil_micro = uint8(fil_micro);
figure(4)
imshow(fil_micro,[])