Professional Documents
Culture Documents
Page 1
Lecture notes
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
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.
Page 2
Lecture notes
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
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
790
Page 3
Lecture notes
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
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
792
Page 4
Lecture notes
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
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
794
Page 5
Lecture notes
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
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
796
Page 6
Lecture notes
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
797
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.
Page 7
Lecture notes
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
799
Periodic noise
Jorma Kekalainen
800
Page 8
Lecture notes
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
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
802
Page 9
Lecture notes
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')
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
804
Page 10
Lecture notes
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
805
Page 11
Lecture notes
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
807
808
Page 12
Lecture notes
Jorma Kekalainen
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
810
Page 13
Lecture notes
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
811
Example
Using a 3*3 mask, the output value is the median of
the values in the mask.
812
Page 14
Lecture notes
Median filtering
Jorma Kekalainen
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
814
Page 15
Lecture notes
Jorma Kekalainen
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
816
Page 16
Lecture notes
Jorma Kekalainen
817
Example
Jorma Kekalainen
818
Page 17
Lecture notes
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
820
Page 18
Lecture notes
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
822
Page 19
Lecture notes
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
823
Example
Jorma Kekalainen
824
Page 20
Lecture notes
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
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
826
Page 21
Lecture notes
100 images
Jorma Kekalainen
Image Processing
Note:
This method only worksDigital
if the
Gaussian noise has zero mean.827
Example
Jorma Kekalainen
828
Page 22
Lecture notes
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
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
830
Page 23
Lecture notes
(3*3) averaging
Jorma Kekalainen
(5*5) averaging
831
(m
i, j
ri , j )
i, j
832
Page 24
Lecture notes
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
833
Jorma Kekalainen
Image Processing
Note:
MMSE(minimum mean Digital
square
error) filtering
834
Page 25
Lecture notes
835
836
Page 26
Lecture notes
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
838
Page 27
Lecture notes
(5*5) filtering
(9*9) filtering
(7*7) filtering
Jorma Kekalainen
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
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')
840
Page 28
Lecture notes
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
841
Example
Jorma Kekalainen
842
Page 29
Lecture notes
Jorma Kekalainen
843
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
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
844
Page 30
Lecture notes
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:
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
846
Page 31
Lecture notes
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
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
848
Page 32
Lecture notes
(b)
Jorma Kekalainen
849
Jorma Kekalainen
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')
850
Page 33
Lecture notes
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
852
Page 34
Lecture notes
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
853
Note
Jorma Kekalainen
854
Page 35
Lecture notes
Note
Jorma Kekalainen
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
856
Page 36