You are on page 1of 36

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Noises and Noise Cleaning
Methods

Lecture weeks 15 and 16

Page 1

Lecture notes

Digital Image Processing by Jorma Kekalainen

Definition of noise
We may define noise to be any degradation in the image
signal, caused by external disturbance.
If an image is being sent electronically from one place to
another, via satellite or wireless transmission, or through
cable, we may expect errors to occur in the image signal.
These errors will appear on the image output in different ways
depending on the type of disturbance in the signal.
Usually we know what type of errors to expect, and hence the
type of noise on the image; hence we can choose the most
appropriate method for reducing the effects.
Cleaning an image corrupted by noise is thus an important
area of image restoration.
Jorma Kekalainen

Digital Image Processing

787

Types of noise
Now we will investigate some of the standard
noise forms, and the different methods of
eliminating or reducing their effects on the
image.
Impulse noise (also called shot noise, binary noise
or salt and pepper noise)
Gaussian noise
Speckle noise
Periodic noise
Note: Other theoretical noise models are Poisson, Rayleigh, Gamma and Exponential
Jorma Kekalainen
Digital Image Processing
788
noises.
They can be used for approximating
skewed histograms.

Lecture weeks 15 and 16

Page 2

Lecture notes

Digital Image Processing by Jorma Kekalainen

Impulse noise
This degradation can be caused by sharp, sudden
disturbances in the image signal; its appearance is
randomly scattered white or black (or both) pixels
over the image.
To demonstrate its appearance, we will use the
familiar gray-scale image:
c=imread('cameraman.tif');
figure, imshow('cameraman.tif');
title('Clean image')

Jorma Kekalainen

Digital Image Processing

789

Imnoise function
To add noise, we use the Matlab function imnoise, which
takes a number of different parameters.
To add salt and pepper noise:
>> c_sp=imnoise(c,'salt & pepper');
The amount of noise added is 10% (= default value) to add
more or less noise we include an optional parameter, being a
value between 0 and 1 indicating the fraction of pixels to be
corrupted.
E.g.,
>> imnoise(c,'salt & pepper',0.2);
would produce an image with 20% of its pixels corrupted by
salt and pepper noise.
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

790

Page 3

Lecture notes

Digital Image Processing by Jorma Kekalainen

Impulse noise
c=imread('cameraman.tif');
c_sp=imnoise(c,'salt & pepper');
figure, imshow(c_sp)
title('Image with 10% of pixels corrupted by binary noise')
c_sp=imnoise(c,'salt & pepper',0.2);
figure, imshow(c_sp)
title('Image with 20% of pixels corrupted by binary noise')

Jorma Kekalainen

Digital Image Processing

791

Gaussian noise
Gaussian noise is an idealized form of white
noise, which is caused by random fluctuations
in the signal
Electronic circuit noise and sensor noise

Gaussian noise is white noise which is


normally distributed
Easy to use mathematically
Therefore often used in practice even if the model is
not perfect
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

792

Page 4

Lecture notes

Digital Image Processing by Jorma Kekalainen

Gaussian noise
If the image is represented as I and the Gaussian
noise by N then we can model a noisy image by
simply adding the two:
I + N.
Here we may assume that I is a matrix whose
elements are the pixel values of our image, and N
is a matrix whose elements are normally
distributed.
It can be shown that this is an appropriate model
for noisy image.
Jorma Kekalainen

Digital Image Processing

793

imnoise(c,'gaussian');
The effect can again be demonstrated by the
imnoise function:

>> c_ga=imnoise(c,'gaussian');
As with salt and pepper noise, the gaussian
parameter also can take optional values,
giving the mean (m) and variance (v) of the
noise.
The default values are m=0.0 and v=0.01.
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

794

Page 5

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

c=imread('cameraman.tif');
c_ga=imnoise(c,'gaussian');
figure, imshow(c_ga)
title('Image with additive Gaussian noise with m=0 and
v=0.01')

Jorma Kekalainen

Digital Image Processing

795

Speckle noise
Whereas Gaussian noise can be modeled by random values added to an
image; speckle noise (or speckle) can be modeled by random values
multiplied by pixel values, hence it is also called multiplicative noise.
Speckle noise is a major problem in some radar applications.
As before, imnoise can do speckle:
>> c_spk=imnoise(c,'speckle');
In Matlab, speckle noise is implemented as
I+N*I= I(1+N)
where I is the image matrix, and N consists of normally distributed
values with mean 0.
An optional parameter gives the variance of N ; its default value is 0.04.

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

796

Page 6

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

c=imread('cameraman.tif');
c_spk=imnoise(c,'speckle');
figure, imshow(c_spk)
title('Image with speckle noise with m=0 and v=0.04')

Jorma Kekalainen

Digital Image Processing

797

Image corrupted by Gaussian and


speckle noise
Gaussian noise

Speckle noise

Note: Although Gaussian noise and speckle noise appear superficially similar, they are
formed by two totally different methods, and, thus require different approaches for
Kekalainen
Digital Image Processing
798
theirJorma
removal.

Lecture weeks 15 and 16

Page 7

Lecture notes

Digital Image Processing by Jorma Kekalainen

Periodic noise
If the image signal is subject to a periodic, rather than a
random disturbance, we might obtain an image
corrupted by 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
noise may be caused by an electric motor.
The effect of this kind noise is bars over the image.
The function imnoise does not have a periodic option,
but we can create our own periodic noise by adding a
periodic matrix (using a trigonometric function) to our
image.
Jorma Kekalainen

Digital Image Processing

799

Periodic noise

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

800

Page 8

Lecture notes

Digital Image Processing by Jorma Kekalainen

Periodic noise
We can create periodic noise by overlaying an image with a
trigonometric function:
c=imread('cameraman.tif');
s=size(c);
[x,y]=meshgrid(1:s(1),1:s(2));
pn=sin(x+y)+1;
c_pn=(im2double(c)+pn/2)/2;
figure,imshow(c_pn)
title('Image with periodic noise pn=sin(x+y)+1')
The first lines simply create a suitable sine matrix function, and
adjusts its output to be in the range 0 - 2.
The c_pn line first adjusts the cameraman image to be double class;
adds the scaled sine function to it, and divides by 2 to produce a
matrix of type double with all elements in the range 0.0 1.0.
This can be viewed directly with imshow.
Jorma Kekalainen

Digital Image Processing

801

Example
c=imread('cameraman.tif');
s=size(c);
[x,y]=meshgrid(1:s(1),1:s(2));
pn=sin(x+y)+1;
c_pn=(im2double(c)+pn/2)/2;
figure,imshow(c_pn)
title('Image with periodic noise pn=sin(x+y)+1')

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

802

Page 9

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
c=imread('cameraman.tif');
s=size(c);
[x,y]=meshgrid(1:s(1),1:s(2));
pn=sin(x/2+y/3)+1;
c_pn=(im2double(c)+pn/2)/2;
figure,imshow(c_pn)
title('Image with periodic noise pn=sin(x/2+y/3)+1')

Note: For example


>> [x,y]=meshgrid(1:3,1:3)
x=
1 2 3
1 2 3
1 2 3
y=
1 1 1
2 2 2
Jorma Kekalainen
3 3 3

Digital Image Processing

803

Example

[x,y]=meshgrid(1:256,1:256);
s=1+sin(x+y/1.5);
cp=(double(c)/128+s)/4;
figure,imshow(cp)
where c is the cameraman image.
The second line simply creates a sine function, and adjusts its output to be
in the range 0 - 2.
The next 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.
This can be viewed directly with imshow

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

804

Page 10

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
Salt and pepper noise, Gaussian noise and
speckle noise can all be cleaned by using
spatial filtering techniques.
Periodic noise, however, requires image
transforms for best results.

Jorma Kekalainen

Digital Image Processing

805

Digital Image Processing


Cleaning Impulse Noise

Lecture weeks 15 and 16

Page 11

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example

c=imread('cameraman.tif');
c_sp=imnoise(c,'salt & pepper');
figure, imshow(c_sp)
title('Image with 10% of pixels corrupted by binary noise')
a3=fspecial('average');
c_sp_a3=filter2(a3,c_sp);
figure, imshow(uint8(c_sp_a3))
title('Image with 10% of pixels corrupted by binary noise filtered
by 3*3 averaging filter')
a5=fspecial('average',[5,5]);
c_sp_a5=filter2(a5,c_sp);
figure, imshow(uint8(c_sp_a5))
title('Image with 10% of pixels corrupted by binary noise filtered
by 5*5 averaging filter')

Jorma Kekalainen

Digital Image Processing

807

Effect of low pass filtering


Given that pixels corrupted by salt
and pepper noise are high
frequency components of an image,
we should expect a low-pass filter
should reduce them.
So we might try filtering with an
average filter:
a3=fspecial('average');
c_sp_a3=filter2(a3,c_sp);
The result is not noticeably better
than the noisy image.
The noise is not so much removed
as smeared over the image.
Note: Impulse noise = binary noise = salt and pepper
Kekalainen
Digital Image Processing
noiseJorma
= shot
noise.

Lecture weeks 15 and 16

808

Page 12

Lecture notes

Digital Image Processing by Jorma Kekalainen

Effect of low pass filtering

The effect is even more pronounced if


we use a larger averaging filter:
>> a7=fspecial('average',[7,7]);
>> c_sp_a7=filter2(a7,c_sp);
>> a11=fspecial('average',[11,11]);
>> c_sp_a11=filter2(a11,c_sp);
(5*5) averaging

Jorma Kekalainen

Digital Image Processing

809

Example

a7=fspecial('average',[7,7]);
c_sp_a7=filter2(a7,c_sp);
figure, imshow(uint8(c_sp_a7))
title('Image with 10% of pixels corrupted by binary noise
filtered by 7*7 averaging filter')
a11=fspecial('average',[11,11]);
c_sp_a11=filter2(a11,c_sp);
figure, imshow(uint8(c_sp_a11))
title('Image with 10% of pixels corrupted by binary noise
filtered by 11*11 averaging filter')

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

810

Page 13

Lecture notes

Digital Image Processing by Jorma Kekalainen

Median filtering
Median filtering seems almost tailor-made for
removal of salt and pepper noise.
Recall that the median of a set is the middle
value when they are sorted.
If there are an even number of values, the
median is the mean of the middle two.
A median filter is an example of a non-linear
spatial filter.
Jorma Kekalainen

Digital Image Processing

811

Example
Using a 3*3 mask, the output value is the median of
the values in the mask.

We see that very large or very small values - noisy


values - will end up at the top or bottom of the
sorted list.
Thus the median will in general replace a noisy value
with one closer to its surroundings.
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

812

Page 14

Lecture notes

Digital Image Processing by Jorma Kekalainen

Median filtering

In Matlab, median filtering is


implemented by the medfilt2 function:
>> c_sp_m3=medfilt2(c_sp);
The result is a vast improvement on using
averaging filters.

Jorma Kekalainen

Cleaning salt and pepper


noise with a median filter

Digital Image Processing

813

Example

% Cleaning binary (salt & pepper noise) noise with median filter
C=imread('cameraman.tif');
figure,imshow(C)
title('Clean image')
Csp=imnoise(C,'salt & pepper');%The amount of noise added to 10%
figure, imshow(Csp)
title('Image with 10% of its pixels corrupted by binary noise')
Cspm3=medfilt2(Csp);% class: uint8
figure, imshow(Cspm3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 10% of its pixels corrupted by binary noise')

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

814

Page 15

Lecture notes

Digital Image Processing by Jorma Kekalainen

Using median filter on more noise


This would produce an

If we corrupt more pixels with noise:


image with 20% of its
>> c_sp2=imnoise(c,'salt & pepper',0.2); pixels corrupted by noise.
then medfilt2 still does a remarkably good job.
20% salt & pepper noise

Jorma Kekalainen

After (3*3) median filtering

Digital Image Processing

815

Example
% Cleaning binary (salt & pepper noise) noise with median filter
% 20% of pixel corrupted by noise
Csp2=imnoise(C,'salt & pepper',0.2);% 20% of pixel corrupted by
noise
figure, imshow(Csp2)
title('Image with 20% of its pixels corrupted by binary noise')
Cspm3=medfilt2(Csp);% class: uint8
figure, imshow(Cspm3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

816

Page 16

Lecture notes

Digital Image Processing by Jorma Kekalainen

Try to remove noise completely


To remove noise completely, we can either try a double
application of the 3*3 median filter, or try a 5*5 median filter
on the original noisy image:
medfilt2 takes an optional
parameter a 2 element vector
>> c_sp2_m5=medfilt2(c_sp2,[5,5]);
Using medfilt2 twice

Jorma Kekalainen

giving the size of the mask to


be used.
Using a (5*5) median filter

Digital Image Processing

817

Example

% Using 3*3 median filter twice


Cspm3twice=medfilt2(Cspm3);
figure, imshow(Cspm3twice)
title('Image filtered twice by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')

% Using 5*5 median filter


Csp2m5=medfilt2(Csp2,[5,5]);
figure, imshow(Csp2m5)
title('Image filtered by 5*5 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

818

Page 17

Lecture notes

Digital Image Processing by Jorma Kekalainen

Digital Image Processing


Cleaning Gaussian Noise
with Image Averaging and
Wiener Filtering

Image averaging
It may sometimes happen that instead of just one
image corrupted with Gaussian noise, we have
many different copies of it.
An example is satellite imaging; if a satellite
passes over the same spot many times, we will
obtain many different images of the same place.
Another example is in microscopy: we might take
many different images of the same object.
In such a case a very simple approach to cleaning
Gaussian noise is to simply take the average - the
mean - of all the images.
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

820

Page 18

Lecture notes

Digital Image Processing by Jorma Kekalainen

Image averaging
To see why this works, suppose we have n copies of our
image, each with noise; then the ith noisy image will be:
M+Ni
where M is the matrix of original values, and Ni is a matrix of
normally distributed random values with mean 0.
We can find the mean Mmean = M

M av

1 n
1 n
1 n
= (M + N i ) = M + N i
n i =1
n i =1
n i =1
1 n
= M + Ni
n i =1

Image Processing
M av n
MDigital
mean = M

Jorma Kekalainen

821

Image averaging
Since Ni is normally distributed with mean 0, it
can be readily shown that the average of all
the Ni s will be close to zero; actually the
greater the number of Ni s the closer to zero
the average is .
Thus Mav Mmean = M and the approximation
is closer for larger number of images.

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

822

Page 19

Lecture notes

Digital Image Processing by Jorma Kekalainen

Example
We can demonstrate this with the familiar cameraman image.
We first need to create different versions with Gaussian noise,
and then take the average of them.
We shall create 10 versions.
One way is to create an empty three-dimensional array of
depth 10, and fill each level with a noisy image:
>> s=size(c);
>> c_ga10=zeros(s(1),s(2),10);
>> for i=1:10 c_ga10(:,:,i)=imnoise(c,'gaussian'); end

Jorma Kekalainen

Digital Image Processing

823

Example

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

824

Page 20

Lecture notes

Digital Image Processing by Jorma Kekalainen

Comment
Note here that the gaussian option of imnoise
calls the random number generator randn,
which creates normally distributed random
numbers.
Each time randn is called, it creates a different
sequence of numbers.
So we may be sure that all levels in our threedimensional array do indeed contain different
images.
Jorma Kekalainen

Digital Image Processing

825

Image averaging
Now we can take the average:
c_ga10_av=mean(c_ga10,3);
The optional parameter 3 here indicates that we
are taking the mean along the third dimension of
our array.
The averaged image is not quite clear, but we
obtained a vast improvement on the noisy
image.
An even better result is obtained by taking the
average of 100 images; this can be done by
replacing 10 with 100 in the commands above.
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

826

Page 21

Lecture notes

Digital Image Processing by Jorma Kekalainen

Image averaging to remove


Gaussian noise
10 images

100 images

Jorma Kekalainen
Image Processing
Note:
This method only worksDigital
if the
Gaussian noise has zero mean.827

Example

%Cleaning Gaussian noise by averaging


images
%Gaussian noise
C=imread('cameraman.tif');
Cga=imnoise(C,'gaussian');
% Default mean=0.0 and variance=0.01
figure, imshow(Cga)
title('Image with additive Gaussian noise
with m=0,v=0.01')
% Create 10 image versions
s=size(C);
Cga10=zeros(s(1),s(2),10);
for i=1:10 Cga10(:,:,i)=imnoise(C,'gaussian');
end
%
Cga10av=mean(Cga10,3);
figure, imshow(Cga10av/255)
title('10 times average image')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')

Jorma Kekalainen

Lecture weeks 15 and 16

%100 times averaging


Cga100=zeros(s(1),s(2),100);
for i=1:100
Cga100(:,:,i)=imnoise(C,'gaussian'); end
Cga100av=mean(Cga100,3);
figure, imshow(Cga100av/255)
title('100 times average image')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')

Digital Image Processing

828

Page 22

Lecture notes

Digital Image Processing by Jorma Kekalainen

Average filtering
If the Gaussian noise has mean 0, then we would
expect that an average filter would average the
noise to 0.
The larger the size of the filter mask, the closer to
zero.
Unfortunately, averaging tends to blur an image,
as we have seen previously.
However, if we are prepared to trade off blurring
for noise reduction, then we can reduce noise
significantly by this method.
Jorma Kekalainen

Digital Image Processing

829

Example
Suppose we take the (3*3) and (5*5)
averaging filters, and apply them to the noisy
image c_ga.
>> a3=fspecial('average');
>> a5=fspecial('average',[5,5]);
>> cg3=filter2(a3,c_ga);
>> cg5=filter2(a5,c_ga);

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

830

Page 23

Lecture notes

Digital Image Processing by Jorma Kekalainen

Averaging filtering to remove


Gaussian noise
The results are not really particularly pleasing;
although there has been some noise
reduction, the smeary nature of the resulting
images is unattractive.
Image with additive Gaussian
noise with m=0,v=0.01

(3*3) averaging

Jorma Kekalainen

(5*5) averaging

Digital Image Processing

831

Least squares filtering


Given a degraded image M of some original image M and a
restored version R, what measure can we use to say whether
our restoration has done a good job?
Clearly we would like R to be as close as possible to the
correct image M.
One way of measuring the closeness of R to M is by adding
the squares of all differences:

(m

i, j

ri , j )

i, j

where the sum is taken over all pixels of R and M (which we


assume to be of the same size).
This sum can be taken as a measure of the closeness of R to
M.
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

832

Page 24

Lecture notes

Digital Image Processing by Jorma Kekalainen

Wiener filters
If we can minimize this value, we may be sure
that our procedure has done as good a job as
possible.
Filters which operate on this principle of least
squares are called Wiener filters.
They come in many guises; we shall look at
the particular filter which is designed to
reduce Gaussian noise.
Jorma Kekalainen

Digital Image Processing

833

Why Wiener filter?


The Wiener filter is optimal in the sense
that it minimizes the mean square error
between the undistorted image g(x,y)
and the restored image (x,y).
i.e. it minimizes e2=E[|g(x,y)- (x,y)|2]

Jorma Kekalainen
Image Processing
Note:
MMSE(minimum mean Digital
square
error) filtering

Lecture weeks 15 and 16

834

Page 25

Lecture notes

Digital Image Processing by Jorma Kekalainen

Wiener filter reducing Gaussian


noise
This filter is a non-linear spatial filter; we move a mask across
the noisy image, pixel by pixel, and as we move, we create an
output image the gray values of whose pixels are based on the
values under the mask.
Since we are dealing with additive noise, our noisy image M
can be written as M=M+N, where M is the original correct
image, and N is the noise; which we assume to be normally
distributed with mean 0.
However, within our mask, the mean may not be zero;
suppose the mean is mf , and the variance in the mask is f2 .
Suppose also that the variance of the noise over the entire
image is known to be g2 .
Jorma Kekalainen

Digital Image Processing

835

Wiener filter reducing Gaussian


noise
Then the output value can be calculated as

where g is the current value of the pixel in the noisy


image.
In practice, we calculate mf by simply taking the
mean of all gray values under the mask, and f2 by
calculating the variance of all gray values under the
mask.
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

836

Page 26

Lecture notes

Digital Image Processing by Jorma Kekalainen

Wiener filter reducing Gaussian


noise
We may not necessarily know the value g2 (the
variance of the noise over the entire image) , so the
Matlab function wiener2 which implements Wiener
filtering uses a slight variant of the above equation:

where n is the computed noise variance, and is


calculated by taking the mean of all values of f2 (the
variance in the mask) over the entire image.
Jorma Kekalainen

Digital Image Processing

837

Example
Suppose we take the noisy image with additive Gaussian noise
with m=0,v=0.01 shown below, and attempt to clean this
image with Wiener filtering.
We will use the wiener2 function, which can take an optional
parameter indicating the size of the mask to be used.
The default size is (3*3).
We shall create four images:
>> c1=wiener2(c_ga);
>> c2=wiener2(c_ga,[5,5]);
>> c3=wiener2(c_ga,[7,7]);
>> c4=wiener2(c_ga,[9,9]);
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

838

Page 27

Lecture notes

Digital Image Processing by Jorma Kekalainen

Wiener filtering to remove Gaussian noise


(3*3) filtering

(5*5) filtering

(9*9) filtering

(7*7) filtering

Jorma Kekalainen

Digital Image Processing

839

Example

%Wiener filtering
C=imread('cameraman.tif');
figure,imshow(C)
title('Clean image')
Cga=imnoise(C,'gaussian'); %Default
%mean=0.0 and variance=0.01
figure, imshow(Cga)
title('Image with additive Gaussian noise
with m=0,v=0.01')
%
Cgaw3=wiener2(Cga);
Cgaw5=wiener2(Cga,[5,5]);
Cgaw7=wiener2(Cga,[7,7]);
Cgaw9=wiener2(Cga,[9,9]);

Jorma Kekalainen

Lecture weeks 15 and 16

figure, imshow(Cgaw3)
title('Image filtered by 3*3 Wiener filter')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')
figure, imshow(Cgaw5)
title('Image filtered by 5*5 Wiener filter')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')
figure, imshow(Cgaw7)
title('Image filtered by 7*7 Wiener filter')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')
figure, imshow(Cgaw9)
title('Image filtered by 9*9 Wiener filter')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')

Digital Image Processing

840

Page 28

Lecture notes

Digital Image Processing by Jorma Kekalainen

Comment
Being a low pass filter, Wiener filtering tends to blur edges
and high frequency components of the image.
But it does a far better job than using a low pass blurring filter.
We can achieve very good results for noise where the
variance is not as high as that in our current image (variance
0.01).

Jorma Kekalainen

Digital Image Processing

841

Example

% Using smaller gaussian noise


(m=0,v=0.005)
Cga2=imnoise(C,'gaussian',0,0.005);
figure, imshow(Cga2)
title('Image with additive Gaussian
noise with m=0,v=0.005')
Cga2w=wiener2(Cga2,[7,7]);
figure,imshow(Cga2w)
title('Image filtered by 7*7 Wiener
filter')
xlabel('Image with additive Gaussian
noise with m=0,v=0.005')

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

842

Page 29

Lecture notes

Digital Image Processing by Jorma Kekalainen

The image and its appearance


after Wiener filtering
The result is a clear improvement over the
original noisy image.

Jorma Kekalainen

Digital Image Processing

843

Example: whos command


>> whos
Name

Size

C
256x256
Cga
256x256
Cga10
256x256x10
Cga100
256x256x100
Cga100av
256x256
Cga10av
256x256
Cga2
256x256
Cga2w
256x256
Cga3
256x256
Cga5
256x256
Cgaw3
256x256
Cgaw5
256x256
Cgaw7
256x256
Cgaw9
256x256
Csp
256x256
Csp2
256x256

Jorma Kekalainen

Lecture weeks 15 and 16

Bytes Class

Attributes

65536 uint8
65536 uint8
5242880 double
52428800 double
524288 double
524288 double
65536 uint8
65536 uint8
524288 double
524288 double
65536 uint8
65536 uint8
65536 uint8
65536 uint8
65536 uint8
65536 uint8

Csp2m5
256x256
65536 uint8
Cspm3
256x256
65536 uint8
Cspm3twice 256x256
65536 uint8
a11
11x11
968 double
a3
3x3
72 double
a5
5x5
200 double
a7
7x7
392 double
ans
1x2
16 double
c
256x256
65536 uint8
c_pn
256x256
524288 double
c_sp
256x256
65536 uint8
c_sp_a11
256x256
524288 double
c_sp_a3
256x256
524288 double
c_sp_a5
256x256
524288 double
c_sp_a7
256x256
524288 double
cp
256x256
524288 double
i
1x1
8 double
pn
256x256
524288 double
s
1x2
16 double
x
256x256
524288 double
y
256x256
524288 double

Digital Image Processing

844

Page 30

Lecture notes

Digital Image Processing by Jorma Kekalainen

Exercise
The arrays below represent small grayscale
images. Compute the 4*4 image that would
result in each case if the middle 16 pixels were
transformed using a 3*3 median filter:

Using the previous images transform them by


using a 3*3 averaging filter.
Jorma Kekalainen

Digital Image Processing

845

Exercise
Produce a gray subimage of the color image flowers.tif by
>> f=imread('flowers.tif');
>> fg=rgb2gray(f);
>> f=im2uint8(f(30:285,60:315));
(a) Add 10% salt & pepper noise to the image. Attempt to
remove the noise with (3*3) average filtering and (3*3)
median filtering.
Which method gives the best results?
(b) Repeat the above question but with 20% noise and (3*3)
median filtering only.
(c) Compare the previous result with a double (3*3) median
filtering and (5*5) median filter.
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

846

Page 31

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note: Im2uint8 convert image to 8-bit unsigned integers.

Solution
>> f=imread('flowers.tif');
fg=rgb2gray(f);
f=im2uint8(f(30:285,60:315));
figure,imshow(f)
title('f=im2uint8(f(30:285,60:315))')

Jorma Kekalainen

>> fsp=imnoise(f,'salt & pepper');%The amount


of noise added defaults to 10%
figure, imshow(fsp)
title('Image with 10% of its pixels corrupted by
binary noise')

Digital Image Processing

847

Solution (a)
>> a3=fspecial('average');% class double
fspa3=filter2(a3,fsp);% class: double
figure, imshow(uint8(fspa3);% or (fspa3/255)
title('Image filtered by 3*3 averaging filter')
xlabel('Image with 10% of its pixels corrupted
by binary noise')

Jorma Kekalainen

Lecture weeks 15 and 16

>> fspm3=medfilt2(fsp);% class: uint8


figure, imshow(fspm3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 10% of its pixels corrupted
by binary noise')

Digital Image Processing

848

Page 32

Lecture notes

Digital Image Processing by Jorma Kekalainen

>> % Cleaning binary (salt & pepper noise)


noise with median filter
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of
pixel corrupted by noise
fsp20m3=medfilt2(fsp20);% class: uint8
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
binary noise')
xlabel('Image with 20% of its pixels corrupted
by binary noise')

(b)

Jorma Kekalainen

Digital Image Processing

849

(c)% Using 5*5 median filter

% Using 3*3 median filter twice


fsp20m3twice=medfilt2(fsp20m3);
figure, imshow(fsp20m3twice)
title('Image filtered twice by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted
by binary noise')

Jorma Kekalainen

Lecture weeks 15 and 16

fsp20m5=medfilt2(fsp20,[5,5]);
figure, imshow(fsp20m5)
title('Image filtered by 5*5 median filter')
xlabel('Image with 20% of its pixels corrupted
by binary noise')

Digital Image Processing

850

Page 33

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
f=imread('flowers.tif');
figure,imshow(f)
title('Original image ''flowers.tif''')
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of pixel corrupted by noise
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by binary noise')
fsp20m3r=medfilt2(fsp20(:,:,1));% Red component
fsp20m3g=medfilt2(fsp20(:,:,2));
fsp20m3b=medfilt2(fsp20(:,:,1));
fsp20m3=cat(3,fsp20m3r, fsp20m3g, fsp20m3b);% all components back into a
single 3D array for use with imshow
figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
Jorma Kekalainen
Digital Image Processing
851
xlabel('Image with 20% of its pixels
corrupted by binary noise')

Note
f=imread('flowers.tif');
figure,imshow(f)
title('Original image ''flowers.tif''')
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of pixel corrupted by noise
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by binary noise')
a3=fspecial('average');
fsp20a3r=filter2(a3,fsp20(:,:,1));%Red component
fsp20a3g=filter2(a3,fsp20(:,:,2));
fsp20a3b=filter2(a3,fsp20(:,:,3));
fsp20a3=cat(3,fsp20a3r, fsp20a3g, fsp20a3b);% all components back into a
single 3D array for use with imshow
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

852

Continued on the following page

Page 34

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note
figure,imshow(uint8(fsp20a3))
title('Image filtered by 3*3 average filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
%
fsp20m3r=medfilt2(fsp20(:,:,1));% Red component
fsp20m3g=medfilt2(fsp20(:,:,2));
fsp20m3b=medfilt2(fsp20(:,:,1));
fsp20m3=cat(3,fsp20m3r, fsp20m3g, fsp20m3b);% all
components back into a single 3D array for use with imshow
figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
Jorma Kekalainen

Digital Image Processing

853

Note

Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

854

Page 35

Lecture notes

Digital Image Processing by Jorma Kekalainen

Note

Jorma Kekalainen

Digital Image Processing

855

Exercise
Add Gaussian noise to the grayscale flowers image
with the following parameters:
(a) mean 0, variance 0.01 (default)
(b) mean 0, variance 0.02
(c) mean 0, variance 0.05
(d) mean 0, variance 0.1
In each case, attempt to remove the noise with
average filtering and with Wiener filtering.
Can you produce satisfactory results with the last
two noisy images?
Jorma Kekalainen

Lecture weeks 15 and 16

Digital Image Processing

856

Page 36

You might also like