Digital Image Processing - Lecture Weeks 21 and 22
Digital Image Processing - Lecture Weeks 21 and 22
Page 1
Lecture notes
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
1067
Page 2
Lecture notes
Low pass
Jorma Kekalainen
High pass
1068
1069
Page 3
Lecture notes
Then the inverse Fourier transform of the elementwise product of F and m is the result we require:
Jorma Kekalainen
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
1071
Page 4
Lecture notes
Jorma Kekalainen
1072
Example
Jorma Kekalainen
1073
Page 5
Lecture notes
Jorma Kekalainen
1074
1075
Page 6
Lecture notes
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
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
1077
Page 7
Lecture notes
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
1078
Jorma Kekalainen
1079
Page 8
Lecture notes
Jorma Kekalainen
1080
Jorma Kekalainen
1081
Page 9
Lecture notes
Jorma Kekalainen
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
(b) Cutoff 30
1083
Page 10
Lecture notes
Jorma Kekalainen
1084
Comparison
Jorma Kekalainen
1085
Page 11
Lecture notes
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)])
1086
Page 12
Lecture notes
Jorma Kekalainen
1088
Jorma Kekalainen
1089
Page 13
Lecture notes
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.
1090
Jorma Kekalainen
1091
Page 14
Lecture notes
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
Jorma Kekalainen
1093
Page 15
Lecture notes
Examples
Cutoffs: c>15, c>30, c>60
Jorma Kekalainen
1094
1095
c>50
c>100
c>120
Jorma Kekalainen
Page 16
Lecture notes
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
1097
Page 17
Lecture notes
Butterworth filters
Butterworth filters are based on the following functions for
low pass filters and for high pass filters :
Jorma Kekalainen
1098
Jorma Kekalainen
1099
Page 18
Lecture notes
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
1100
Jorma Kekalainen
1101
Page 19
Lecture notes
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
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
1103
Page 20
Lecture notes
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
1104
Compare
Jorma Kekalainen
1105
Page 21
Lecture notes
Exercise
Study effects of Butterworth low pass filter
with different order (n) and cutoff radius (D)
on cameraman.tif image
Jorma Kekalainen
1106
Solution
Jorma Kekalainen
1107
Page 22
Lecture notes
n=1,D=1,10,20,30
Jorma Kekalainen
1108
n=1,D=40,50,100
Jorma Kekalainen
1109
Page 23
Lecture notes
n=2,D=1,10,20,30
Jorma Kekalainen
1110
n=2,D=40,50,100
Jorma Kekalainen
1111
Page 24
Lecture notes
n=4,D=1,10,20,30
Jorma Kekalainen
1112
n=4,D=40,50,100
Jorma Kekalainen
1113
Page 25
Lecture notes
n=8,D=1,10,20,30
Jorma Kekalainen
1114
n=8,D=40,50,100
Jorma Kekalainen
1115
Page 26
Lecture notes
D=50,n=16,32,64,128
Jorma Kekalainen
1116
Exercise
Study effects of Butterworth high pass filter
with different order (n) and cutoff radius (D)
on cameraman.tif image
Jorma Kekalainen
1117
Page 27
Lecture notes
D=1
D=5
D=10
D=20,
D=50,
Jorma Kekalainen
D=100
1118
Jorma Kekalainen
1119
Page 28
Lecture notes
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
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
Page 29
Lecture notes
Jorma Kekalainen
1122
1123
Page 30
Lecture notes
Jorma Kekalainen
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
1125
Page 31
Lecture notes
Example
Jorma Kekalainen
1126
Example
Jorma Kekalainen
1127
Page 32
Lecture notes
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
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
1129
Page 33
Lecture notes
Example
Jorma Kekalainen
1130
Example
Jorma Kekalainen
1131
Page 34
Lecture notes
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
1133
Page 35
Lecture notes
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
Jorma Kekalainen
1135
Page 36
Lecture notes
1136
=30
Jorma Kekalainen
1137
Page 37
Lecture notes
1138
=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.
Page 38
Lecture notes
Jorma Kekalainen
1140
Page 39
Lecture notes
1142
1143
Page 40
Lecture notes
Jorma Kekalainen
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.
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
1145
Page 41
Lecture notes
Jorma Kekalainen
1146
Jorma Kekalainen
After inversion
1147
Page 42
Lecture notes
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
1148
Jorma Kekalainen
1149
Page 43
Lecture notes
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
1151
Page 44
Lecture notes
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
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
An attempt to recover
by inverse filtering
1153
Page 45
Lecture notes
Jorma Kekalainen
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
1155
Page 46
Lecture notes
n=10
D=30,40,50,60,70,80
Jorma Kekalainen
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
1157
Page 47
Lecture notes
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
1159
Page 48
Lecture notes
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
1160
d<0.005
d<0.01
1161
Page 49
Lecture notes
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
Grayscaled subimage
1163
Page 50
Lecture notes
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
1165
Page 51
Lecture notes
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
1166
1167
Page 52
Lecture notes
Jorma Kekalainen
Straight division
1168
Jorma Kekalainen
1169
Page 53
Lecture notes
Jorma Kekalainen
Constrained division
1170
Compare
Original and
blurred
Jorma Kekalainen
1171
Page 54
Lecture notes
Example
Jorma Kekalainen
1172
Page 55