Digital Image Processing
Digital Image Processing
Gerard Blanchet
Maurice Charbit
This page intentionally left blank
Digital Signal and Image Processing using MATLAB
This page intentionally left blank
Digital Signal and Image
Processing using MATLAB
Gerard Blanchet
Maurice Charbit
Part of this book adapted from "Signaux et images sous Matlab : méthodes, applications et
exercices corriges" published in France by Hermes Science Publications in 2001
First published in Great Britain and the United States in 2006 by ISTE Ltd
Translated by Antoine Hervier
Apart from any fair dealing for the purposes of research or private study, or criticism or
review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may
only be reproduced, stored or transmitted, in any form or by any means, with the prior
permission in writing of the publishers, or in the case of reprographic reproduction in
accordance with the terms and licenses issued by the CLA. Enquiries concerning reproduction
outside these terms should be sent to the publishers at the undermentioned address:
ISTE Ltd ISTE USA
6 Fitzroy Square 4308 Patrice Road
London WIT 5DX Newport Beach, CA 92663
UK USA
www.iste.co.uk
The rights of Gerard Blanchet and Maurice Charbit to be identified as the authors of this work
have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988.
Blanchet, Gerard.
[Signaux et images sous Matlab. English]
Digital signal and image processing using Matlab / Gerard Blanchet, Maurice Charbit.
p. cm.
Translation of: Signaux et images sous Matlab.
Includes index.
ISBN-13: 978-1-905209-13-2
ISBN-10: 1-905209-13-4
1. Signal processing—Digital techniques—Data processing. 2. MATLAB. I.Charbit, Maurice.
II. Title.
TK5102.9.B545 2006
621.382'2-dc22
2006012690
Printed and bound in Great Britain by Antony Rowe Ltd, Chippenham, Wiltshire.
MATLAB is a trademark of The MathWorks, Inc. and is used with per-
mission. The Math Works does not warrant the accuracy of the text or exer-
cises in this book. This book's use or discussion of MATLAB software does
not constitute endorsement or sponsorship by The Math Works of a particular
pedagogical approach or use of the MATLAB software.
This page intentionally left blank
Contents
Preface 15
Introduction to MATLAB 23
1 Variables 24
1.1 Vectors and matrices 24
1.2 Arrays 26
1.3 Cells and structures 27
2 Operations and functions 29
2.1 Matrix operations 29
2.2 Pointwise operations 30
2.3 Constants and initialization 31
2.4 Predefined matrices 31
2.5 Mathematical functions 32
2.6 Matrix functions 34
2.7 Other useful functions 34
2.8 Logical operators on boolean variables 35
2.9 Program loops 35
3 Graphically displaying results 36
4 Converting numbers to character strings 39
5 Input/output 39
6 Program writing 40
Bibliography 739
Index 747
This page intentionally left blank
Preface
Basic concepts of D S P
The recent technical evolutions, along with their successions of technological
feats and price drops have allowed systems based on micro-controllers and
microprocessors to dominate the field of signal and image processing, at the
expense of analog processing. Reduced to its simplest form, signal processing
amounts to manipulating d a t a gathered by sampling analog signals. Digital
16 Digital Signal and Image Processing using MATLAB
Signal and Image Processing, or DSIP, can therefore be defined as the art of
working with sequences of numbers.
The sampling theorem is usually the first element found in a DSIP course, be-
cause it justifies the operation by which a continuous time signal is replaced
by a discrete sequence of values. It states t h a t a signal can be perfectly recon-
structed from the sequence of its samples if the sampling frequency is greater
t h a n a fundamental limit called the Nyquist frequency. If this is not the case,
it results in an undesired effect called spectrum aliasing.
F i l t e r i n g a n d E l e m e n t s of F i l t e r D e s i g n
Linear filtering was originally used to extract relevant signals from noise. The
basic tools will be introduced: the discrete convolution, the impulse response,
Preface 17
the frequency response, the z-transform. We will then focus on the fundamen-
tal relation between linear filtering with rational transfer functions and linear
constant-coefficient recursive equations.
Filter design is described based on a few detailed examples, particularly the
window method and the bilinear transform. The concepts of over-sampling and
under-sampling are then introduced, some applications of which are frequency
change and the reduction of quantization noise. From a broader perspective,
multi-rate processing and filter banks which are described here, are two subjects
t h a t attract a lot of attention in the field of DSIP.
An introduction to images
Image processing is described in its own separate chapter. Many of the concepts
used in signal processing are also used in image processing. The only difference
is t h a t two indices are used instead of one. However images have particular
characteristics t h a t require specific processing: erosion, expansion, etc. The
computation time is usually much longer for images t h a n it is for signals. It is
nevertheless possible to conduct image processing with MATLAB or one of
its clones. This theme will be discussed using examples on 2D filtering, contour
detection, and other types of processing in cases where the 2D nature of the
images does not make t h e m too different from a I D signal. This chapter will
also be the opportunity to discuss image compression and entropie coding.
Random Processes
Up until now, the signals used as observation models have been described by
functions t h a t depend on a finite number of well known parameters and on
simple known basic functions: the sine function, the unit step function, the
impulse function... This type of signal is said to be deterministic.
There are other situations where deterministic functions cannot provide us
with a relevant apprehension of the variability of the phenomena. Signals must
then be described by characteristics of a probabilistic nature. This requires
the use of random processes, which are time-indexed sequences of random vari-
ables. Wide sense stationary processes, or WSSP, are an important category
of random processes. The study of these processes is mainly based on the es-
sential concept of power spectral density, or P S D . The PSD is the analog for
W S S P of the square module of the Fourier transform for deterministic signals.
The formulas for the linear filtering of W S S P are then laid down. Thus, we
infer t h a t WSSPs can also be described as the linear filtering of a white noise.
This result leads to a large class of stationary processes: the AR process, the
MA process, and the A R M A process.
18 Digital Signal and Image Processing using MATLAB
Spectral Estimation
One of the main problems DSIP is concerned with is evaluating the PSD of
WSSPs. In the case of continuous spectra, it can be solved by using non-
parametric approaches (smooth periodograms, average periodograms, etc.) or
parametric methods based on linear models (AR, MA, A R M A ) . As for line
spectra, the most commonly used methods are the periodogram and what are
called high resolution methods, which use the structures of the signal and the
noise: Prony, Pisarenko, MUSIC, ESPRIT, etc.
T h e least squares
This chapter discusses the use of the least squares method for solving problems.
This method is used in a number of problems, in fields such as spectral analysis,
modelling, linear prediction, communications... We will discuss such methods
as Wiener, RLS, LMS, K a i m a n . . .
Applications
This last chapter presents case studies t h a t go a little further in depth t h a n the
examples described earlier. The emphasis is set on audio signal processing, on
compression as well restoring and denoising for speech and music, and on mod-
ulation, demodulation and equalization issues for digital communications. This
chapter is also an opportunity to discover typical approaches and algorithms:
pitch detection, PSOLA, D T W , ACP, LBG, V i t e r b i . . .
As a Conclusion
One of the issues raised by many of those who use signal processing has to
do with the artificial aspect introduced by simulation. For example, we use
sampling frequencies equal to 1, and therefore frequencies with no dimension.
There is a risk t h a t the student may lose touch with the physical aspect of
the phenomena and, because of t h a t , fail to acquire the intuition of these
phenomena. T h a t is why we have tried, at least in the first chapters, to give
exercises t h a t used values with physical units: seconds, Hz, etc.
This work discusses important properties and theorems, but its objective
is not to be a book on mathematics. Its only claim, and certainly an excessive
one, is to show how interesting signal and image processing can be, by providing
themes of study we chose because they were good examples, because they were
simple, while trying not to be too trivial.
All of the subjects discussed far from cover the extent of knowledge required
in this field. However they seem to us to be a solid foundation for an engineer
who would happen to deal with DSIP problems.
Notations and Abbreviations
0 E m p t y Set
Í
Dirac Distribution when í £ R
S(t)
Kronecker Symbol when Í G Z
Re(z) Real P a r t of z
Im(z) Imaginary Part of z
i or j = v—1
x(t) ^± X(f) Fourier Transform
(x*y)(t) Continuous Time Convolution
= / x(u)y(t — u)du
JR
(x*y)(t) Discrete Time Convolution
= Σ x{u)y{t - w)
uez
20 Digital Signal and Image Processing using MATLAB
P{X e A) Probability t h a t X E A
Έ{Χ} Expectation Value of X
Xc = X -Έ{Χ} Zero-mean R a n d o m Variable
2
var(X)=E{|Xc|} Variance of X
Έ{Χ\Υ} Conditional Expectation of X given Y
In this book the name MATLAB} ® (short for Matrix Laboratory) will refer to:
■WWH.lJi.HUILI.LII
File Edit Options Windows Help
►a=2*logl0(5)
■ Command line
Result
Prompt
(R)
Figure 1 - The MATLAB command window on MS-Windows
This editor may either be integrated in the software or kept external (the
user's favorite editor). Program files use the extension .m. If a program is
called p r o g l . m , all the user has to do is type p r o g l in the MATLAB com-
(R)
<MATLAB>
CurrentDirectory| Workspace\ Copyright 19S4-2005 The MathWorks, Inc.
Vepsion 7.0.4.352 (R14) Service Pack 2
Command History
* - 4Í7ÍU5 4:34 PM -%
Γ** - 4J8Í05 3:29 AM -%
1-11 To get slatted, select MATLAB Help or Demos fpom the Help menu.
•»Sari |
(R)
F i g u r e 2 — The MATLAB window in an X-windows environment. The definition
of the routine folder can be done directly by clicking on the icon with ". . . " in the
top-right corner of the window. The definition of the directory path can be done by
selecting the item s e t path . . . in the menu f i l e
Clones of MATLAB are now available. Some belong to the public domain.
There also exists a compiler t h a t allows the user to translate MATLAB pro-
grams in machine language, making the execution quicker, and meaning t h a t
it is not required to own the interpreter.
1 Variables
1.1 Vectors and matrices
E x a m p l e 2 ( A s s i g n m e n t of a real m a t r i x ) Type a = [ l 2 3 ; 4 5 6] at
the MATLAB prompt in the c o m m a n d window. The answer is shown in
Figure 3.
D « Command ^ ^ ^ ^ = ^ ^ ^ ^ ^ ¡EIE
»a=| 1 2 3;4 5 6]
■
Π
a = ^~"^- Assignment of matrix a
1
1
1 2 3 -* Result
4 5 6 (2 lines, 3 columns)
F i g u r e 3 — Assigning a matrix
2 3 4 5 6 7 8 9 10
E x a m p l e 4 ( I n c r e m e n t e d i m p l i c i t e n u m e r a t i o n ) Type a = ( 0 : 4 : 1 0 ) .
MATLAB® returns:
0 4 8
26 Digital Signal and Image Processing using MATLAB
The last element of a vector is indicated by the reserved word end. In the
previous example, a ( e n d ) indicates t h a t its value is 8.
It is possible to extend the size of a matrix. The interpreter takes care of
available space by dynamically allocating memory space during the analysis of
the typed phrase.
— When defining variables and objects, the language takes into account
whether letters are capital or lowercase.
1.2 Arrays
A=[l:3;4:6]
» A
A=
1 2 3
4 5 6
» A(: , : ,2)=zeros(2,3) , '/„ or A ( : , : , 2 ) = 0
A(:,:,l) =
1 2 3
4 5 6
A(:,:,2) =
0 0 0
0 0 0
1.3 Cells a n d s t r u c t u r e s
langcell={'MATLAB', [ 6 . 5 ; 2 . 3 ] , 2 0 0 2 }
» langcell(2)
ans =
[2x1 double]
» langcell{2}
ans =
6.5000
2.3000
» langcell{2}(l)
ans =
6.5000
l a n g c e l l is made up of three elements: the first one is a character string,
the second one is a column vector, and the third one is a scalar. This
example shows the difference in syntax between an array and a cell, a left
brace ({) and a right brace (}) being used instead of a left square bracket
([) and a right square bracket (]). As for the content, l a n g c e l l ( 2 ) refers
to the vector [6 . 5000; 2 . 3] , l a n g c e l l { 2 } to the content of this vector, and
l a n g c e l l { 2 } ( l ) to the numerical value 6.5.
first field, the character string 6 . 5 to the second field, and the numerical value
2002 to the third field:
» l a n g s t r u c = s t r u c t C L a n g u a g e ' , 'MATLAB' , 'Version' , ' 6 . 5 ' , 'Year' ,2002) ;
>>langstruc.Year
ans =
2002
»
The second instruction displays the content of l a n g s t r u c . Year, which is
2002. A 1 x 1 dimension structure is organized in the same way as an n x 1
dimension array of cells, where n is the number of fields of the structure. Cells
can therefore be compared to structures with unnamed fields.
The following example defines a structure named l a n g s t r u c , comprised of
two recordings. Each recording contains all three fields Language, V e r s i o n ,
and Year to which were respectively assigned the sequences of two character
strings MATLAB and C, of the two values 6.5 and 15.1, and of the two values
2002 and 2003:
» langstruc=struct('Langage',{{'MATLAB','C'}},...
'Version' , [6.5;15.1],'Year',[2002;2003] );
>> l a n g s t r u c
langstruc =
Language: {'MATLAB' 'C>
Version: [2x1 double]
Year: [2x1 double]
>> langstruc.Langage{l}
ans =
MATLAB
>> langstruc.Language(1)
ans =
'MATLAB'
»
These objects can be handled using certain functions: isstruct,
fieldnames, s e t f i e l d , rmfield, c e l l f u n , c e l l d i s p , num2cell, cell2mat,
c e l l 2 s t r u c t , s t r u c t 2 c e l l . . . An example of a conversion is as follows:
>> c l e a r a l l
» langcell={'MATLAB',[6.5;2.3],2002}
>> chps={'Langage','Version','Year'};
>> cell2structClangceli,chps,2)
ans =
Language: 'MATLAB'
Version: 6.5000
Year: 2002
»
The 2 t h a t is part of the instruction c e l l 2 s t r u c t C l a n g c e l i , chps , 2 ) indi-
cates the dimension of l a n g c e l l t h a t needs to be taken into account to define
the number of fields. Here, for example, s i z e ( l a n g c e l l , 2 ) means t h a t the
number of fields is 3.