Report
Report
EE40054
Author: Lecturer:
Alexandros Vlissidis Dr. Adrian Evans
Contents
1 Introduction 1
2 Implementation 1
2.1 Linear Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.1.1 Mean Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.1.2 Gaussian Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1.3 Frequency Domain Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.4 Unsharph Masking Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Non-Linear Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.2.1 Median Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2.2 Weighted Median Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2.3 Adaptive Weighted Median Filter . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.4 Truncated Median Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3 Morphological Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3.1 Erosion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3.2 Dilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Page i
chapter: Implementation Alexandros Vlissidis
1 Introduction
This assignment explores the use of linear, non-linear and morphological filters on various images.
These filters can be used to remove impulsive noise from images to prepare them for edge detection
and feature extraction. This report focuses on two images, one ultasound image of a foetus, and
a SAR image of New Zealand. The functions described in this report assume double images as
inputs, in order to make use of MATLAB’s statistics functions.
2 Implementation
2.1 Linear Filters
Linear filters use convolution in order to smooth an image. This efffectively removes the noise
from an image and improves the result of edge detection algorithms, by convolving the input
image with the impulse response of the filter. The negative effects of linear filters is that they
also blur the image, thus removing detail, and have poor performance at the presence of impulsive
noise. The general formula of linear filters is given in the equation below.
PN
i=1 αi x(i)
y(k) = P N
, x(i) ∈ Wx (1)
i=1 αi
Where x(i) are the input image pixels and α are the weights, which are multplied with the pixel
values. The output pixel value is y(k). Equation (1) has been implemented in MATLAB as a
generic function that can be used by all linear filtes in order to reduce code duplication. The
main loop of this function is shown below.
f o r i = (1+ rpad ) : ( rows−rpad )
f o r j = (1+ cpad ) : ( c o l s −cpad )
% E x t r a c t t h e window a r e a
window = o r i g i n a l ( ( i −rpad ) : ( i+rpad ) , ( j −cpad ) : ( j+cpad ) ) ;
i f wsum ˜= 0
out ( i , j ) = sum ( sum ( window . ∗ k e r n e l ) ) /wsum ;
else
% Dont d i v i d e by t h e sum o f t h e w e i g h t s
out ( i , j ) = abs ( sum ( sum ( window . ∗ k e r n e l ) ) ) ;
end
end
end
This function takes as input the kernel and the original image. It scans the window across the
image, convolving the kernel with the window. In this way, this function can be used by any
linear filter, as long as they provide an image and a kernel or correct dimensions. However, in
some filters the sum of the weights adds up to zero. Hence, there is a check for that case.
Page 1 of 20
chapter: Implementation Alexandros Vlissidis
The kernel has 9 elements, and spans 1 element around the central index. Equation (2) has been
implemented in MATLAB.
f u n c t i o n [ out ] = m e a n f i l t e r ( o r i g i n a l , s i z e )
out = a p p l y l i n e a r f i l t e r ( o r i g i n a l , k e r n e l ) ;
f i g u r e ; imshow ( out )
end
The code for the mean filter implementation has been simplified a lot by the use of the function
described in Listing 1. The mean filter builds a kernel with weights that sum up to one. So each
weight must have the value of one over the number of cells in the kernel. The results of this filter
are illustrated below.
It can be seen from the figures that as the kernel size increases more smoothing is achieved. This
is because more of the image is taken into account in the averaging, blurring the edges even more.
Page 2 of 20
chapter: Implementation Alexandros Vlissidis
Where x and y are the indices of the elements, relative to the central value in the window and
σ is the standard deviation desires, which is essentially the order of the filter. The size of the
gaussian mask is set to 2(3σ) + 1. This filter has been implemented as a MATLAB function
below.
f u n c t i o n [ out ] = g a u s s i a n ( o r i g i n a l , sigma )
k e r n e l = m a k e g a u s s i a n k e r n e l ( sigma ) ;
out = a p p l y l i n e a r f i l t e r ( o r i g i n a l , k e r n e l ) ;
f i g u r e ; imshow ( out )
end
This code is severely simplified due to the use of the general linear filter function and a factory
method to create a gaussian kernel. This is shown below.
f u n c t i o n [ k e r n e l ] = m a k e g a u s s i a n k e r n e l ( sigma )
% C a l c u l a t e t h e padding
pad = c e i l ( k s i z e / 2 ) ;
% Populate the k e r n e l
for i = 1: ksize
for j = 1: ksize
% Translate the i n d i c e s r e l a t i v e to the c e n t r a l c e l l
k e r n e l ( i , j ) = g a u s s ( i −pad , j −pad , sigma ) ;
end
end
end
f u n c t i o n [ v a l u e ] = g a u s s ( x , y , sigma )
v a l u e = exp ( −(x ˆ2 + y ˆ 2 ) / ( 2 ∗ sigma ˆ 2 ) ) ;
end
Here the function takes the standad deviation of the filter as an input. From that it can infer the
size of the kernel needed. The kernel is populated by using a gaussian function which takes the
indices of the cell relative to the central value, as input, as well as the standard deviation. The
results of this filter are shown below.
Page 3 of 20
chapter: Implementation Alexandros Vlissidis
(c) σ = 2 (d) σ = 3
The result seem to show what is expected. Since this is a smoothing filter, as the standard
deviation increases the image blur is increased, as the kernel size increases and a larger area is
taken into account in the convolution. The purpose of applying a smoothing filter is to improve
edge detection, since together with the edges, the noise of the image is smoothed as well. The
improvement in edge detection is shown below.
Page 4 of 20
chapter: Implementation Alexandros Vlissidis
It is clear from this test that this gaussian smoothing filter has a desirable effect of edge detection.
As smoothing is applied, the noise in the image is less prominent, and hence less edges are detected
compared to the original image.
In order to apply a filter in the frequency domain, the Fourier Transform of the image must
be calculated. This is a compute intensiver process. However, if the dimensions of our image are
a power of two, then the much faster FFT algorithm can be applied. In this case, a Butterworth
low-pass filter has been implemented. A low-pass filter will effectively cut-off the edges, which
are the high frequency components of the image, as the pixel values change rapidly at the edges.
The equation of a 2D Butterworth filter in the frequency domain is shown below.
1
H(u, v) = , u = 0, 1, ..., M − 1, v = 0, 1, ..., N − 1 (4)
1 + ( Frc )2p
Where Fc is the cutoff frequency of the filter. This is the frequency where the filter’s response
has reached half of its maximum value. The order of the filter p controls the smootheness of
the filter. Decrasing the order of the filter increases the smoothing applied. Finally, r is the 2D
version of the distance from the image’s DC component.
p
r = (u − uo )2 + (v − vo )2 (5)
Page 5 of 20
chapter: Implementation Alexandros Vlissidis
Where uo and vo are the indices at the DC component of the image. This equation corresponds
to a rotation of the 1D version of the filter, which generated a circular region where the filter
applies. The frequency response of this filter is illustrated below.
[ rows , c o l s ] = s i z e (F) ;
H = z e r o s ( rows , c o l s ) ;
r c e n t e r = rows / 2 ;
ccenter = cols /2;
% Crop t h e r e s u l t t o t h e i n i t i a l size
out = out ( 1 : r , 1 : c ) ;
f i g u r e ; imshow ( out )
f i g u r e ; mesh ( 1 : c o l s , 1 : rows , H) ;
colormap ( c o o l ( 1 0 0 ) )
end
Page 6 of 20
chapter: Implementation Alexandros Vlissidis
As mentioned before in order to apply an FFT algorithm, the size of the image must be a power
of 2. Hence the first thing the function does is to work out the size the image needs to be, and
passes that to MATLAB’s fft2 function which pads it with zeros to reach that size. Then we
must shift the result so that the DC component aligns with the center of the image. This is
achieved with MATLAB’s fftshift function. Then the mask is populated using Equation (4). The
much faster multiplication of the mask H and the image F , gives us our result. Then we must
translate back to the spatial domain with an inverse FFT function, and crop the image to the
initial size. The result of this low pass filter is shown below.
It is obvious that the high frequencies have been cut out and this has blurred the image. However,
in order to improve edge detection a high pass filter would be more appropriate, since that would
in theory only allow the high frequencies. This can be achieved using the same code, only changing
the response of the filter to be 1 − H(u, v). The response of a Butterworth high-pass filter is
shown below.
Page 7 of 20
chapter: Implementation Alexandros Vlissidis
It can be seen that the edge detection is improved after we apply the high-pass filter, however, still
a lot of noise is present and detected as an edge. The Gaussian filter had a better performance
in edge detection.
Page 8 of 20
chapter: Implementation Alexandros Vlissidis
[ rows , c o l s ] = s i z e ( o r i g i n a l ) ;
out = z e r o s ( rows , c o l s ) ;
rpad = ( k s i z e −1) / 2 ;
cpad = ( k s i z e −1) / 2 ;
% Apply t h e a d a p t i v e f i l t e r f u n c t i o n
out ( i , j ) = m + k ∗ ( o r i g i n a l ( i , j ) − m) ;
end
end
f i g u r e ; imshow ( out )
end
(c) k = 0.1
It is obvious that with a value close to 1, no smoothing is performed. However, with k = 0.1, the
Page 9 of 20
chapter: Implementation Alexandros Vlissidis
Where xl (i) is the lth smallest value in the window, and αl is the weight for the lth position.
Hence for this equation to apply the pixel values of the window must be sorted and then the
weights must be applied. However, in the non-linear version, the weight do not correspond to a
multiplication, but represent the number of times that pixel value must be inserted in the sorted
list. After, this is applied, the median of the final list is the outputs value of the filter, y(k).
This has been implemented as a generic function, in MATLAB below. This allows for more code
re-use between all the median filters.
f u n c t i o n [ out ] = a p p l y n o n l i n e a r f i l t e r ( o r i g i n a l , w e i g h t s , k s i z e )
[ rows , c o l s ] = s i z e ( o r i g i n a l ) ;
out = z e r o s ( rows , c o l s ) ;
rpad = ( k s i z e −1) / 2 ;
cpad = ( k s i z e −1) / 2 ;
% S o r t t h e window
mask = s o r t ( window ( 1 : end ) ) ;
l i s t = [ ] ; % Empty t h e f i n a l l i s t
f o r k = 1: length ( weights )
% B u i l d t h e temporary l i s t f o r t h i s v a l u e and w e i g h t
b u f f e r = o n e s ( 1 , w e i g h t s ( k ) ) ∗ mask ( k ) ;
end
In this implementation at temporary buffer is built, to hold the number of values needed to be
inserted in the final list, which has the size of the weight for that specific pixel value. This can
easily be used by any non-linear filter, by just providing a list of the weights.
Page 10 of 20
chapter: Implementation Alexandros Vlissidis
% E v e r y t h i n g i s 0 e x c e p t from t h e middle e l e m e n t
weights = zeros (1 , length ) ;
w e i g h t s ( 1 , ( l e n g t h −1) / 2 ) = 1 ;
out = a p p l y n o n l i n e a r f i l t e r ( o r i g i n a l , w e i g h t s , k s i z e ) ;
f i g u r e ; imshow ( out )
end
The results of this filter can be shown using the New Zealand SAR image, which contains impulsive
(spekle) noise.
It can be seen that as the size of the kernel increases more smoothing is applied. However, even in
the extreme case the edges of the image are preserved and the impulsive noise in the flat regions
is completely removed. In theory this will enhance edge detection. This is illustrated below.
Page 11 of 20
chapter: Implementation Alexandros Vlissidis
It is clear that with the median filter applied, the outline of the island can be detected much
more clearly, excluding the noise in the image.
out = a p p l y n o n l i n e a r f i l t e r ( o r i g i n a l , w e i g h t s , k s i z e ) ;
f i g u r e ; imshow ( out )
end
This implements a filter with all the weights set to 1 and the middle weight to 3. The results of
this function are illustrated below.
Page 12 of 20
chapter: Implementation Alexandros Vlissidis
We can see that the center weighted median filter has the same results with a simple median
filter. We can then safely assume that it has the same ability to remove impulsive noise and thus
improve edge detection algorithms.
Page 13 of 20
chapter: Implementation Alexandros Vlissidis
f u n c t i o n [ out ] = a d a p t i v e w e i g h t e d m e d i a n ( o r i g i n a l , k s i z e , cweight , c )
[ rows , c o l s ] = s i z e ( o r i g i n a l ) ;
out = z e r o s ( rows , c o l s ) ;
rpad = ( k s i z e −1) / 2 ;
cpad = ( k s i z e −1) / 2 ;
d = zeros ( ksize , ksize ) ;
mpos = c e i l ( k s i z e / 2 ) ; % Work out t h e middle matrix e l e m e n t
% Pre−s t o r e t h e d i s t a n c e matrix
for i = 1: ksize
for j = 1: ksize
d ( i , j ) = d i s t a n c e ( [ i j ] , [ mpos mpos ] ) ;
end
end
sigma = s t d ( window ) ;
x = mean ( window ) ;
% i f t h e mean o f t h e window i s z e r o s k i p t h i s i t e r a t i o n
i f x == 0
out ( i , j ) = x ;
continue
end
% Compute t h e w e i g h t s f o r t h i s window
idx = 1;
for k = 1: ksize
for q = 1: ksize
% Calculate weight f o r t h i s element
w e i g h t = round ( c w e i g h t − ( c ∗ d ( k , q ) ∗ sigma ) /x ) ;
% C r e a t e t h e new v a l u e s t o i n s e r t
b u f f e r = o n e s ( 1 , w e i g h t ) ∗ window ( i d x ) ;
idx = idx + 1;
% I n s e r t new v a l u e s
l i s t = [ list , buffer ] ;
end
end
out ( i , j ) = median ( l i s t ) ;
end
end
f i g u r e ; imshow ( out )
end
function [ d ] = distance (x , y)
% C a l c u l a t e t h e e u c l i d e a n d i s t a n c e between two v e c t o r s
d = s q r t ( sum ( ( x − y ) . ˆ 2 ) ) ;
end
Page 14 of 20
chapter: Implementation Alexandros Vlissidis
This implementation build a matrix with the distances from the middle element, as they are
constant and do not need to be calculated in every loop iteration. This is stored in matrix d.
Then the window area is extracted and sorted. The next step is to calculate the statistics of the
window, but if the mean is zero, the iteration is skipped, as the weight equation would have a
division by zero. For each weight a buffer is built, of size weight and values of the corresponding
pixel in the window. It is much faster to built a buffer and then contatinate with a final list,
rather than inserting new values one at a time. In addition, this speeds up the sorting. Since the
window is already sorted, we do not have to sort again for the larger final list, as the values will
still be in order. The results of this filter are shown below.
It is obvious that this median filter has a really good performance at removing spekle noise. As
the kernel size increase almost all the noise is gone, but the edges are still preserved. Some detail
is still lost, however.
Page 15 of 20
chapter: Implementation Alexandros Vlissidis
Since the median filter has removed most of the spekle noise the edge detection algorithm can
more clearly find the edges of the island. The dark side, however, is aparently too dark to be
detected, i.e. it has too low intensity to be perceived as an edge. However, the adaptive weighted
median filter has improved the edge detection.
Because the window has a small number of values in it, there is a high probability that there is
not going to be a mode, as all the values will probably be unique. Hence the mode filter tries
to approximate the mode by cutting the extreme values, and taking the median of what is left.
This has been implemented in MATLAB below.
Page 16 of 20
chapter: Implementation Alexandros Vlissidis
f u n c t i o n [ out ] = t r u n c a t e d m e d i a n ( o r i g i n a l , k s i z e )
[ rows , c o l s ] = s i z e ( o r i g i n a l ) ;
out = z e r o s ( rows , c o l s ) ;
rpad = ( k s i z e −1) / 2 ;
cpad = ( k s i z e −1) / 2 ;
% C a l c u l a t e t h e d i s t a n c e s from t h e median
upperd = max( window ) − med ;
l o w e r d = med − min ( window ) ;
% Work out t h e c u t o f f v a l u e
i f upperd > l o w e r d
d i s t a n c e = upperd ;
e l s e i f upperd < l o w e r d
d i s t a n c e = lowerd ;
end
% Cut t h e extreme v a l u e s
window = window ( abs ( ( window − med) ) < d i s t a n c e ) ;
f i g u r e ; imshow ( out )
end
This function calculated the median and then the distance of the median from minimum and
maximum values in the window. The smaller distance of the two is kept as a cutoff value. The
window values that are kept are the one’s whose distance from the median is less that the cutoff
distance. The median of the new window must approximate the mode more accurately.
Page 17 of 20
chapter: Implementation Alexandros Vlissidis
It can be seen that this filter is a bit more agressive at the edges than the median filters. It is
able to remove impulsive noise as well as all the median filter. However, the edges appear to be
more sharp, even at large mask sizes. We can assume that this will aid edge detection even more.
It is clear that the truncated median filter not only improves the edge detection with respect to
the original image, but also it has better performance that the adaptive weighed median filter.
Less of the noise is perceived as edges, and generally the edges are more clear. It still cannot
Page 18 of 20
chapter: Implementation Alexandros Vlissidis
make the edge detection algorithm perceive the dark side of the island as an edge.
[ rows , c o l s ] = s i z e ( o r i g i n a l ) ;
[ serows , s e c o l s ] = s i z e ( s e ) ;
out = z e r o s ( rows , c o l s ) ;
It can be seen that after the window is extracted, the pixel values to be sorted are selected using
logical indexing according to the structuring element, which is a simple matlab matrix, with ones
in the positions that matter and zeros elsewhere. Hence, this function is generic enough to be
used with any structuring element as well as for erosion and dilation. The order of the filter will
determine whether erosion or dilation will take place.
2.3.1 Erosion
In order to perform erosion, the function described in Listing 12 is used, with a low order value.
This means that the output value of the filter will be in the low values of the window. Hence,
at the edges, which have high intensity, the shape of an object will shrink. The output image
will take the value of the flat regions which have lower pixel values. The erosion function can
successfully remove spekle noise from the nzers1.jpg image.
Page 19 of 20
chapter: Implementation Alexandros Vlissidis
(c) Erosion, se = 5x5, order = 10 (d) Erosion Edge Detection, se = 5x5, order = 10
The Erosion filter successfully removed the spekle noise from this image, thus allowing for a much
better edge detection that without any filtering. In fact, it performs particularly, well. This is
because is resembles the function of a median filter. The difference is that it has a more flexible
structuring element structure, which allows to look for specific shapes, and a more flexible output
pixel, as it does not have to be the median of the window.
2.3.2 Dilation
Dilation is a filter whose effect if opposite to erosion. It changes the shapes in an image, by
expanding its edges. This can help in repairing breaks in images or intrusions. For example, it
an image containing a handwritten character, which has breaks, a dilation can repair this hole.
This is done by using the same technique as erosion. However, this time, the output value of the
filter is selected from the high values of the window. Hence, the edges of objects, which have high
values, are expanded into the flat regions, with low pixel values. This can be illustrated using
the rice.png MATLAB image.
Page 20 of 20