Lecture notes
Digital Image Processing by Jorma Kekalainen
Digital Image Processing
Jorma Kekalainen
Digital Image Processing
Frequency Domain
Filtering
Lecture weeks 21 and 22
Page 1
Lecture notes
Digital Image Processing by Jorma Kekalainen
Filtering in the frequency domain
We have seen previously that one of the
reasons for the use of the Fourier transform in
image processing is due to the convolution
theorem: a spatial convolution can be
performed by element-wise multiplication of
the Fourier transform by a suitable filter
matrix.
Here we shall explore some filtering by this
method.
Jorma Kekalainen
Digital Image Processing
1066
Ideal filters
As the ideal filters are radially symmetric about the
center of the transform, they can be simply
described in terms of their cross sections.
That is, we can describe the filter as a function of the
distance x from the center.
For an ideal low pass filter, this function can be
expressed as
where D is the cutoff radius.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1067
Page 2
Lecture notes
Digital Image Processing by Jorma Kekalainen
Ideal filter functions
Then the ideal high pass filters can be described similarly:
These functions are illustrated in
Low pass
Jorma Kekalainen
High pass
Digital Image Processing
1068
Ideal low pass filtering
Suppose we have a Fourier transform matrix F,
shifted so that the DC coefficient is in the
center.
Since the low frequency components are
towards the center, we can perform low pass
filtering by multiplying the transform by a
matrix in such a way that center values are
maintained, and values away from the center
are either removed or minimized.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1069
Page 3
Lecture notes
Digital Image Processing by Jorma Kekalainen
Ideal low pass filtering
One way to do this is to multiply the transform by an
ideal low-pass matrix, which is a binary matrix m
defined by:
Then the inverse Fourier transform of the elementwise product of F and m is the result we require:
Jorma Kekalainen
Digital Image Processing
1070
Note: The circle c displayed previously is just such a matrix with D=15.
Example
Let's see what happens if we apply this filter to an image. First
we obtain an image and its DFT.
>> cm=imread('cameraman.tif');
>> cf=fftshift(fft2(cm));
>> figure,fftshow(cf,'log')
The cameraman image and its DFT are shown below.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1071
Page 4
Lecture notes
Digital Image Processing by Jorma Kekalainen
Ideal filtering on the DFT
Now we can perform a low pass filter by multiplying the
transform matrix by the circle matrix (recall that dot asterisk is
the Matlab syntax for element-wise multiplication of two
matrices):
>> cfl=cf.*c;
>> figure,fftshow(cfl,'log')
and this is shown beside.
Jorma Kekalainen
Digital Image Processing
1072
Example
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1073
Page 5
Lecture notes
Digital Image Processing by Jorma Kekalainen
Applying ideal low pass filtering
Now we can take the inverse transform and display the result:
>> cfli=ifft2(cfl);
>> figure,fftshow(cfli,'abs')
After inversion
and this is shown beside.
Note the ringing about the
edges in this image. This is a
direct result of the sharp cutoff
of the circle. The ringing is
Jorma Kekalainen
Digital Image Processing
transferred
to the image.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1074
1075
Page 6
Lecture notes
Digital Image Processing by Jorma Kekalainen
Comment
Note that even though cfli is obviously a matrix of
real numbers, we are still using fftshow to display
it.
This is because the ifft2 and fft2 functions being
numeric, will not produce mathematically perfect
results, but rather very close numeric
approximations.
So using fftshow with the 'abs' option rounds out
any errors obtained during the transform and its
inverse.
Jorma Kekalainen
Digital Image Processing
1076
Example
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
xlabel('fftshow(Cf,''log'')')
%
%Lowpass filtering
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<15);
Cfl=Cf.*c;% Lowpass filtering(symbol l)
figure,fftshow(Cfl,'log')
title('DFT of the cameraman image multiplied by an ideal low-pass matrix c')
xlabel('fftshow(Cfl,''log'')')
%
%Now we can take the inverse transform and display the result:
Cfli=ifft2(Cfl);
figure,fftshow(Cfli,'abs')
title('IDFT [the cameraman image multiplied by an ideal low-pass matrix c]')
xlabel('fftshow(Cfli,''abs'')')
Jorma
Kekalainen
Digital Image Processing
Lecture weeks 21 and 22
1077
Page 7
Lecture notes
Digital Image Processing by Jorma Kekalainen
DFT of a very small circle (c<5)
We would expect that the smaller the circle, the more blurred the image, and the
larger the circle; the less blurred.
%Effect of a very small circle (c<5)
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<5);
figure, imshow(c)
title('Very small circle (c<5)')
%
cf=fftshift(fft2(c));
figure, fftshow(cf,'log')
title('DFT of the very small circle(c<5)')
xlabel('fftshow(cf,''log'')')
%without function
%figure, imshow(mat2gray(log(1+abs(cf))))
%title('DFT of the very small circle (c<5)')
Jorma Kekalainen
Digital Image Processing
1078
Effect of a very small circle (c<5)
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
xlabel('fftshow(Cf,''log'')')
%Lowpass filtering
Cfl=Cf.*c;% Lowpass filtering(symbol l)
figure,fftshow(Cfl,'log')
title('DFT of of the cameraman image multiplied by an ideal low-pass matrix c<5')
xlabel('fftshow(Cfl,''log'')')
%
%Now we can take the inverse transform and display the result:
Cfli=ifft2(Cfl);
figure,fftshow(Cfli,'abs')
title('IDFT the cameraman image multiplied by an ideal low-pass matrix c(<5)')
xlabel('fftshow(Cfli,''abs'')')
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1079
Page 8
Lecture notes
Digital Image Processing by Jorma Kekalainen
Effect of a very small circle (c<5)
Jorma Kekalainen
Digital Image Processing
1080
DFT of a larger circle (c<30)
%Effect of a larger circle (c<30)
r=30;
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<r);
figure, imshow(c)
title(['Circle c<', num2str(r)])
%
cf=fftshift(fft2(c));
figure, fftshow(cf,'log')
title(['DFT of the circle c<',num2str(r)])
xlabel('fftshow(cf,log)')
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1081
Page 9
Lecture notes
Digital Image Processing by Jorma Kekalainen
Effect of a larger circle (c<30)
Jorma Kekalainen
Digital Image Processing
1082
Note
We would expect that the smaller the circle, the more blurred
the image, and the larger the circle; the less blurred.
Figure below demonstrates this, using cutoffs of 5 and 30.
Notice that ringing is still present, and clearly visible in figure (b).
(a) Cutoff 5
Jorma Kekalainen
Lecture weeks 21 and 22
(b) Cutoff 30
Digital Image Processing
1083
Page 10
Lecture notes
Digital Image Processing by Jorma Kekalainen
Effect of a gentle cutoff(c<30)
Jorma Kekalainen
Digital Image Processing
1084
Comparison
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1085
Page 11
Lecture notes
Digital Image Processing by Jorma Kekalainen
Experiments with gentle cutoffs
%Experiments with gentle cutoffs
promt='Give cutoff value r=';
r=input(promt)
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<r);
%Circle with gentle cutoff or spatially blurred circle
b=1./(1+(z./r).^2);
%
figure,imshow(b)
title(['Circle c<',num2str(r),' with a gentle cutoff'])
%
bf=fftshift(fft2(b));
figure, fftshow(bf,'log')
title(['DFT of the circle c<',num2str(r),' with a
gentle cutoff'])
xlabel('fftshow(cf,log)')
%without function
%figure, imshow(mat2gray(log(1+abs(bf))))
%title('DFT of the circle c<30) with a gentle cutoff')
%
Jorma Kekalainen
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
%Lowpass filtering
Cfl=Cf.*b;% Lowpass filtering(symbol l)
figure,fftshow(Cfl,'log')
title(['DFT of of the cameraman image multiplied
by a blurred low-pass matrix c<',num2str(r)])
%
%Now we can take the inverse transform and
display the result:
%
Cfli=ifft2(Cfl);
figure,fftshow(Cfli,'abs')
title(['IDFT the cameraman image multiplied by a
blurred low-pass matrix c<',num2str(r)])
Digital Image Processing
1086
Digital Image Processing
High Pass Filtering
Lecture weeks 21 and 22
Page 12
Lecture notes
Digital Image Processing by Jorma Kekalainen
High pass filtering
Just as we can perform low pass filtering by
keeping the center values of the DFT and
eliminating the others, so high pass filtering
can be performed by the opposite: eliminating
center values and keeping the others.
This can be done with a minor modification of
the preceding method of low pass filtering.
Jorma Kekalainen
Digital Image Processing
1088
High pass filtering
First we create the circle:
>> [x,y]=meshgrid(-128:127,-128:127);
>> z=sqrt(x.^2+y.^2);
>> c=(z>15);
and multiply it by the DFT of the image:
>> cfh=cf.*c;
>> figure,fftshow(cfh,'log')
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
The DFT after
high pass filtering
1089
Page 13
Lecture notes
Digital Image Processing by Jorma Kekalainen
IDFT
The inverse DFT can be easily produced and displayed:
>> cfhi=ifft2(cfh);
The resulting image
>> figure,fftshow(cfhi,'abs')
and this is shown beside.
As with low pass filtering, the
size of the circle influences the
information available to the
inverse DFT, and hence the
final result.
Jorma Kekalainen
Digital Image Processing
1090
High pass filtered (c>15) image
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1091
Page 14
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
%High pass filtering
%First we create the filter circle:
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z>15);% High pass filtering
figure, imshow(c)
title('Ideal high pass filter (c>15)')
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
%and the multiply it by the DFT of the image:
Cfh=Cf.*c;
figure,fftshow(Cfh,'log')
title('Product of an ideal high pass filter (c>15) and DFT of the image')
%
% The inverse DFT can be easily produced and displayed:
Cfhi=ifft2(Cfh);
figure,fftshow(Cfhi,'abs')
Jorma Kekalainen
Image Processing
title('IDFT of the ideal high passDigital
filtered
(c>15) image')
1092
Ideal high pass filtering with
different cutoffs
The following figures show some results of ideal high pass
filtering with different cutoffs.
If we have small cutoff, such as in the next figure, we are
only removing a small amount of the transform.
We would thus expect that only the lowest frequencies of
the image would be removed; then there is some grayscale
detail in the final image, but large areas of low frequency
are close to zero.
If the cutoff is large, then more information is removed
from the transform, leaving only the highest frequencies.
We also see this in the following figures; only the edges of
the image remain.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1093
Page 15
Lecture notes
Digital Image Processing by Jorma Kekalainen
Examples
Cutoffs: c>15, c>30, c>60
Cutoffs: c>1, c>2, c>5
Jorma Kekalainen
Digital Image Processing
1094
Digital Image Processing
1095
c>50
c>100
c>120
Jorma Kekalainen
Lecture weeks 21 and 22
Page 16
Lecture notes
Digital Image Processing by Jorma Kekalainen
Digital Image Processing
Butterworth Filtering
Butterworth filtering
Ideal filtering simply cuts off the Fourier
transform at some distance from the center.
This is very easy to implement, as we have
seen, but has the disadvantage of introducing
unwanted effects: ringing, into the result.
One way of avoiding this is to use as a filter
matrix a circle with a less sharp cutoff.
A popular choice is to use Butterworth filters.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1097
Page 17
Lecture notes
Digital Image Processing by Jorma Kekalainen
Butterworth filters
Butterworth filters are based on the following functions for
low pass filters and for high pass filters :
where the parameter n is called the order of the filter and D is
cutoff radius.
The size of n dictates the sharpness of the cutoff.
Butterworth filters with n = 2
Jorma Kekalainen
Digital Image Processing
1098
Comparison of Butterworth filters
Butterworth filters with n = 2
Butterworth filters with n = 4
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1099
Page 18
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
It is easy to implement these filters in Matlab;
here are the commands to produce a
Butterworth low pass filter of size 256*256
with cutoff circle D=15 and order n=2:
[x,y]=meshgrid(-128:127,-128:127));
bl=1./(1+((x.^2+y.^2)/15^2).^2);
Jorma Kekalainen
Digital Image Processing
1100
Low pass Bu-filter
Here are the commands to produce a Butterworth low pass
filter of size 256*256 with cutoff circle D=15 and order n=2
and its 3D-plot.
[x,y]=meshgrid(-128:127,-128:127);
bl=1./(1+((x.^2+y.^2)/15^2).^2);
zl=bl;
figure, mesh(x,y,zl)
title('bl=1./(1+((x.^2+y.^2)/15^2).^2)')
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1101
Page 19
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
Applying a Butterworth low
pass filter to the DFT of the
cameraman image we get
results shown beside and
the next slices.
Note that there is no sharp
cutoff as seen previously
with the ideal low pass
filter; also that the outer
parts of the transform are
not equal to zero, although
they are dimmed
considerably.
Jorma Kekalainen
DFT after Butterworth
low pass filtering
Digital Image Processing
1102
Note
Compare the transform after multiplying with a Butterworth filter with
the original transform
We see that the Butterworth filter causes an attenuation of values away
from the center, but they don't become suddenly zero, as with the ideal
low pass filter.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1103
Page 20
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
Performing the inverse
transform and
displaying it as we have
done previously
produces the resulting
image beside.
This is a blurred image,
but now the ringing is
completely absent.
Jorma Kekalainen
Resulting image
Digital Image Processing
1104
Compare
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1105
Page 21
Lecture notes
Digital Image Processing by Jorma Kekalainen
Exercise
Study effects of Butterworth low pass filter
with different order (n) and cutoff radius (D)
on cameraman.tif image
Jorma Kekalainen
Digital Image Processing
1106
Solution
%Experiments with Butterworth low pass filter
promt1='Give filter order n=';
n=input(promt1)
promt2='Give cutoff value D=';
D=input(promt2)
%So to apply a Butterworth low pass filter to the DFT of the cameraman image
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
%Apply a Butterworth low pass filter to the DFT of the cameraman image:
[x,y]=meshgrid(-128:127,-128:127);
bl=1./(1+((x.^2+y.^2)/D^2).^n);
Cfbl=Cf.*bl;
figure,fftshow(Cfbl,'log')
title(['Butterworth lowpass filtered (n=',num2str(n),' and D=',num2str(D),') DFT image'])
%Inverting and displaying the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
title(['Butterworth lowpass filtered (n=',num2str(n),' and D=',num2str(D),' ) image'])
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1107
Page 22
Lecture notes
Digital Image Processing by Jorma Kekalainen
n=1,D=1,10,20,30
Jorma Kekalainen
Digital Image Processing
1108
n=1,D=40,50,100
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1109
Page 23
Lecture notes
Digital Image Processing by Jorma Kekalainen
n=2,D=1,10,20,30
Jorma Kekalainen
Digital Image Processing
1110
n=2,D=40,50,100
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1111
Page 24
Lecture notes
Digital Image Processing by Jorma Kekalainen
n=4,D=1,10,20,30
Jorma Kekalainen
Digital Image Processing
1112
n=4,D=40,50,100
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1113
Page 25
Lecture notes
Digital Image Processing by Jorma Kekalainen
n=8,D=1,10,20,30
Jorma Kekalainen
Digital Image Processing
1114
n=8,D=40,50,100
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1115
Page 26
Lecture notes
Digital Image Processing by Jorma Kekalainen
D=50,n=16,32,64,128
Jorma Kekalainen
Digital Image Processing
1116
Exercise
Study effects of Butterworth high pass filter
with different order (n) and cutoff radius (D)
on cameraman.tif image
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1117
Page 27
Lecture notes
Digital Image Processing by Jorma Kekalainen
Solution: Bu high pass filters
n=1
D=1
D=5
D=10
D=20,
D=50,
Jorma Kekalainen
Digital Image Processing
D=100
1118
Bu high pass filters
n=2,D=10,50,100
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1119
Page 28
Lecture notes
Digital Image Processing by Jorma Kekalainen
Exercise
2n
f low ( x ) =
x
1+
1
f (x) =
2n
low
D
1+
1. Show that
+
=1,when
2. Write a Matlab function
function out=lbutter(im,d,n)
to generate low pass Butterworth filters of general sizes.
3. Write a Matlab function
function out=hbutter(im,d,n)
to generate high pass Butterworth filters of general sizes.
4. Write a Matlab function
function out=butter(im,d,n,type)
to generate low or high pass Butterworth filters of
general sizes.
Jorma Kekalainen
Digital Image Processing
1120
Solution
1.
Show that
2n
f low ( x ) =
x
1+
1
f (x) =
2n
low
D
1+
=1, kun
2n
f low ( x ) + f high ( x ) =
1
x
1+
D
2n
1
D
1+
x
2n
Jorma Kekalainen
2n
2n
D
x
1+ +1+
x
D
=
2n
x D 2n
1 + 1 +
D x
2n
2n
2n
D
x
D
x
1+ +1+
2+ +
x
D
x
D =1
=
=
2n
2n
2n
2n
2n
2n
x
D
x D
D
x
1+ + +
2+ +
x Image1
D4
x4
D
Digital
2
4
3
x
D 1121
Processing
4
=1
Lecture weeks 21 and 22
Page 29
Lecture notes
Digital Image Processing by Jorma Kekalainen
Solution: Low pass Butterworth
filter function
function lout=lbutter(im,d,n)
% LBUTTER(IM,D,N) creates a low-pass Butterworth filter
% of the same size as image IM, with cutoff D, and order N
%
% Use e.g.:
% x=imread('cameraman.tif');
% l=lbutter(x,25,2);
%
height=size(im,1);
width=size(im,2);
[x,y]=meshgrid(-floor(width/2):floor((width-1)/2),-floor(height/2):
...
floor((height-1)/2));
lout=1./(1+((x.^2+y.^2)/d^2).^n);
Jorma Kekalainen
Digital Image Processing
1122
Solution: High pass Butterworth
filter function
function hout=hbutter(im,d,n)
% HBUTTER(IM,D,N) creates a high-pass
%Butterworth filter of the same size as image IM,
%with cutoff D, and order N
%
% Use e.g.:
% x=imread('image.tif');
% l=hbutter(x,25,2);
%
hout=1-lbutter(im,d,n);
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1123
Page 30
Lecture notes
Digital Image Processing by Jorma Kekalainen
Solution: Low or high pass Bufilter function
function [ out] = butter(im,d,n,type)
% A function to generate a low pass or high pass Butterworth filter
%function out=butter(im,d,n,type)
% BUTTER(IM,D,N,TYPE) creates a low-pass or high pass Butterworth filter
height=size(im,1);
width=size(im,2);
[x,y]=meshgrid(-floor(width/2):floor((width-1)/2),-floor(height/2): ...
floor((height-1)/2));
%
if strcmp(type,'low')
out=1./(1+((x.^2+y.^2)/d^2).^n);
elseif strcmp(type,'high')
out=1-1./(1+((x.^2+y.^2)/d^2).^n);
else
error('TYPE must be ''low'' or ''high''')
end
Jorma Kekalainen
Digital Image Processing
1124
Example
So to apply a Butterworth
low pass filter function
lbutter to the DFT of the
cameraman image:
bl=lbutter(c,15,1);
cfbl=cf.*bl;
figure,fftshow(cfbl,'log')
and then inverting and
displaying the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
Jorma Kekalainen
Lecture weeks 21 and 22
DFT after Butterworth
low pass filtering
Digital Image Processing
1125
Page 31
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
So to apply a Butterworth low
pass filter function lbutter to the
DFT of the cameraman image:
bl=lbutter(C,15,1);
cfbl=cf.*bl;
figure,fftshow(cfbl,'log')
and then inverting and displaying
the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
Jorma Kekalainen
We can apply a Butterworth
high pass filter function
similarly, first by creating
the filter and applying it to
the image transform:
bh=hbutter(C,15,1);
cfbh=cf.*bh;
figure,fftshow(cfbh,'log')
then inverting and
displaying the result
cfbhi=ifft2(cfbh);
figure,fftshow(cfbhi,'abs')
Digital Image Processing
1126
Example
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1127
Page 32
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
% Applying Butterworth low pass filter
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
bl=lbutter(Cf,15,1);
Cfbl=Cf.*bl;
figure,fftshow(Cfbl,'log')
title('DFT of the cameraman image
multiplied by Bu-lpf (D=15,n=1)')
%inverting and displaying the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
title('IDFT')
Jorma Kekalainen
%Applying Butterworth high pass filter
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
bh=hbutter(Cf,15,1);
Cfbh=Cf.*bh;
figure,fftshow(Cfbh,'log')
title('DFT of the cameraman image
multiplied by Bu-hpf (D=15, n=1)')
%inverting and displaying the result:
Cfbhi=ifft2(Cfbh);
figure,fftshow(Cfbhi,'abs')
title('Bu-highpass filtered (D=15, n=1)
image ')
Digital Image Processing
1128
Example
%Experiments with Butterworth low or high pass filter
promt1='Give filter order n=';
n=input(promt1)
promt2='Give cutoff radius D=';
D=input(promt2)
promt3='Give Butterworth filter type (low or high) type=';
type=input(promt3,'s')
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
bl=butter(Cf,D,n,type);% Call Bu function
Cfbl=Cf.*bl;
figure,fftshow(Cfbl,'log')
title('Applying butterworth filter to the DFT of the cameraman image')
%%and then inverting and displaying the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
title(['Butterworth (D=',num2str(D),', n=',num2str(n),', type= ',type,' pass) filtered image'])
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1129
Page 33
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
Jorma Kekalainen
Digital Image Processing
1130
Example
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1131
Page 34
Lecture notes
Digital Image Processing by Jorma Kekalainen
Digital Image Processing
Gaussian Filtering in
Frequency Domain
Gaussian filtering
We have discussed Gaussian filters earlier, and we saw that
they could be used for low pass filtering in the spatial
domain.
However, we can also use Gaussian filters in the frequency
domain.
As with ideal and Butterworth filters, the implementation is
very simple: create a Gaussian filter, multiply it by the
image transform, and invert the result.
Since Gaussian filters have the very nice mathematical
property that a Fourier transform of a Gaussian is a
Gaussian, we should get exactly the same results as when
using a linear Gaussian spatial filter.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1133
Page 35
Lecture notes
Digital Image Processing by Jorma Kekalainen
Gaussian filter
Gaussian filters may be considered to be the most smooth of all the filters
we have discussed so far, with ideal filters the least smooth, and
Butterworth filters in the middle.
We can create Gaussian filters using the fspecial function, and apply them
to our transform.
C=imread('cameraman.tif');
Cf=fftshift(fft2(C));
h = fspecial('gaussian', hsize, sigma)
g1=mat2gray(fspecial('gaussian',256,10)); returns a rotationally symmetric
Gaussian lowpass filter of size hsize
Cg1=Cf.*g1;
with standard deviation sigma
fftshow(Cg1,'log')
(positive). hsize can be a vector
g2=mat2gray(fspecial('gaussian',256,30)); specifying the number of
rows and columns in h, or it can be a
Cg2=Cf.*g2;
scalar, in which case h is a square
figure,fftshow(Cg2,'log')
Jorma Kekalainen
matrix.
The default value for hsize is [3 3]; the
Digital Image Processing
1134
default value for sigma is 0.5.
Note
Note the use of the mat2gray function.
The fspecial function on its own produces a low pass Gaussian filter with a
very small maximum:
>> g=fspecial('gaussian',256,10);
format long, max(g(:))
ans =
0.001587575526792
>> g=fspecial('gaussian',256,30);
format long, max(g(:)), format
ans =
1.767967201419648e-04
The reason is that fspecial adjusts its output to keep the volume under the
Gaussian function always 1.
This means that a wider function, with a large standard deviation, will
have a low maximum.
So we need to scale the result so that the central value will be 1; and
mat2gray does that automatically.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1135
Page 36
Lecture notes
Digital Image Processing by Jorma Kekalainen
Gaussian low pass filter
In each case, the final parameter of the fspecial function is
the standard deviation; it controls the width of the filter.
As we see, the larger the standard deviation, the wider the
function, and so the greater amount of the transform is
preserved.
The results of the transform on the original image can be
produced using the usual sequence of commands:
cgi1=ifft2(cg1);
cgi2=ifft2(cg2);
fftshow(cgi1,'abs');
fftshow(cgi2,'abs');
and the results are shown below.
Jorma Kekalainen
Digital Image Processing
1136
Applying a Gaussian low pass filter in
the frequency domain
=10
The larger the standard
deviation, the wider
the function, and so the
greater amount of the
transform is preserved.
=30
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1137
Page 37
Lecture notes
Digital Image Processing by Jorma Kekalainen
High pass Gaussian filter
We can apply a high pass Gaussian filter easily; we create a
high pass filter by subtracting a low pass filter from 1.
h1=1-g1;
h2=1-g2;
ch1=cf.*h1;
ch2=cf.*h2;
ch1i=ifft2(ch1);
chi1=ifft2(ch1);
chi2=ifft2(ch2);
fftshow(chi1,'abs')
figure,fftshow(chi2,'abs')
Jorma Kekalainen
Digital Image Processing
1138
Applying a Gaussian high pass
filter in the frequency domain
=10
=30
Note: As with ideal and Butterworth filters, the wider the high pass
filter, the more of the transform we are reducing, and the less of the
Jorma Kekalainen
Digital Image Processing
1139
original
image will appear in the
result.
Lecture weeks 21 and 22
Page 38
Lecture notes
Digital Image Processing by Jorma Kekalainen
Gaussian hp filtered (size=256,
std=10 and std=30) DFT-image
Jorma Kekalainen
Digital Image Processing
1140
Digital Image Processing
Removal of Periodic Noise
Lecture weeks 21 and 22
Page 39
Lecture notes
Digital Image Processing by Jorma Kekalainen
Removal of periodic noise
Earlier we investigated a number of different
noise types which can affect images.
However, we did not look at periodic noise.
Periodic noise may occur if the imaging
equipment (the acquisition or networking
hardware) is subject to electronic disturbance
of a repeating nature, such as may be caused
by an electric motor.
Jorma Kekalainen
Digital Image Processing
1142
Creating periodic noise
We can synthetically create periodic noise by overlaying an
image with a trigonometric function,e.g.,:
[x,y]=meshgrid(1:256,1:256);
s=1+sin(x+y/1.5);
Cp=(double(C)/128+s)/4;
where C is the cameraman image from previous sections.
The second line simply creates a sine function, and adjusts its
output to be in the range 0 2.
The last line first adjusts the cameraman image to be in the
same range; adds the sine function to it, and divides by 4 to
produce a matrix of type double with all elements in the
range 0.0 1.0.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1143
Page 40
Lecture notes
Digital Image Processing by Jorma Kekalainen
Image with periodic noise, and its
transform
This can be viewed directly with imshow, and it is shown
below .
The extra two spikes away from the center
We can produce its DFT: correspond to the noise just added. In general
>> cpf=fftshift(fft2(cp)); the smaller the period of the noise, the further
from the center the two spikes will be.
Jorma Kekalainen
Digital Image Processing
This is because a
small period
corresponds to a
high frequency
(large change over
a small distance),
and is therefore
further away from
the center of the
1144
shifted transform.
Removing noise spikes
We will now remove these extra spikes, and invert the result.
If we use data cursor and move it around the image, we find that the
spikes have row, column values of (156,170) and (102,88).
These have the same distance from the center: 49.0918.
We can check this by
>>z=sqrt(x.^2+y.^2);
>> z(156,170)
>> z(102,88)
ans =49.0918
ans =49.0918
There are two methods we can
use to eliminate the spikes
Band reject filtering
Jorma Kekalainen
Notch
filtering
Lecture weeks 21 and 22
Digital Image Processing
1145
Page 41
Lecture notes
Digital Image Processing by Jorma Kekalainen
Band reject filtering
We create a filter consisting of ones with a ring of zeroes; the
zeroes lying at a radius of 49 from the center:
br=(z < 47 | z > 51);
where z is the matrix consisting of distances from the origin.
This particular ring will have a thickness large enough to cover the
spikes.
Then as before, we multiply this by the transform:
cpfbr=cpf.*br
The result is that the spikes have been blocked out by this filter.
Taking the inverse transform produces the image
Cpfbri=ifft2(Cpfbr);
figure, fftshow(Cpfbri,'log')
Jorma Kekalainen
Digital Image Processing
1146
Removing periodic noise with a
band-reject filter
Note that not all the noise has gone, but a significant amount
has, especially in the center of the image.
A band-reject filter
Jorma Kekalainen
Lecture weeks 21 and 22
After inversion
Digital Image Processing
1147
Page 42
Lecture notes
Digital Image Processing by Jorma Kekalainen
Notch filtering
With a notch filter, we simply make the rows
and columns of the spikes zero:
cpf(156,:)=0;
cpf(102,:)=0;
cpf(:,170)=0;
cpf(:,88)=0;
Jorma Kekalainen
Digital Image Processing
1148
Removing periodic noise with a
notch filter
As before, much of the noise in the center has been removed.
Making more rows and columns of the transform zero would result in a
larger reduction of noise.
After inversion
A notch filter
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1149
Page 43
Lecture notes
Digital Image Processing by Jorma Kekalainen
Digital Image Processing
Inverse Filtering
Inverse filtering
We have seen that we can perform filtering in the Fourier
domain by multiplying the DFT of an image by the DFT of a
filter: this is a direct use of the convolution theorem.
We thus have
where X is the DFT of the image; F is the DFT of the filter, and
Y is the DFT of the result.
If we are given Y and F, then we should be able to recover the
DFT of the original image X simply by dividing by F:
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1151
Page 44
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
Suppose, for example we take the cameraman image
cameraman.tif, and blur it using a low-pass Butterworth filter:
C=imread('cameraman.tif');
Cf=fftshift(fft2(C));
b=lbutter(C,15,2);
Cb=Cf.*b;
Cba=abs(ifft2(Cb));
Cba=uint8(255*mat2gray(Cba));
imshow(Cba)
The result is shown beside.
Jorma Kekalainen
Digital Image Processing
1152
Example
We can attempt to recover the
original image by dividing by the
filter:
C1=fftshift(fft2(Cba))./b;
C1a=abs(ifft2(C1));
imshow(mat2gray(C1a))
The result is shown beside and
there is no improvement!
Jorma Kekalainen
Lecture weeks 21 and 22
An attempt to recover
by inverse filtering
Digital Image Processing
1153
Page 45
Lecture notes
Digital Image Processing by Jorma Kekalainen
Conclusion and solution
The trouble is that some elements of the Butterworth matrix
are very small, so dividing produces very large values which
dominate the output.
We can deal with this problem in two ways:
1. Apply a low pass filter L to the division:
This should eliminate very low (or zero) values.
2. Constrained division: choose a threshold value d, and if
|F(i, j)| < d, we dont perform a division, but just keep our
original value:
Jorma Kekalainen
Digital Image Processing
1154
Example
We can apply the first method by multiplying a Butterworth
low pass filter to the division:
Cbf=fftshift(fft2(Cba));
C1=(Cbf./b).*lbutter(C,40,10);
C1a=abs(ifft2(C1));
imshow(mat2gray(C1a))
Next figure shows the results obtained by using a different
cutoff radius of the Butterworth filter each time: (a) 30, (b) 40
(as in the Matlab commands just given), (c) 50, (d) 60, and
(e) 80.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1155
Page 46
Lecture notes
Digital Image Processing by Jorma Kekalainen
n=10
It seems that using a low pass filter with a cutoff
round about 50 will yield the best results.
After we use larger cutoffs, the result degenerates.
D=30,40,50,60,70,80
Jorma Kekalainen
Digital Image Processing
1156
Example
C=imread('cameraman.tif');
Cf=fftshift(fft2(C));
% Blurring the image
b=lbutter(C,15,2);
Cb=Cf.*b;
Cba=abs(ifft2(Cb));
Cba=uint8(255*mat2gray(Cba));
figure, imshow(Cba)
title('Image blurred by Bu-lpf (D=15, n=2)')
%
%We can attempt to recover the original image by dividing by the filter:
%
C1=fftshift(fft2(Cba))./b;
C1a=abs(ifft2(C1));
figure, imshow(mat2gray(C1a))
title('Recovery attempt of the original image at inverse filtering')
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1157
Page 47
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
We can at randomly test on the ability of Butterworth
low pass filter with different cutoff radius and order to
eliminate very low values.
promt1='Give filter order n=';
n=input(promt1)
promt2='Give cutoff radius D=';
D=input(promt2)
Cbf=fftshift(fft2(Cba));
C1=(Cbf./b).*lbutter(C,D,n);
C1a=abs(ifft2(C1));
figure, imshow(mat2gray(C1a))
title(['Inverse filtering using Bu-lpf (D=',num2str(D),',
n=',num2str(n),') to eliminate
zeros'])
Jorma Kekalainen
Digital Image Processing
1158
Inverse filtering using
constrained division
We can try the second method; to implement
it we simply make all values of the filter which
are too small equal to 1 e.g.:
d=0.01;
b=lbutter(C,15,2);b(find(b<d))=1;
C1=fftshift(fft2(Cba))./b;
C1a=abs(ifft2(C1));
imshow(mat2gray(C1a))
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1159
Page 48
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
We can systematically search for suitable
threshold d with certain cutoff radius e.g.:
for d=0:0.001:0.01;
b=lbutter(C,15,2);b(find(b<d))=1;
C1=fftshift(fft2(Cba))./b;
C1a=abs(ifft2(C1));
figure, imshow(mat2gray(C1a))
title(['All small Bu-lpf outputs(<',num2str(d),')
are set equal to 1'])
end
Jorma Kekalainen
Digital Image Processing
1160
Inverse filtering using constrained
division
d<0.001
d<0.005
d<0.01
It seems that using a
threshold d in the
range 0.005d 0.01
produces reasonable
results.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1161
Page 49
Lecture notes
Digital Image Processing by Jorma Kekalainen
Motion deblurring
We can consider the removal of blur caused by motion to be a
special case of inverse filtering.
Suppose we take an image with text and blur it by a small
amount.
bc=imread('board.tif');
bg=im2uint8(rgb2gray(bc));
b=bg(100:355,30:285);
figure,imshow(b)
These commands simply take the color image of a circuit
board (the image board.tif), makes a grayscale version of data
type uint8, and picks out a square subimage.
JormaThe
result is shown in the
following figure.
Kekalainen
Digital Image Processing
1162
Originals
board.tif
Jorma Kekalainen
Lecture weeks 21 and 22
Grayscaled subimage
Digital Image Processing
1163
Page 50
Lecture notes
Digital Image Processing by Jorma Kekalainen
Motion blurred image
To blur it, we can use the blur parameter of the fspecial
function.
m=fspecial('motion',11,0);
bm=imfilter(b,m);
imshow(bm)
The result of the
blur has effectively
obliterated the text
on the image.
Jorma Kekalainen
Digital Image Processing
1164
fspecial('motion',len,theta)
m= fspecial('motion',len,theta) returns a filter to
approximate, once convolved with an image, the
linear motion of a camera by len pixels, with an
angle of theta degrees in a counterclockwise
direction.
The filter becomes a vector for horizontal and
vertical motions.
The default len is 9 and the default theta is 0,
which corresponds to a horizontal motion of nine
pixels.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1165
Page 51
Lecture notes
Digital Image Processing by Jorma Kekalainen
Note
fspecial(type) creates a two-dimensional filter of the specified
type. type is a string having one of these values.
Value
Description
average
disk
gaussian
laplacian
Averaging filter
Circular averaging filter (pillbox)
Gaussian lowpass filter
Approximates the two-dimensional Laplacian
operator
Laplacian of Gaussian filter
Approximates the linear motion of a camera
Prewitt horizontal edge-emphasizing filter
Sobel horizontal edge-emphasizing filter
log
Motion
Prewitt
Sobel
Jorma Kekalainen
Digital Image Processing
1166
Attempts at removing motion blur
To deblur the image, we need to divide its transform by the
transform corresponding to the blur filter.
This means that we first must create a matrix corresponding
to the transform of the blur:
m2=zeros(256,256);
m2(1,1:11)=m;
mf=fft2(m2);
Now we can attempt to divide by this transform.
bmi=ifft2(fft2(bm)./mf);
fftshow(bmi,'abs')
and the result is shown in the following figure.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1167
Page 52
Lecture notes
Digital Image Processing by Jorma Kekalainen
Attempts at removing motion blur
As with inverse
filtering, the result
is not particularly
good, because the
values close to zero
in the matrix mf
have tended to
dominate the
result.
Jorma Kekalainen
Straight division
Digital Image Processing
1168
Attempts at removing motion blur
As above, we can constrain the division by only dividing by
values which are above a certain threshold e.g.:
d=0.02;
mf=fft2(m2);mf(find(abs(mf)<d))=1;
bmi=ifft2(fft2(bm)./mf);
imshow(mat2gray(abs(bmi))*2)
where the last multiplication by 2 just brightens the result,
which is shown in the following figure.
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1169
Page 53
Lecture notes
Digital Image Processing by Jorma Kekalainen
Attempts at removing motion blur
The writing,
especially in the
centre of the image,
is now quite legible.
Jorma Kekalainen
Constrained division
Digital Image Processing
1170
Compare
Original and
blurred
Jorma Kekalainen
Lecture weeks 21 and 22
Straight and constrained division
Digital Image Processing
1171
Page 54
Lecture notes
Digital Image Processing by Jorma Kekalainen
Example
%Suppose we take an image and blur it by a small amount.
bc=imread('board.tif');
bg=im2uint8(rgb2gray(bc));
b=bg(100:355,30:285);
figure, imshow(b)
title('Take the color image and make a grayscale version of data type uint8')
xlabel('and pick out a square subimage')
m=fspecial('motion',11,0);% Blurring filter
bm=imfilter(b,m);
figure, imshow(bm)
title('Blurred image with filter fspecial(''motion'',11,0)')
%To deblur the image, we need to divide its transform by the transform corresponding to the blur filter
m2=zeros(256,256);
m2(1,1:11)=m;%
mf=fft2(m2);% First create a matrix corresponding to the transform of the blur
%Now we can attempt to divide by this transform.
bmi=ifft2(fft2(bm)./mf);
figure, fftshow(bmi,'abs')
title('Recovery attempt of the original image through straight division')
d=0.02;% Discrimination threshold
mf=fft2(m2);mf(find(abs(mf)<d))=1;
bmi=ifft2(fft2(bm)./mf);
figure, imshow(mat2gray(abs(bmi))*2);% The last multiplication by 2 just brightens the result
title(['Constrained division using threshold d = ',num2str(d)])
Jorma Kekalainen
Lecture weeks 21 and 22
Digital Image Processing
1172
Page 55