Digital Image Processing Using Matlab (Gonzalez)
Digital Image Processing Using Matlab (Gonzalez)
I
1 Preface xi
I Ack~zowledglnents xii
About the Authors xiii
t
Inf~oducfion 1
Previezv 1
Background 1
What Is Digital Image Processing? 2
Background on MATLAB and the Image Processing Toolbox 4
Areas of Image Processing Covered in the Book 5
The Book Web Site 6
Notation 7
The MATLAB Working Environment 7
1.7.1 The MATLAB Desk top 7
1.7.2 Using the MATLAB Editor to Create M-Files 9
1.7.3 Getting Help 9
1.7.4 Saving and Retrieving a Work Session 10
How References Are Organized in the Book 11
Summary 11
!c
5
2 Fundamentals 12
Preview 12
d
2.1 Digital Image Representation 12
i
2.1.1 Coordinate Conventions 13
2.1.2 Images as Matrices 14
'
9 . 2 Reading Images 14
2.3 Displaying Images 16
2.4 Writing Images 18
! 2.5 Data Classes 23
2.6 ImageTypes 24
j 2.6.1 Intensity Images 214
t 2.6.2 Binary Images 25
)i 2.6.3 A Note on Terminology 25
; 2.7 Converting between Data Classes and Image Types 25
: 2.7.1 Converting between Data Classes 25
I 2.7.2 Converting between Image Classes and Types 26
; 2.8 Array Indexing 30
2.8.1 VectorIndexing 30-
2.8.2 Matrix Indexing 32
I
2.8.3 Selecting Array Dimensions 37
a%l Contents iay Contents vii
2.9 Some Important Standard Arrays 37 1i 4.6 Sharpening Frequency Domain Fillers 136
4.6.1 Basic Highpass Filtering 136
2.10 Introduction to M-Function Programming 38 Y
2.10.1 M-Files 38 4.6.2 High-Frequency Emphasis Filtering 138
t Summary 140
2.10.2 Operatclrs 40
2.10.3 Flow Control 49 I, r
2.10.4 Code Optimization 55 i; Image Restoration 141
2.10.5 Interactive 1 / 0 59 f Preview 141
2.10.6 A Brief [ntroduction to Cell Arrays and Structures 62 5.1 A Model of the Image DegradationIRestoration Process 142
;
i
4.4 Obtaining Frequency Domain Filters from Spatial Filters 122 'r 6.2.2 The YCbCr Color Space 205
4..5 Generating Filters Directly in the Frequency Domain 127
it 6.2.3 The HSV Color Space 205
4.5.1 Creating Meshgrid Arrays for Use in Implementing Filters 6.2.4 The CMY and CMYK Color Spaces 206
in the Izrequency Domain 128 1 6.2.5 The HSI Color Space 207
4.5.2 Lowpass Frequency Domain Filters 129 6.3 The Basics of Color Image Processing 215
4.5.3 Wireframe and Surface Plotting 132 ' 6.4 Color Transformations 216
.3 Contents P Contents ix
6.5 Spatial Filtering of Color Images 227 Combining Dilation and Erosion 347
6.5.1 Color Image Smoothing 227 9.3.1 Opening and Closing 347
6.5.2 Color Image Sharpening 230 9.3.2 The Hit-or-Miss Transformation 350
6.6 Working Directly in RGB Vector Space 231 9.3.3 Using Lookup Tables 353
6.6.1 Color Edge Detection Using the Gradient 232 9.3.4 Functionbwrnorph 356
6.6.2 Image Segmentation in RGB Vector Space 237 Labeling Connected Components 359
Summary 241 Morphological Reconstruction 362
7 9.5.1 Opening by Reconstruction 363
/ Wavelets 242 9.5.2 Filling Holes 365
9.5.3 Clearing Border Objects 366
Preview 242
Gray-Scale Morphology 366
7.1 Background 242
9.6.1 Dilation and Erosion 366
7.2 The Fast Wavelet Transform 245
7.2.1 FWTs Using the Wavelet Toolbox 246 9.6.2 Opening and Closing 369
7.2.2 FWTs without the Wavelet Toolbox 252 9.6.3 Reconstruction 374
7.3 Working with Wavelet Decomposition Structures 259 Summary 377
7.3.1 Editing Wavelet Decomposition Coefficients without
the Wavelet Toolbox 262
7.3.2 Displaying Wavelet Decomposition Coefficients 266
10 Image Segmentation
Preview 378
378
7.4 The Inverse Fast Wavelet Transform 271 10.1 Point, Line, and Edge Detection 379
7.5 Wavelets in Image Processing 276 10.1.1 Point Detection 379
Summary 281 10.1.2 Line Detection 381
12 Object Recognition
gramming, all at a level typical of that found in a junior/senior curriculum in a techni-
484 cal discipline. Rudimentary knowledge of MATLAB also is desirable.
To achieve these objectives, we felt that two key ingredients were needed. The
Preview 484
first was to select image processing material that is representative of material cov-
12.1 Background 484
ered in a formal course of instruction in this field. The second was to select soft-
12..2 Computing Distance Measures i n MATLAB 485
ware tools that are well supported and documented, and which have a wide range
12..3 Recognition Based o n Decision-Theoretic Methods 488
of applications in the "real" world.
12.3.1 Forming Pattern Vectors 488 To meet the first objective,most of the theoretical concepts in the following chapters
12.3.2 Pattern, Matching Using Minimum-Distance Classifiers 489 were selected from Digital Image Processing by Gonzalez and Woods, which has been
12.3.3 Matching b y Correlation 490 the choice introductory textbook used by educators all over the world for over two
12.3.4 Optimum Statistical Classifiers 492 decades.'Ihe software tools selected are from the MATLAB Image Processing Toolbox
12.3.5 Adaptive Learning Systems 498 (R), which similarly occupies a position of eminence in both education and industrial
12Y.4 Structural Recognition 498 app1ications.A basic strategy followed in the preparation of the book was to provide a
12.4.1 Working with Strings i n MATLAB 499 seamless integration of well-established theoretical concepts and their implementation
12.4.2 String Matching 508 using state-of-the-art software tools.
Summary 513 The book is organized along the same lines as Digital Image Processing. In this way,
the reader has easy access to a more detailed treatment of all the image processing
A
Appendix Function Summary 514 concepts discussed here, as well as an up-to-date set of references for further reading.
Following this approach made it possible to present theoretical material in a succinct
C
Appendix M- unctions 552
under most operating systems in use t0day.A unique feature of this book is its empha-
sis on showing how to develop new code to enhance existing MATLAB and IPT func-
tionality. This is an important feature in an area such as image processing, which, as
noted earlier, is characterized by the need for extensive algorithm decreloprnent and
Bibliography 594 experimental work.
Index 597 After an introduction to the fundamentals of MATLAB functions and program-
ming, the book proceeds to address the mainstream areas of image processing. The
a Preface
major areas covered include intensity transformations, linear and nonlinear spatial fil-
About the Authors
tering, filtering in the frequency domain, image restoration and registration, color
image processing, wavelets image data compression, morphological image processing, Rafael C. Gonzalez
image segmentation, region and boundary representation and description, and object R. C. Gonzalez received the B.S.E.E. degree from the University of Miami in 1965
recognition. This material is complemented by numerous illustrations of how to solve and the M.E. and Ph.D. degrees in electrical engineering from the University of
image processing problems using MATLAB and IPT functions.In cases where a func- Florida. Gainesville, in 1967 and 1970, respectively. He joined the Electrical and
tion did not exist, a new function was written and documented as part of the instruc- Computer Engineering Department at the University of Tennessee, Knoxville
tional focus of the book. Over 60 new functions are included in the following chapters. (UTK) in 1970, where he became Associate Professor in 1973, Professor in 1978.
These functions increase the scope of IPT by approximately 35 percent and also serve and Distinguished Service Professor in 1984. He served as Chairman of the de-
the important purpose of further illustrating how to implement new image processing partment from 1994 through 1997'.He is currently a Professor Emeritus of Electri-
software solutions. cal and Computer Engineering at UTK.
The material is presented in textbook format, not as a software manual. Although He is the founder of the Imagt: & Pattern Analysis Laboratory and the Robot-
the book is self-contained, we have established a companion Web site (see Section 1.5) ics & Computer Vision Laboratory at the University of Tennessee. He also found-
designed to provide support in a number of areas. For students following a formal ed Perceptics Corporation in 1982 and was its president until 1992. The last three
course of studv or individuals embarked on a program of self study, the site contains years of this period were spent under a full-time employment contract with West-
tutorials and reviews on background material, as well as projects and image databases, inghouse Corporation, who acquired the company in 1989. Under his direction,
including all images in the book. For instructors, the site contains classroom presenta- Perce~ticsbecame highly successful in image processing, computer vision, and
tion materials that include Powerpoint slides of all the images and graphics used in the laser disk storage technologies. In its initial ten years, Perceptics introduced a se-
book. Individuals already familiar with image processing and I I T fundamentals will ries of innovative products, including: The world's first commercially-available
find the site a useful place for up-to-date references, new implementation techniques, computer vision system for autonlatically reading the license plate on moving ve-
and a host of other support material not easily found elsewhere.All purchasers of the hicles; a series of large-scale image processing and archiving systems used by the
book are eligible to download executable files of all the new functions developed in U.S. Navy at six different manufacturing sites throughout the country to inspect
the text. the rocket motors of missiles in the Trident I1 Submarine Program; the market
As is true of most writing efforts of this nature, progress continues after work on the leading family of imaging boards for advanced Macintosh computers; and a line of
manuscript stops. For this reason, we devoted significant effort to the selection of ma- trillion-byte laser disk products.
terial that we believe is Fundamental, and whose value is likely to remain applicable in He is a frequent consultant to industry and government in the areas of pattern
a rapidly evolving body of knowledge. We trust that readers of the book will benefit recognition, image processing, and machine learning. His academic honors for work
from this effort and thus find the material timely and useful in their work. in these fields include the 1977 UTK College of Engineering Faculty Achievement
Award; the 1978 UTK Chancellor's Research Scholar Award; the 1980 Magnavox En-
Acknowledgments gineering Professor Award; and the 1980 M. E. Brooks Distinguished Professor
Award. In 1981 he became an IBh4 Professor at the University of Tennessee and in
We are indebted to a number of individuals in academic circles as well as in industry 1984 he was named a Distinguished Service Professor there. He was awarded a Dis-
and government who have contributed to the preparation of the book.Their contribu- tinguished Alumnus Award by the University of Miami in 1985, the Phi Kappa Phi
tions have been important in so many different ways that we find it difficult to ac- Scholar Award in 1986, and the University of Tennessee's Nathan W. Dougherty
knowledge them in any other way but alphabetically. We wish to extend our Award for Excellence in Engineering in 1992. Honors for industrial accomplishment
appreciation to Mongi A. Abidi, Peter J. Acklam, Serge Beucher, Emesto Bribiesca, include the 1987 IEEE Outstanding Engineer Award for Commercial Development
Michael W. Davidson, Courtney Esposito, Naomi Fernandes, Thomas R. Gest, Roger in Tennessee: the 1988 Albert Rose National Award for Excellence in Commercial
Heady, Brian Johnson, Lisa Kempler, Roy Lurie, Ashley Mohamed, Joseph E. Image Processing; the 1989 B. Otto Wheeley Award for Excellence in Technology
Pascente, David. R. Pickens, Edgardo Felipe Riveron, Michael Robinson. Loren Shure, Transfer: the 1989 Coopers and Lybrand Entrepreneur of the Year Award; the 1992
Jack Sklanski, Sally Stowe, Craig Watson, and Greg Wolodkin. We also wish to ac- IEEE Region 3 Outstanding Engineer Award; and the 1993Automated Imaging As-
knowledge the organizations cited in the captions of many of the figures in the book sociation National Award for Technology Development.
for their permission to use that material. Dr. Gonzalez is author or co-author of over 100 technical articles, two edited
Special thanks go to Tom Robbins, Rose Kernan, Alice Dworkin, Xiaohong books, and five textbooks in the fields of pattern recognition, image processing,
Zhu, Bruce Kenselaar, and Jayne Conte at Prentice Hall for their commitment to and robotics. His books are used in over 500 universities and research institutions
excellence in all aspects of the production of the book.Their creativity, assistance, throughout the world. He is listed in the prestigious Marquis Who's Who in Amer-
and patience are truly appreciated. ica,Marquis Who's Who in Engineering, Marquis Who's Who in the World, and in
RAFAEL C. GONZALEZ
10 other national and international biographical citations. He is the co-holder of
two U.S. Patents. and has been an associate editor of the IEEE Transactions on
...
Xlll
J *& About the Auihors
Syster?zs, Man crnci CyDt.rnetics, and the International Jocrrnal of Computer and In-
forr??ation Sciences. He is a member of numerous professional and honorary soci-
eties, including Tau Beta Pi, Phi Kappa Phi, Eta Kappa Nu, and Sigma Xi. He is a
Fellow of the IEEE.
Richard E. Woods
Richard E. Woods earned his B.S., M.S., and Ph.D. degrees in Electrical Engineer-
ing from the University of Tennessee, Knoxville. Hi:s professional experiences
rrange from entrepreneurial to the more traditional academic, consulting, govern-
mental, and industrial pursuits. Most recently, he founded MedData Interactive, a
Ihigh technology company specializing in the development of handheld computer
systems for medical applications. He was also a founder and Vice President of Per-
ceptics Corporation, where he was responsible for the d~evelopmentof many of the
company's quantitative image analysis and autonomou!; decision making products.
Prior to Perceptics and MedData, Dr. Woods was an Assistant Professor of Elec-
trical Engineering and Computer Science at the University of Tennessee and prior to
that, a computer applications engineer at Union Carbide Corporation. As a consul-
tant, he has been involved in the development of a number of special-purpose digital
processors for a variety of space and military agencies, including NASA, the Ballistic
Missile Systems Command, and the Oak Ridge National Laboratory. Preview
Dr. Woods has published numerous articles related to digital signal processing Digital image processing is an area characterized by the need for extensive ex-
and is co-author of Digital Image Processing, the leading text in the field. H e is a perimental work to establish the viability of proposed solutions to a given
member of several professional societies, including Tau Beta Pi, Phi Kappa Phi, problem. In this chapter we outline how a theoretical base and state-of-the-art
and the IEEE. In 1986, he was recognized as a Distinguished Engineering Alum- software can be integrated into a prototyping environment whose objective is
nus of the University of Tennessee. to provide a set of well-supported tools for the solution of a broad class of
problems in digital image processing.
Steven L. Eddins
Steven L. Eddins is development manager of the image processing group at The Background
MathWorks. Inc. He led the development of several versions of the company's
Image Processing Toolbox. His professional interests include building software An important characteristic underlying the design of image processing sys-
tools that are based on the latest research in image processing algorithms, and that tems is the significant level of testing and experimentation that normally is re-
have a broad range of scientific and engineering applic:ations. quired before arriving at an acceptable solution. This characteristic implies
Prior to joining I h e MathWorks, Inc. in 1993, Dr. E:ddins was on the faculty of that the ability to formulate approaches and quickly prototype candidate solu-
the Electrical Engineering and Computer Science Department at the University of tions generally plays a major role in reducing the cost and time required to
Illinois, Chicago. There he taught graduate and senior-level classes in digital image arrive at a viable system implementation.
processing, computer vision, pattern recognition, anti filter design, and he per- Little has been written ii the way of instructional material to bridge the gap
formed research in the area of image compression. between theory and application in a well-supported software environment. The
Dr. Eddins holds a B.E.E. (1986) and a Ph.D. (1990), both in electrical engineering main objective of this book is to integrate under one cover a broad base of the-
from the Georgia Institute of Technology.He is a membler of the IEEE. oretical concepts with the knowledge required to implement those concepts
using state-of-the-art image processing software tools. The theoretical underpin-
nings of the material in the following chapters are mainly from the leading text-
book in the field: Digital linage Processing, by Gonzalez and Woods, published
by Prentice Hall. The software code and supporting tools are based on the lead-
ing software package in the field: The MATLAB Image Processing ~ o o l b o x . ~
'In the following discussion and in subsequent chapters we sometimes refer to Digital lmage Proce.wng
by Gonzalez and Woods as .'the Gonzalez-Woods book." and to the Image Processing Toolbox as "IPT"
0' simply as the "toolbox."
Chapter 1 3%Introduction 1.2 x What Is Digital Image Processing? 3
from The Mathworks, Inc. (see Section 1.3). The material in the present book Vision is the most advanced of our senses, so it is not surprising that images
shares the same design, notation, and style of presentation as the Gonzalez- play the single most important role in human perception. However, unlike hu-
Woods book, thus simplifying cross-referencing between the two. mans. who are limited to the visual band of the electromagnetic (EM) spec-
The book is self-contained. To master its contents, the reader should have trum, imaging machines cover almost the entire E M spectrum, ranging from
introductory preparation in digital image processing, either by having taken a gamma to radio waves.They car) operate also on images generated by sources
formal course of study on the subject at the senior or first-year graduate level, that humans are not accustomed to associating with images. These include ul-
or by acquiring the necessary background in a program of self-study. It is as- trasound, electron microscopy, ,and computer-generated images. Thus, digital
sumed also that the reader has some familiarity with MATLAB, as well as image processing encompasses a wide and varied field of applications.
rudimentary knowledge of the basics of computer programming, such as that There is n o general agreement among authors regarding where image pro-
acquired in a sophomore- or junior-level course on programming in a techni- cessing stops and other related areas, such as image analysis and computer vi-
cally oriented language. Because MATLAB is an array-oriented language. sion, start. Sometimes a distinctron is made by defining image processing as a
basic knowledge of matrix analysis also is helpful. discipline in which both the input and output of a process are images. We be-
The book is based o n principles. It is organized and presented in a textbook lieve this to be a limiting and somewhat artificial boundary. For example,
format, not as a manual. Thus, basic ideas of both theory and software are ex- under this definition, even the trivial task of computing the average intensity
plained prior to the development of any new programming concepts. The ma- of an image would not be consildered an image processing operation. O n the
terial is illustrated and clarified further by numerous examples ranging from other hand, there are fields such as computer vision whose ultimate goal is to
medicine and industrial inspection to remote sensing and astronomy. This ap- use computers to emulate human vision, including learning and being able to
proach allows orderly progression from simple concepts to sophisticated im- make inferences and take actions based on visual inputs. This area itself is a
plementation of image processing algorithms. However, readers already branch of artificial intelligence (AI), whose objective is to emulate human in-
familiar with MATLAB, IPT, and image processing fundamentals can proceed telligence. The field of A1 is in its earliest stages of infancy in terms of devel-
directly to specific applications of interest, in which case the functions in the opment, with progress having been much slower than originally anticipated.
book can be used as an extension of the family of IPT functions. All new func- The area of image analysis (also called image understanding) is in between
tions developed in the book are fully documented, and the code for each is image processing and computer vision.
included either in a chapter o r in Appendix C. There are no clear-cut boundaries in the continuum from image processing
Over 60 new functions are developed in the chapters that follow. These at one end to computer vision at the other. However, one useful paradigm is to
functions complement and extend by 35% the set of about 175 functions in consider three types of computerized processes in this continuum: low-, mid-,
IPT. In addition to addressing specific applications, the new functions are clear and high-level processes. Low-level processes involve primitive operations
examples of how to combine existing MATLAB and IPT functions with new such as image preprocessing to reduce noise, contrast enhancement, and image
code to develop prototypic solutions to a broad spectrum of problems in digi- sharpening.A low-level process is characterized by the fact that both its inputs
tal image processing.The toolbox functions, as well as the functions developed and outputs are images. Mid-level processes o n images involve tasks such as
in the book, run under most operating systems. Consult the book Web site (see segmentation (partitioning an irnage into regions o r objects), description of
Section 1.5) for a complete list. those objects to reduce them to a form suitable for computer processing, and
classification (recognition) of individual objects. A mid-level process is charac-
,wA What Is Digital Image Processing? terized by the fact that its inputs generally are images, but its outputs are at-
tributes extracted from those im,ages (e.g., edges, contours, and the identity of
An image may be defined as a two-dimensional function, f ( x , y ) , where x and individual objects). Finally, higher-level processing involves "making sense" of
y are spatial coordinates, and the amplitude of f at any pair of coordinates an ensemble of recognized objects, as in image analysis, and, at the far end
(x. y ) is called the irzrensity o r gray level of the image at that point. When .r, y, of the continuum, performing the: cognitive functions normally associated with
and the amplitude values of f are all finite, discrete quantities, we call the human vision.
image a digital image. The field of digital image processing refers to processing Based on the preceding comments, we see that a logical place of overlap be-
digital images by means of a digital computer. Note that a digital image is com- tween image processing and image analysis is the area of recognition of
posed of a finite number of elements, each of which has a particular location individual regions or objects in an image.Thus, what we call in this book digital
and value. These elements are referred to as picture elements, image elements, image processing encompasses processes whose inputs and outputs are images
pels, and pi,uels. Pixel is the term most widely used to denote the elements of a and, in aridition, encompasses prclcesses that extract attributes from images. up
digital image. We consider these definitions formally in Chapter 2. to and including the recognition of individual objects. A s a simple illustration
Chapter 1 X8 Introduction 1.4 a Areas of Image Processing Covered in the Book 5
,to clarify these concepts, consider the area of automated analysis of text. The The MATLAB Student Version includes a full-featured version of
processes of acquiring an image of the area containing the text, preprocessing MATLAB. The Student Version can be purchased at significant discounts at
that image, extracting (segmenting) the individual characters, describing the university bookstores and at the Mathworks' Web site (www.mathworks.com).
characters in a form suitable for computer processing, and recognizing those Student versions of add-on products, including the Image Processing Toolbox,
individual characters, are in the scope of what we call digital image processing also are available.
in this book. Making sense of the content of the page may be viewed as
being in the domain of image analysis and even computer vision, depending on
Areas of Image Processing Covered in the Book
the level of complexity implied by the statement: "making sense." Digital
im.age processing, as we have defined it, is used succe:ssfully in a broad range of Every chapter in this book contains the pertinent MATLAB and IPT material
areas of exceptional social and economic value. needed to implement the image processing methods discussed. When a MAT-
LAB or IPT function does not exist to implement a specific method, a new
Background on MATLAB and the Tmage function is developed and documented. As noted earlier, a complete listing of
Processing TooIbox every new function is included in the book. The remaining eleven chapters
cover material in the following areas.
MATLAB is a high-performance language for technical computing. It inte-
Chapter 2: Fundamentals. This chapter covers the fundamentals of MATLAB
grates computation, visualization, and programming in an easy-to-use environ-
notation, indexing, and programming concepts. This material serves as founda-
ment where problems and solutions are expressed in familiar mathematical
tion for the rest of the book.
notation. Typical uses include the following:
Chapter 3: Intensity Transformations and Spatial Filtering. This chapter cov-
4) Math and computation ers in detail how to use MATLAB and IPT to implement intensity transfor-
41 Algorithm development mation functions. Linear and nonlinear spatial filters are covered and
41 Data acquisition illustrated in detail.
41 Modeling, simulation, and prototyping
41 Data analysis, exploration, and visualization Chapter 4: Processing in the Frequency Domain. The material in this chapter
41 Scientific and engineering graphics shows how to use IPT functions for computing the forward and inverse fast
41 Application development, including graphical user interface building Fourier transforms (FFTs), how to visualize the Fourier spectrum, and how to
implement filtering in the frequency domain. Shown also is a method for gen-
:MATLAB is an interactive system whose basic data element is an array that erating frequency domain filters from specified spatial filters.
does not require dimensioning. This allows formulating solutions to many
technical computing problems, especially those involving matrix representa- Chapter 5: Image Restoration. Traditional linear restoration methods, such as
.tions,in a fraction of the time it would take to write a program in a scalar non- the Wiener filter, are covered in this chapter. Iterative, nonlinear methods,
int~eractivelanguage such as C or Fortran. such as the Richardson-Lucy method and maximum-likelihood estimation for
The name MATLAB stands for matrix laboratoiry. MATLAB was written blind deconvolution, are discussed and illustrated. Geometric corrections and
originally to provide easy access to matrix software developed by the LIN- image registration also are covered.
PACK (Linear System Package) and EISPACK (Ei,gen System Package) pro- Chapter 6: Color Image Processing. This chapter deals with pseudocolor and
jects. Today, MATLAB engines incorporate the LAPACK (Linear Algebra full-color image processing. Color models applicable to digital image process-
Package) and BLAS (Basic Linear Algebra Subpro:grams) libraries, constitut- ing are discussed, and IPT functionality in color processing is extended via im-
ing the state of the art in software for matrix computation. plementation of additional color models. The chapter also covers applications
In university environments, MATLAB is the standard computational tool for of color to edge detection and region segmentation.
int:roductory and advanced courses in mathematics, e.ngineering,and science. In
Chapter 7:Wavelets. In its current form, IPT does not have any wavelet trans-
industry, MATLAB is the computational tool of choice for research, develop-
forms.A set of wavelet-related functions compatible with the Wavelet Toolbox
ment, and analysis. MATLAB is complemented b y a family of application-
is developed in this chapter that will allow the reader to implement all the
specific solutions called toolboxes.The Image Proces!jingToolbox is a collection
wavelet-transform concepts discussed in the Gonzalez-Woods book.
of MATLAB functions (called M-functions or M-file,s)that extend the capabili-
ty of the MATLAB environment for the solution of digital image processing Chapter 8: Image Compression. The toolbox does not have any data compres-
problems. Other toolboxes that sometimes are used to complement IPT are the sion functions. In this chapter, we develop a set of functions that can be used
Signal Processing, Neural Network, Fuzzy Logic, and Wavelet Toolboxes. for this purpose.
Chapter 1 38 Introduction 1.7 a The MATLAB Working Environment 7
Chapter 11: Representation and Description. Several new functions for ob-
ject representation and description, including chain-code and polygonal repre- which offers additional support on instructional and research topics.
sentations, are developed in this chapter. New functions are included also for
object description, including Fourier descriptors, texture, and moment invari-
ants. These functions complement an extensive set of region property func- Notation
tions available in IPT. Equations in the book are typeset using familiar italic and Greek symbols,
Chapter 12: Object Recognition. One of the important features of this chap- as in f (x, y) = A sin(ux + v y ) and 4(u, v ) = tan-'[~(u,v)/R(u, v)]. All
ter is the efficient implementation of functions for computing the Euclidean MATLAB function names and symbols are typeset in monospace font, as in
and Mahalanobis distances. These functions play a central role in pattern fft2(f),logical(A),androipoly(f, c , r ) .
matching. The chapter also contains a comprehensive discussion on how to The first occurrence of a MATLAB or IPT function is highlighted by use of
manipulate strings of symbols in MATLAB. String manipulation and matching the following icon on the page margin:
are important in structural pattern recognition.
In addition to the preceding material, the book contains three appendices.
Appendix A: Contains a summary of all IPT and new image-processing func-
tions developed in the book. Relevant MATLAB function also are included. Similarly, the first occurrence of a new function developed in the book is high-
This is a useful reference that provides a global overview of all functions in the lighted by use of the following icon on the page margin:
toolbox and the book.
function name
Appendix B: Contains a discussion on how to implement graphical user inter- 1
-
'm The Book Web Site In this section we give a brief overview of some important operational aspects
of using MATLAB.
An important feature of this book is the support contained in the book Web
site. The site address is 1-7. i The MATLAB Desktop
The MATLAB desktop is the main MATLAB application window. As Fig. 1.1
shows, the desktop contains five subwindows: the Command Window, the
This site provides support to the book in the following areas: Workspace Browser, the Current. Directory Window, the Command History
Downloadable M-files, including all M-files in the book Window, and one or more Figure Windows, which are shown only when the
Tutorials user displays a graphic.
1 Chapter 1 &!& Introduction 1.7 1 The MATLAB Working Environment 9
,r-MATLABDesktop MATLAB uses a search path to find M-files and other MATLAB-related
files, which are organized in directories in the computer file system. Any file
run in MATLAB must reside in the current directory or in a directory that
is on the search path. By default, the files supplied with MATLAB and
Mathworks toolboxes are included in the search path. The easiest way to
see which directories are on the search path, or to add or modify a search
path, is to select Set Path from the File menu on the desktop, and then use
the Set Path dialog box. It is good practice to add any commonly used di-
rectories to the search path to avoid repeatedly having the change the cur-
rent directory.
The Command History Window contains a record of the commands a user
has entered in the Command Window, including both current and previous
MATLAB sessions. Previously entered MATLAB commands can be selected
and re-executed from the Command History Window by right-clicking on a
command or sequence of commands. This action launches a menu from which
to select various options in addition to executing the commands. This is a use-
ful feature when experimenting with various commands in a work session.
t I;eend.l:4:yldl;
Command History .m, as in pixeldup .m. The MATLAB editor window has numerous pull-down
..SM%!lI) menus for tasks such as saving, viewing, and debugging files. Because it per-
I . ti:l:tlid,L:?:endl:
.xrnoir,o, forms some simple checks and also uses color to differentiate between various
.
.,~ll.<,'i.
!
,L*lC.L,f')
>:
>>~re~dI~case-512.LiC'
elements of code, this text editor is recommended as the tool of choice for
writing and editing M-functions.To open the editor, type edit at the prompt in
the Command Window. Similarly, typing edit filename at the prompt opens
the M-file filename. m in an editor window, ready for editing. As noted earli-
IGLIRE 1.1 The MATLAB desktop and its principal components. er, the file must be in the current directory, or in a directory in the search path.
at the beginning of a MATLAB session to have help readily available during that appears.Tnis opens a directory window that allows naming the file and se-
code development or other MATLAB task. lecting any folder in the system in which to save it. Then simply click Save. To
Another way to obtain help for a specific function is by typing doc followed save a selected variable from the Workspace, select the variable with a left
by the function name at the command prompt. For example, typing doc format click and then right-click on the highlighted area. Then select Save Selection
displays documentation for the function called format in the display pane of AS from the menu that appears.This again opens a window from which a fold-
the Help Browser. This command opens the browser if it is not already open. er can be selected to save the variable. To select multiple variables, use shift-
M-functions have two types of information that can be displayed by the click or control-click in the familiar manner, and then use the procedure just
user. The first is called the HI line, which contains the function name and a described for a single variable. All files are saved in double-precision, binary
one-line description. The second is a block of explanation called the Help text format with the extension .mat.These saved files commonly are referred to as
block (these are discussed in detail in Section 2.10.1). Typing help at the MAT'les. For example, a session named, say, mywork~2003~02~10, would ap-
prompt followed by a function name displays both the H I line and the Help pear as the MAT-file mywork~2003~02~10.mat when saved. Similarly, a saved
text for that function in the Command Window. Occasionally, this information image called f inal-image (which is a single variable in the workspace) will
can be more up to date than the information in the Help browser because it is appear when saved as final-image.mat.
extracted directly from the documentation of the M-function in question. Typ- To load saved workspaces andior variables, left-click on the folder icon on
ing lookfor followed by a keyword displays all the H I lines that contain that the toolbar of the Workspace Br'owser window. This causes a window to open
keyword. This function is useful when looking for a particular topic without from which a folder containing the MAT-files of interest can be selected.
knowing the names of applicable functions. For example, typing lookf o r edge Double-clicking on a selected MAT-file or selecting Open causes the contents
at the prompt displays all the H1 lines containing that keyword. Because the of the file to be restored in the Workspace Browser window.
H1 line contains the function name, it then becomes possible to look at specif- It is possible to achieve the same results described in the preceding para-
ic functions using the other help methods. Typing lookfor edge - a l l at the graphs by typing save and load at the prompt, with the appropriate file names
prompt displays the H I line of all functions that contain the word edge in ei- and path information. This approach is not as convenient, but it is used when
ther the H1 line or the Help text block.Words that contain the characters edge formats other than those available in the menu method are required. As an
also are detected. For example, the H1 line of a function containing the word exercise, the reader is encouraged to use the Help Browser to learn more
polyedge in the H1 line or Help text would also be displayed. about these two functions.
It is common MATLAB terminology to use the term help page when refer-
ring to the information about an M-function displayed by any of the preceding
approaches, excluding lookf or. It is highly recommended that the reader be-
How References Are Organized in the Book
come familiar with all these methods for obtaining information because in the All references in the book are listed in the Bibliography by author and date, as
following chapters we often give only representative syntax forms for MAT- in Soille [2003]. Most of the background references for the theoretical content
LAB and IPT functions. This is necessary either because of space limitations of the book are from Gonzalez and Woods [2002]. In cases where this is not
or to avoid deviating from a particular discussion more than is absolutely nec- true, the appropriate new references are identified at the point in the discus-
essary. In these cases we simply introduce the syntax required to execute the sion where they are needed. References that are applicable to all chapters,
function in the form required at that point. By being comfortable with online such as MATLAB manuals and other general MATLAB references, are so
search methods, the reader can then explore a function of interest in more de- identified in the Bibliography.
tail with little effort.
Finally, the MathWorkslWeb site mentioned in Section 1.3 contains a large
database of help material, contributed functions, and other resources that Summary
should be utilized when the online documentation contains insufficient infor- In addition to a brief introduction to notation and basic MATLAB tools, the material
mation about a desired topic. in this chapter emphasizes the importance of a comprehensive prototyping environ-
ment in the solution of digital image processing problems. In the following chapter we
r .7,!Saving and Retrieving a Work Session begin to lay the foundation needed to understand IPT functions and introduce a set of
fundamental programming concepts that are used throughout the book. The material
There are several ways to save and load an entire work session (the contents in Chapters 3 through 12 spans a wide cross section of topics that are in the mainstream
of the Workspace Browser) or selected workspace variables in MATLAB.The of digital image processing applications.However, although the topics covered are var-
simplest is as follows. ied, the discussion in those chapters follows the same basic theme of demonstrating
To save the entire workspace, simply right-click on any blank space in the how combining MATLAB and IPT functions with new code can be used to solve a
Workspace Browser window and select Save Workspace As from the menu broad spectrum of image-processing problems.
2.1 a Digital Irnage Representation 13
X
1 One pixel >"" T
r
One pixel >
1 Chapter 2 & Fundamentals 2.2 @! Reading Images 15
IPT documentation refers to the coordinates in Fig. 2.l(b) as pixel coordi- Format Recognized TABLE 2.1
nates. Less frequently, the toolbox also employs another coordinate conven- Name Description Extensions Some of the
tion called spatial coordinates, which uses x to refer to columns and y to refers imagelgraphics
to rows. This is the opposite of our use of variables x and y. With very few ex- TIFF Tagged Image File Format . t l f , .tiff formats supported
ceptions, we do not use IPT's spatial coordinate convention in this book, but JPEG Joint Photographic Experts Group . j pg, . I peg by i m r e a d and
the reader will definitely encounter the terminology in IPT documentation. GIF Graphics Interchange orm mat^ .g l f i m w r i t e , starting
BMP Windows Bitmap .bmp with MATLAB 6.5.
2.12 Images as Matrices PNG Portable Network Graphics .Prig Earlier versions
XWD X Window Dump .xwd support a subset of
The coordinate system in Fig. 2.l(a) and the preceding discussion lead to the these formats. See
following representation for a digitized image function: ' G I F IS supported by lmread, but not by l m w r i t e .
online help for a
f(0,l) ... f(0,N-1) 1 complete list of
supported formats.
Here, f ilename is a string containing the complete name of the image file (in-
cluding any applicable extension). For example, the command line
The right side of this equation is a digital image by definition. Each element of
this array is called an image element, picture element, pixel, or pel. The terms reads the JPEG (Table 2.1) image chestxray into image array f . Note the use
image and pixel are used throughout the rest of our discussions to denote a of single quotes ( ' ) to delimit the string filename. The semicolon at the end \
fi;,+efi~colon ( ;)
'ATLAB and IPT
digital image and its elements.
A digital image can be represented naturally as a MATLAB matrix:
of a command line is used by MATLAB for suppressing output. If a semicolon ,' '
cumentation use is not included, MATLAB displays the results of the operation(s) specified in
th the terms matrix that 1ine.The prompt symbol (>>I designates the beginning of a command line, -, #"/~P ; ,\
R r o r n p ti>>)
arrd array, mostly in-
rchangeably. How-
as it appears in the MATLAB Command Window (see Fig. 1.1). 0 '
,er,keep in mind When, as in the preceding command line, no path information is included in
zt a matrir is two filename, imread reads the file from the current directory (see Section 1.7.1)
nensional,whereas In Wmdows, drrecto-
and, if that fails, it tries to find the file in the MATLAB search path (see r,,salso arecalled
urt array can have
iy finite dimension. where f ( 1 , 1 ) = f (0,O) (note the use of a monospace font to denote MAT- Section 1.7.1).The simplest way to read an Image from a specified directory is folders
LAB quantities). Clearly the two representations are identical, except for the to include a full or relative path to that directory in f llename. For example,
shift in origin. The notation f ( p, q ) denotes the element located in row p and
column q. For example, f ( 6 , 2 ) is the element in the sixth row and second col-
umn of the matrix f . Qpically we use the letters M and N, respectively, to de-
note the number of rows and columns in a matrix. A 1 x N matrix is called a reads the image from a folder called myimages on the D: drive, whereas
row vector, whereas an M x 1 matrix is called a column vector. A 1 x 1 matrix is
a scalar.
Matrices in MATLAB are stored in variables with names such as A, a, RGB,
real-array, and so on. Variables must begin with a letter and contain only reads the image from the myimages subdirectory of the current working di-
letters, numerals, and underscores. As noted in the previous paragraph, all rectory. The Current Directory Window on the MATLAB desktop toolbar
MATLAB quantities in this book are written using monospace characters. We displays MATLAB's current working directory and provides a simple, man-
use conventional Roman, italic notation, such as f ( x , y), for mathematical ual way to change it. Table 2.1 lists some of the most popular imagelgraphics
expressions. formats supported by imread and imwrite (imwrite is discussed in
Section 2.4).
,
i/i
Reading Images Function s i z e gives the row and column dimensions of an image: . "sGe
*-
Images are read into the MATLAB environment using function imread, >> s i z e ( f )
whose syntax is
ans =
16 Chapter 2 a F~indamentals 2.3 iPB Displaying Images 17
1s 117 s i z e , ~ n n j ~ y This function is particularly useful in programming when used in the following shown on a display that appears below the figure window. When working
MATLA B rrrlrl IPT form to determine automatically the size of an image: with color images, the coordinates as well as the red, green, and blue compo-
Iinctiorls carr rerunl
nore than one out- nents are displayed. If the left button on the mouse is clicked and then held
utr urgunzenr. Multi- >> [ M , N] = size(f); pressed, pixval displays the Euclidean distance between the initial and cur-
de output rent cursor locations.
irylrrnenrs nzrrst be
-rrclosedwithin This syntax returns the number of rows (M) and columns (N) in the image. The syntax form of interest here is
quore bmckets, [ ] The whos function displays additional information about an array. For in-
stance, the statement pixval
I I which shows the cursor on the last image displayed. Clicking the X button on
whas >> whos f
* / \ the cursor window turns it off.
gives
il (a) The following statements read from disk an image called rose-51 2 . t i f , EXAMPLE 2.1:
Name Size Bytes class extract basic information about the image, and display it using imshow: Image reading
and displaying.
f 1024x1024 1048576 uint8 array
Grand t o t a l i s 1048576 elements using 1048576 bytes >> f = i m r e a d ( ' r o s e - 5 1 2 . t i f 1 ) ;
>> whos f
The u i n t 8 entry shown refers to one of several MATLAB data classes dis- Name Size Bytes Class
cussed in Section 2.5. A semicolon at the end of a whos line has no effect, so f 512x512 262 144 uint8 array
normally one is not used. Grand t o t a l i s 262144 elements using 262144 bytes
Displaying Images A semicolon at the end of an imshow line has no effect, so normally one is
Images are displayed on the MATLAB desktop using function imshow, which not used. Figure 2.2 shows what the output looks like on the screen.The figure
has the basic syntax: number appears on the top, left of the window. Note the various pull-down
1" '
menus and utility buttons. They are used for processes such as scaling, saving,
J rmshow and exporting the contents of the display window. In particular, the Edit menu
has functions for editing and formatting results before they are printed or
where f is an image array, and G is the number of intensity levels used to dis- saved to disk.
play it. If G is omitted, it defaults to 256 levels. Using the syntax
imshow(f, [low h i g h ] )
FIGURE 2.2
Screen capture
displays as black all values less than or equal to low, and as white all values showing how an
greater than or equal to high.The values in between are displayed as interme- image appears on
diate intensity values using the default number of levels. Finally, the syntax the MATEAB
desktop.
However, in most
of the examples
throughout this
sets variable low to the minimum value of array f and high to its maximum book, only the
value. This form of imshow is useful for displaying images that have a low dy- images
namic range or that have positive and negative values. themselves are
shown. Note the
Function p i x v a l is used frequently to display the intensity values of indi- figure number on
vidual pixels interactively. This function displays a cursor overlaid on an the top, left part
image. As the cursor is moved over the image with the mouse, the coordi- of the window.
nates of the cursor position and the corresponding intensity values are
3 Chapter 2 B FLI 2.4 Writing Images 19
If another image. g, is displayed using imshow, MATLAB replaces the ~f filename contains no path information, then imwrite saves the file in the
image in the screen with the new image. To keep the first image and output a current working directory.
second image, we use function f i g u r e as follows: The imwrite function can have other parameters, depending on the file for-
mat selected. Most of the work in the following chapters deals either with
>> f i g u r e , imshow(g) JPEG or TIFF images, so we focus attention here on these two formats.
Using the statement A more general imwrite syntax applicable only to JPEG images is
rncrion f i g u r e
~(eatesafigllre wln-
>> imshow(f), f i g u r e , imshow(g) imwrite(f, 'filename.jpgl, ' q u a l i t y ' , q)
' 7 ~W. l ~ e nused
itholrt an argll- where q is an integer between 0 and 100 (the lower the number the higher the
~ t t tns
, sho~u~z here, displays both images. Note that more than one command can be written on a
?imply creates ti line, as long as different commands are properly delimited by commas or semi- degradation due to JPEG compression).
,,cw,+ig~cre window. colons. As mentioned earlier, a semicolon is used whenever it is desired to sup-
-yping f i g u r e ( n ) , Figure 2.4(a) shows an image, f , typical of sequences of images resulting EXAMPLE 2.2:
Irces,figllre number press screen outputs from a command line.
from a given chemical process. It is desired to transmit these images on a rou- Writing an image
,o become vrsible. (b) Suppose that we have just read an image h and find that using imshow ( h ) and using
produces the image in Fig. 2.3(a). It is clear that this image has a low dynamic tine basis to a central site for visual andlor automated inspection. In order to
reduce storage and transmission time, it is important that the images be com- function i m f i n f o.
range, which can be remedied for display purposes by using the statement
pressed as much as possible while not degrading their visual appearance
beyond a reasonable level. In this case "reasonable" means no perceptible
false contouring. Figures 2.4(b) through (f) show the results obtained by writ-
Figure 2.3(b) shows the result. The improvement is apparent. R ing image f to disk (in JPEG format), with q = 50,25,15,5, and 0, respective-
ly. For example, for q = 25 the applicable syntax is
Writing Images >> i m w r i t e ( f , ' b u b b l e s 2 5 . j p g 1 , ' q u a l i t y ' , 25)
Images are written to disk using function imwrite, which has the following
basic syntax: The image for q = 15 [Fig. 2.4(d)] has false contouring that is barely visible,
but this effect becomes quite pronounced for q = 5 and q = 0. Thus, an
imwrite(f, 'filename') acceptable solution with some margin for error is to compress the images with
q = 25. In order to get an idea of the compression achieved and to obtain other
With this syntax, the string contained in filename must include a recognized image file details, we can use function imf i n f o, which has the syntax
file format extension (see Table 2.1). Alternatively, the desired format can be
specified explicitly with a third input argument. For example, the following imfinfo filename ' lmflnfo
command writes f to a TIFF file named patient lo-run1 :
where filename is the complete file name of the image stored in disk. For
>> imwrite(f, 'patientlo-runl', 'tif') example,
or, alternatively, >> imfinfo bubbles25.jpg
b outputs the following information (note that some fields contain no informa-
GURE 2.3 (a) An tion in this case):
\age, h , with low
dynamic range.
J) Result of scaling
Filename : 'bubbles25.jpg1
using i m s h o w FileModDate: '04-Jan-2003 12:31:26'
I , [ ] ) . (Original Filesize : 13849
la age courtesy of Format : 'jpg'
7 r . David R. Formatversion : I I
:nter.)
Comment : {1
20 Chapter 2 n Fundamentals 2.4 a Writing Images 21
>> K = imfinfo('bubbles25.jpg');
>r image-bytes = K.Width*K.Height*K.BitDepth/a;
See Exan~ple2.11 >> compressed-bytes = K . F i l e S i z e ;
for afr~~zctiorz that >> c o m p r e s s i o n - r a t i o = image-bytes/compressed_bytes
creares NILthe images
in Fig. 2.4 using a compression-ratio =
sinrple f o r loop.
35.1612
Note that i m f i n f o was used in two different ways. The first was to type To learn ,nore abo~rt
i m f i n f o bubbles25. j p g at the prompt, which resulted in the information command ,frlnction
drlality, cons~ilrrhe
being displayed on the screen. The second was to type K = i m f i n f o ( ' b u b - help page on this
b l e s 2 5 . j pg ' ) , which resulted in the information generated by irnf i n f o topic. See Section
being stored in K.These two different ways of calling i m f i n f o are an example 1.7.3 regarding help
pages.
of command-function duality, an important concept that is explained in more
detail in the MATLAB online documentation.
'XAMPLE 2.3: 1 Figure 2.5(a) is an 8-bit X-ray image of a circuit board generated during 450 x 450 pixels are distributed over a 1.5 x 1.5-inch area. Processes such as
'sing lmwrite quality inspection. It is in jpg format, at 200 dpi. The image is of size this are useful for controlling the size of an image in a printed document with-
irameters. 450 x 450 pixels, so its dimensions are 2.25 X 2.25 inches. We want to store out sacrificing resolution.
this image in t i f format, with no compression, under the name s f . In addition,
we want to reduce the size of the image to 1.5 X 1.5 inches while keeping the Often, it is necessary to export images to disk the way they appear on the
pixel count at 450 x 450. The following statement yields the desired result: MATLAB desktop. This is especially true with plots, as shown in the next
chapter. The contents of a figure window can be exported to disk in two ways.
The first is to use the File pull-down menu in the figure window (see Fig. 2.2)
and then choose Export. With this option, the user can select a location, file
name, and format. More control over export parameters is obtained by using
The values of the vector [ c o l r e s rowres] were determined by multiplying the p r i n t command:
200 dpi by the ratio 2.25/1.5, which gives 300 dpi. Rather than do the compu-
tation manually, we could write p r i n t -fno -dfileformat -rresno filename print
round >> res = round(200*2.25/1.5); where no refers to the figure number in the figure window of interest,
>> imwrite(f, ' s f . t i f l , 'compression', 'none' , ' r e s o l u t i o n 1 , r e s ) f i l e f o r m a t refers to one of the file formats in Table 2.1, resno is the resolu-
tion in dpi, and filename is the name we wish to assign the file. For example,
where function round rounds its argument to the nearest integer. It is impor- to export the contents of the figure window in Fig. 2.2 as a t i f file at 300 dpi,
tant to note that the number of pixels was not changed by these commands. and under the name hi-res-rose, we would type
Only the scale of the image changed. The original 450 X 450 image at 200 dpi
is of size 2.25 X 2.25 inches. The new 300-dpi image is identical, except that its >> p r i n t -fl - d t i f f -r300 hi-res-rose
'MATLAB documentation often uses the terms drrrri clors and llrrra fipr interchangeably. In this book.
we reserve use of the term type lor images. as discussed in Section 2.6.
24 Chapter 2 .a Fundamentals 2.7 @ Converting between Data Classes and Image Types 25
array, B. is generated by the command B = double (A).This conversion is used from which we see that function im2uint8 sets to 0 all values in the input that
routinely throughout the book because MATLAB expects operands in nu- are less than 0, sets to 255 all values in the input that are greater than 1, and
merical computations to be double-precision. floating-point numbers. If C is an multiplies all other values by 255. Rounding the results of the multiplication to
array of class double in which all values are in the range [O, 2551 (but possibly the nearest integer completes the conversion. Note that the rounding behavior
containing fractional values), it can be converted to an u i n t 8 array with the of im2uint8 is different from the data-class conversion function u i n t 8 dis-
command D = u i n t 8 ( C ) . cussed in the previous section, which simply discards fractional parts.
If an array of class double has any values outside the range [0,255]and it is Converting an arbitrary array of class double to an array of class double
converted to class u i n t 8 in the manner just described, MATLAB converts to scabd to the range [O, 11 can b e accomplished by using function matzgray
0 all values that are less than 0, and converts to 255 all values that are greater whose basic syntax is
than 255. Numbers in between are converted to integers by discarding their
fractional parts.Thus, proper scaling of a double array s o that its elements are
in the range [O, 2551 is necessary before converting it to u i n t 8 . A s indicated in
Section 2.6.2, converting any of the numeric data classes to l o g i c a l results in where image g has values in the range 0 (black) to 1 (white).The specified pa-
an array with logical I s in locations where the input array had nonzero values, rameters Amin and Amax are such that values less than Amin in A become 0 in g,
and logical 0s in places where the input array contained 0s. and values greater than Amax in A correspond to 1in g. Writing
4BLE 2.3
Name Converts Input to: Valid Input Image Data Classes
unctions in IPT
lor converting im2uint8 uint8 logical,uint8,uintl6,anddouble from which we infer that the conversion when the input is of class u i n t 8 is
etween image im2uintl6 uintl6 logical,uint8,uintl6,anddouble done simply by dividing each value of the input array by 255. If the input is of
'asses and types.
-e Table 6.3 for "lat2gray double (in range [0, I]) double class u i n t l 6 the division is by 65535.
L;rnversionsthat im2double double logical,uint8,uintl6,anddouble
pplyspecifically im2bw logical uint8,uintl6,anddouble
1 color images. 'Section 2.8.2 explains the use of square brackets and senlicolons to specify a matrix.
28 Chapter 2 ;aii Fundamentals 2.7 @ Converting between Data Classes and Image Types 29
Finally, we consider conversion between binary and intensity image types. AS mentioned in Section 2.5, we can generate a binary array directly using re-
Function im2bw, which has the syntax lational operators (Section 2.10.2).Thus we get the same result by writing
EXAMPLE 2.4: a (a) We wish to convert the following double image >> gbd = im2double(gb)
Converting gbd =
between image
classes and types. >' = [ ; 41 0 0
1 1
because im2double would have divided all the elements by 255. This did not
happen in the preceding conversion because im2double detected that the
input was a l o g i c a l array, whose only possible values are 0 and 1.If the input
in fact had been an u i n t 8 numeric array and we wanted to convert it to class
double while keeping the 0 and 1 values, we would have converted the array
Then we convert it to binary using a threshold, say, of value 0.6: by writing
Finally, we point out that MATLAB supports nested statements, so we could have TO access blocks of elements, we use MATLAB's colon notation. For exam-
started with image f and arrived at the same result by using the one-line statement ple, to access the first three elements of v we write
&F
>> gbd = im2double(im2bw(mat2gray(f), 0.6)); ;& '5-i
.s,<? ,. .,
.,,-<,;c.:co!an
.;,
. k.
or by using partial groupings of these functions. Of course, the entire process
could have been done in this case with a simpler command: 1 3 5
>> gbd = double(f > 2); Similarly, we can access the second through the fourth elements
Array Indexing 3 5 7
MATLAB supports a number of powerful indexing schemes that simplify
array manipulation and improve the efficiency of programs. In this section we or all the elements from, say, the third through the last element:
discuss and illustrate basic indexing in one and two dimensions (i.e., vectors
and matrices). More sophisticated techniques are introduced as needed in sub-
sequent discussions.
. linspace
As shown in the following section, the ability to use a vector as an index into
another vector also plays a key role in matrix indexing.
2,8.2 Matrix Indexing The following statement extracts the top two rows:
Matrices can be represented conveniently in MATLAB as a sequence of row
vectors enclosed by square brackets and separated by semicolons. For exam-
ple, typing
displays the 3 X 3 matrix To create a matrix B equal to A but with its last column set to Os, we write
Note that the use of semicolons here is different from their use mentioned ear-
lier to suppress output or to write multiple commands in a single line.
We select elements in a matrix just as we did for vectors, but now we need
two indices: one to establish a row location and the other for the correspond- Operations using end are carried out in a manner similar to the examples
ing column. For example, to extract the element in the second row, third col- given in the previous section for vector indexing. The following examples illus-
umn. we write trate this.
>> A ( e n d , end - 2)
ans =
7
ans =
6 4
9 7
This use of the colon is helpful when, for example, we want to find the sum of
Using vectors to index into a matrix provides a powerful approach for ele-
ment selection. For example, all the elements of a matrix:
then
>> A ( D )
ans =
1
6
Finally, we point out that use of a single colon as an index into a matrix se- Accessing A with a single subscript indexes directly into this column. For exam-
lects all the elements of the array (on a column-by-column basis) and arranges ple, A ( 3 ) accesses the third value in the column, the number 7; A ( 8 ) accesses
them in the form of a column vector. For example, with reference to matrix T2, the eighth value, 6, and so on. When we use the column notation, we are simply
36 Chapter 2 @ Fundamentals 2.9 i$it Some Important Standard Arrays 37
addressing all the elements, A ( 1 :end). This type of indexing is a basic staple in Finally, Fig. 2.6(e) shows a horizontal scan line through the middle of
vectorizing loops for program optimization, as discussed in Section 2.10.4. Fig. 2.6(a), obtained using the command
EXAMPLE 2.5: % The image in Fig. 2.6(a) is a 1024 X 1024 intensity image, f, of class u i n t 8 .
Some simple The image in Fig. 2.6(b) was flipped vertically using the statement
image operations The p l o t function is discussed in detail in Section 3.3.1. nrai
using array >> f p = f ( e n d : - l : l , :);
indexing.
The image shown in Fig. 2.6(c) is a section out of image (a), obtained using 2.8.3 Selecting Array Dimensions
the command Operations of the form
operation(A, dim)
Similarly, Fig. 2.6(d) shows a subsampled image obtained using the where o p e r a t i o n denotes an applicable MATLAB operation, A is an array,
statement and dim is a scalar, are used frequently in this book. For example, suppose that
A is an array of size M X N. The command
gives the size of A along its first dimension, which is defined by MATLAB as
the vertical dimension. That is, this command gives the number of rows in A.
Similarly, the second dimension of an array is in the horizontal direction, so
FIGURE 2.6 the statement s i z e (A,2) gives the number of columns in A. A singleton di-
Results obtained mension is any dimension, dim, for which s i z e (A, d i m ) = 1. Using these con-
using array cepts, we could have written the last command in Example 2.5 as
indexing.
(a) Original
image. (b) Image
flipped vertically.
(c) Cropped MATLAB does not restrict the number of dimensions of an array, so being
image. able to extract the components of an array in any dimension is an important
(d) Subsampled feature. For the most part, we deal with 2-D arrays, but there are several in-
image. (e) A stances (as when working with color or multispectral images) when it is neces-
horizontal scan sary to be able to "stack" images along a third or higher dimension. We deal
line throueh the
middle of t h e with this in Chapters 6,11, and 12. Function ndims, with syntax
Image in (a).
>> [ s , p] = sumprod(f, g ) ;
or they can be used as elements of other functions, in which case they become
subfunctions. As noted in the previous paragraph, if the output has a single ar-
gument, it is acceptable to write it without the brackets, as in
Introduction to M-Function Programming
One of the most powerful features of the Image Processing Toolbox is its
transparent access to the MATLAB programming environment. As will be- The H1 line is the first text line. It is a single comment line that follows the
come evident shortly, MATLAB function programming is flexible and partic- function definition line. There can be no blank lines or leading spaces between
ularly easy to learn. the H1 line and the function definition line. An example of an H1 line is
?!+I 0.1 M-Files % SUMPROD Computes t h e sum and product of two images
So-called M-files in MATLAB can be scripts that simply execute a series of As indicated in Section 1.7.3, the H 1 line is the first text that appears when a
MATLAB statements, or they can be functions that can accept arguments and user types
can produce one or more outputs. The focus of this section in on M-file func-
tions.These functions extend the capabilities of both MATLAB and IPT to ad- '> help function-name
dress specific, user-defined applications.
40 Chapter 2 a Fundamentals 2.1 0 Introduction to M-Function Programming 41
at the MATLAB prompt. Also, as mentioned in that section, typing lookfor of corresponding elements of A and B. In other words, if C = A . *B,
keyword displays all the HI lines containing the string keyword.This line pro- then C ( I , J ) = A ( I , J ) "6 ( I , J ) . Because matrix and array operations are the
vides important summary information about the M-file, so it should be as de- same for addition and subtraction, the character pairs .+ and .- are not used.
scriptive as possible. When writing an expression such as B = A, MATLAB makes a "note" that B
Help text is a text block that follows the H I line, without any blank lines in is equal to A, but does not actually copy the data into B unless the contents of
between the two. Help text is used to provide comments and online help for A change later in the program. This is an important point because using dif-
the function. When a user types help f unction-name at the prompt, MAT- ferent variables to "store" the same information sometimes can enhance code
LAB displays all comment lines that appear between the function definition clarity and readability. Thus, the fact that MATLAB does not duplicate infor-
line and the first noncomment (executable or blank) line. The help system ig- mation unless it is absolutely necessary is worth remembering when writing
nores any comment lines that appear after the Help text block. MATLAB code. Table 2.4 lists the MATLAB arithmetic operators, where A
The function body contains all the MATLAB code that performs computa-
tions and assigns values to output arguments. Several examples of MATLAB MATLAB Comments
TABLE 2.4
code are given later in this chapter. Array and matrix
Operator Name Function and Examples
arithmetic
All lines preceded by the symbol "%" that are not the HI line or Help text are
+ Array and matrix plus(A, B ) operators.
considered function comment lines and are not considered part of the Help text addition computations
block. It is permissible to append comments to the end of a line of code. - Array and matrix minus (A, 8 ) involving these
M-files can be created and edited using any text editor and saved with the subtraction operators can be
extension .m in a specified directory, typically in the MATLAB search path. .* Array multiplication times (A, 13) implemented using
Another way to create or edit an M-file is to use the e d i t function at the the operators
prompt. For example, * Matrix multiplication mtimes (A, B ) A*B, standard matrix themselves, as in
c4.d A + B, or using the
multiplication, or a*A,
-);6>edit >> e d i t sumprod MATLAB
, / multiplication of a scalar
times all elements of A. functions shown, as
opens for editing the file sumprod. m if the file exists in a directory that is in the in plus (A, B).The
MATLAB path or in the current directory. If the file cannot be found, MAT- .I Array right division rdivide(A, 8 ) C=A./B, C(1, J )
examples shown
LAB gives the user the option to create it. As noted in Section 1.7.2, the = A ( I ,J)/B(I, J ) .
for arrays use
MATLAB editor window has numerous pull-down menus for tasks such as .\ Array left division ldivide (A, B ) C=A.\B, C(1, J ) matrices to
= B ( I , J ) / A ( I ,J ) . simplify the
saving, viewing, and debugging files. Because it performs some simple checks
and uses color to differentiate between various elements of code, this text edi- / Matrix right division mrdivide (A, B ) A/B is roughly the same as notation, but they
A * i n v ( B ) , depending are easily
tor is recommended as the tool of choice for writing and editing M-functions. on computational accuracy. extendable to
\ Matrix left division mldivide(A, B ) A\B is roughly the same as higher dimensions.
2.1 0.2 Operators i n v ( A ) *B,depending
on computational accuracy.
MATLAB operators are grouped into three main categories:
. Array power power(A,B) If C=A.^B,then
Arithmetic operators that perform numeric computations C(1, J ) =
Relational operators that compare operands quantitatively A ( 1 , J ) * B ( I ,J ) .
Logical operators that perform the functions AND, OR, and NOT A Matrix power mpower (A, B ) See onIine help for a
These are discussed in the remainder of this section. discussion of this operator.
.' Vector and matrix transpose ( A ) A. ' . Standard vector and
transpose matrix transpose.
Arithmetic Operators
Vector and matrix ctranspose(A) A'. Standard vector and
MATLAB has two different types of arithmetic operations. Matrix arithmetic complex conjugate matrix conjugate transpose.
operations are defined by the rules of linear algebra. Array arithmetic opera- transpose WhenAisrealA.' =A'.
tions are carried out element by element and can be used with multidimen- + Unary plus uplus ( A ) +A is the same as 0 + A.
dvt sional arrays. The period (dot) character (.) distinguishes array operations - Unary minus uminus ( A ) -A is the same as 0 - A
' notahon from matrix operations. For example, A*B indicates matrix multiplication in the or -1 *A.
traditional sense, whereas A . *B indicates array multiplication, in the sense that Colon Discussed in Section 2.8.
the result is an array, the same size as A and 6,in which each element 1s the
$2 Chapter 2 a Fundamentals 2.1 0 is8 Introduction to M-Function Programming 43
lABLE 2.5 ~ s e don the right of any of the previous three forms. Function m i n has the
Function Description
I'he image same syntax forms just described.
irithmetic lmadd Adds two images; or adds a constant to an image.
'unctions
;upported by IPT.
I imsubtract Subtracts two images; or subtracts a constant from an image. Suppose that we want to write an M-function, call it f g p r o d , that multiplies EXAMPLE 2.6:
immultiply Multiplies two images, where the multiplication is two input images and outputs the product of the images, the maximum and min- Illustration of
carried out between pairs of corresponding image elements; imum values of the product, and a normalized product image whose values are operators
arithmeticand
or multiplies a constant times an image. in the range [0, 11.Using the text editor we write the desired function as follows: functions max and
imdivide Divides two images, where the division is carried out min.
between pairs of corresponding image elements; or divides f u n c t i o n [p, pmax, pmin, p n l = i m p r o d ( f , g )
an image by a constant. %IMPROD Computes t h e p r o d u c t o f two images.
I imabsdiff Computes the absolute difference between two images. % [ P , PMAX, PMIN, PN] = IMPROD(F, G)t o u t p u t s t h e element-by-
I imcomplement Complements an image. See Section 3.2.1. % element p r o d u c t o f two i n p u t images, F and G I t h e p r o d u c t
imlincomb Computes a linear combination of two or more images. See % maximum and minimum values, and a n o r m a l i z e d p r o d u c t a r r a y w i t h
Section 5.3.1 for an example. % values i n t h e range [0, I ] . The i n p u t images must be o f t h e same
% s i z e . They can be of c l a s s u i n t 8 , u n i t 1 6 , o r double. The o u t p u t s
% a r e o f c l a s s double.
fd = double(f);
and B are matrices or arrays and a and b are scalars. All operands can be real gd = d o u b l e ( g ) ;
or complex. The dot shown in the array operators is not necessary if the p = fd.*gd;
operands are scalars. Keep in mind that images are 2-D arrays, which are pmax = m a x ( p ( : ) ) ;
equivalent to matrices, so all the operators in the table are applicable to pmin = m i n ( p ( : ) ) ;
images. pn = mat2gray(p);
The toolbox supports the image arithmetic functions listed in Table 2.5. Al-
though these functions could be implemented using MATLAB arithmetic op- Note that the input images were converted to d o u b l e using the function
d o u b l e instead of i m 2 d o u b l e because, if the inputs were of type u i n t 8 ,
erators directly, the advantage of using the IPT functions is that they support
the integer data classes whereas the equivalent MATLAB math operators re- i m 2 d o u b l e would convert them to the range [0, 11.Presumably, we want p to
quire inputs of class d o u b l e . contain the product of the original values.To obtain a normalized array, pn, in
Example 2.6, to follow, uses functions max and min. The former function has the range [O,l]we used function m a t 2 g r a y . Note also the use of single-colon
the syntax forms indexing, as discussed in Section 2.8.
Suppose that f = [ I 2; 3 41 and g = [I 2 ; 2 1 1. Typing the preceding
function at the prompt results in the following output:
C = max(A)
C = max(A, 6)
>> [ p , pmax, pmin, p n ] = i m p r o d ( f , g )
C = max(A, [ 1, d i m )
[C, .
I]= m a x ( . . ) P =
1 4
In the first form, if A is a vector, max ( A ) returns its largest element; if A is a ma-
trix, then max ( A ) treats the columns of A as vectors and returns a row vector
pmax =
containing the maximum element from each column. In the second form,
max (A, B) returns an array the same size as A and B with the largest elements 6
taken from A or 6. In the third form, max (A, [ ] , d i m ) returns the largest ele- pmin =
ments along the dimension of A specified by scalar dim. For example, max (A,
[ 1 , 1 ) produces the maximum values along the first dimension (the rows) of
..
A. Finally, [C, II = max ( . ) also finds the indices of the maximum values of
'In MATLAB documentation, it is customary to use uppercase characters in the H1 line and in Help text
A, and returns them in output vector I. If there are several identical maximum when referring to function names and arguments. This is done to avoid confusion between program
values, the index of the first one found is returned.The dots indicate the syntax names/variables and normal explanatory text.
46 Chapter 2 r Fundamentals 2.1 0 r Introduction to M-Function Programming 47
EXAMPLE 2.8: a Consider the AND operation on the following numeric arrays:
Logical operators.
> > A = [ I 2 0; 0 4 51; ans =
>> B = [ I -2 3 ; 0 1 11;
1 1 1
> > A & B
ans =
1 1 0 ans =
0 1 1 1 1 1
>> a l l ( B )
We see that the AND operator produces a logical array that is of the same size
as the input arrays and has a 1 at locations where both operands are nonzero ans =
and 0s elsewhere. Note that all operations are done on pairs of corresponding 0 0 1
elements of the arrays, as before.
The OR operator works in a similar manner. An OR expression is t r u e if ei- >> any(B)
ther operand is a logical 1 or nonzero numerical quantity, or if they both are ans =
logical 1s or nonzero numbers; otherwise it is f a l s e . The NOT operator works
with a single input. Logically, if the operand is t r u e , the NOT operator converts 0 1 1
it to f a l s e . When using NOT with numeric data, any nonzero operand becomes
0, and any zero operand becomes 1. S Note how functions a l l and any operate on columns of A and B. For instance,
the first two elements of the vector produced by a l l ( 6 ) are 0 because each
MATLAB also supports the logical functions summarized in Table 2.8. The of the first two columns of B contains at least one 0; the last element is 1 be-
a l l and any functions are particularly useful in programming. cause all elements in the last column of B are nonzero. a
'ABLE 2.9
~omefunctions
hat return a
Function
iscell(C)
Description
True if C is a cell array.
I Number Representation
MATLAB uses conventional decimal notation, with a n optional decimal point
and leading plus o r minus sign, for numbers. Scientific notation uses the letter
.agical 1or a iscellstr(s) True if s is a cell array of strings. e to specify a power-of-ten scale factor. Imaginary numbers use either i o r j as
' 2gicalO True if s is a character string.
ischar(s) a suffix. Some examples of valid number representations are
lepending on
ihether the value isempty ( A ) True if A is the empty array, [ 1.
~r condition in isequal(A, 6 ) True if A and B have identical elements and dimensions.
'heir arguments i s f i e l d (S , ' name ' ) True if ' name ' is a field of structure S.
re t r u e or
alse. See online iSf inite(A) True in the locations of array A that are finite.
.elp for a isinf (A) True in the locations of array A that are infinite. All numbers are stored internally using the long format specified by the Insti-
-0mplete list isletter(A) True in the locations of A that are letters of the alphabet. tute of Electrical and Electronics Engineers (IEEE) floating-point standard.
islogical (A) True if A is a logical array. Floating-point numbers have a finite precision of roughly 16 significant deci-
ismember(A,B) True in locations where elements of A are also in 6. mal digits and a finite range of approximately to
isnan ( A ) True in the locations of A that are NaNs (see Table 2.10 for
a definition of NaN). 2.1 0.3 Flow Control
isnumeric(A) True if A is a numeric array. The ability t o control the flow of operations based o n a set of predefined con-
isprime(A) True in locations of A that are prime numbers. ditions is at the heart of all programming languages. In fact, conditional
isreal (A) True if the elements of A have no imaginary parts. branching was one of two key developments that led to the formulation of
isspace(A) True at locations where the elements of A are whitespace general-purpose computers in the 1940s (the other development was the use
characters. of memory to hold stored programs and data). MATLAB provides the eight
issparse(A) True if A is a sparse matrix. flow control statements summarized in Table 2.11. Keep in mind the observa-
tion made in the previous section that MATLAB treats a logical 1 o r nonzero
i s s t r u c t (S) True if S is a structure.
number as t r u e , and a logical o r numeric 0 as f a l s e .
i f expressionl
statements1
e l s e i f expression2 Finally, note that the e r r o r function terminates execution of the program and
statements2 outputs the message contained within the parentheses (the quotes shown are
else required).
statements3
end
w h i l e expression
switch
statements
end This is the statement of choice for controlling the flow of an M-function based
on different types of inputs.The syntax is
As in the case o f f o r , w h i l e loops can be nested:
switch switch-expression
w h i l e expression1 case c a s e - e x p r e s s i o n
statements 1 Statement ( s )
w h i l e expression2 case { c a s e - e x p r e s s i o n 1 , case-expression2, . . .}
statements2 statement ( s )
end otherwise
a d d i t i o n a l loop1 statements statement ( s )
end end
54 Chapter 2 a Fundamentals 2.1 0 tgl Introduction to M-Function Programming 55
The switch construct executes groups of statements based on the value of a f o r c = cy:colhigh
variable or expression. The keywords case and otherwise delineate the ycount = ycount + 1 ;
~ ( x c o u n t ,ycount) = f ( r , c ) ;
groups. Only the first matching case is executed.' There must always be an
end to match the switch statement. The curly braces are used when multiple end
expressions are included in the same case statement. As a simple example, end
suppose that we have an M-function that accepts an image f and converts it to In the following section we give a significantly more efficient implementation
a specified class, call it newclass. Only three image classes are acceptable for of this code. As an exercise, the reader should implement the preceding pro-
the conversion: uint8, u i n t 16, and double.The following code fragment per- gram using while instead of f o r loops. B
forms the desired conversion and outputs an error if the class of the input
image is not one of the acceptable classes:
2.1 0.4 Code Optimization
switch newclass As discussed in some detail in Section 1.3, MATLAB is a programming lan-
case ' u i n t 8 ' guage specifically designed for array operations. Taking advantage of this fact
g = im2uint8(f); whenever possible can result in significant increases in computational speed.
case ' u i n t l 6 '
g = im2uintl6(f); In this section we discuss two important approaches for MATLAB code opti-
case ' d o u b l e ' mization: vectorizing loops and preallocating arrays.
g = im2double(f);
otherwise Vectorizing Loops
error('Unknown o r improper image c l a s s . ' ) Vectorizing simply means converting f o r and while loops to equivalent vec-
end tor or matrix operations. As will become evident shortly, vectorization can re-
sult not only in significant gains in computational speed, but it also helps
The switch construct is used extensively throughout the book. improve code readability. Although multidimensional vectorization can be dif-
ficult to formulate at times, the forms of vectorization used in image process-
EXAMPLE 2.12: In this example we write an M-function (based on f o r loops) to extract a ing generally are straightforward.
Extracting a rectangular subimage from an image. Although, as shown in the next section,
subimage a We begin with a simple example. Suppose that we want to generate a 1-D
we could do the extraction using a single MATLAB statement, we use the pre- function of the form
given image.
sent example later to compare the speed between loops and vectorized code.
The inputs to the function are an image, the size (number of rows and f (x) = A s i n ( x / 2 ~ )
columns) of the subimage we want to extract, and the coordinates of the top, for x = 0 , 1 , 2 , . . . , M - 1.A f o r loop to implement this computation is
left corner of the subimage. Keep in mind that the image origin in MATLAB
is at (1, I ) , as discussed in Section 2.1.1. for x = 1 :M % Array i n d i c e s i n MATLAB cannot be 0 .
f ( x ) = A*sin((x - 1 ) / ( 2 * p i ) ) ;
function s = subim(f, m, n, r x , cy) end
%SUBIM E x t r a c t s a subimage, s , from a given image, f .
% The subimage i s of s i z e m-by-n, and t h e coordinates However, this code can be made considerably more efficient by vectorizing it;
% of i t s t o p , l e f t corner a r e ( r x , c y ) .
that is, by taking advantage of MATLAB indexing, as follows:
s = zeros(m, n ) ;
rowhigh = rx + m - 1 ;
colhigh = cy + n - 1 ;
xcount = 0;
f o r r = rx:rowhigh As this simple example illustrates, 1-D indexing generally is a simple
xcount = xcount + 1 ; process. When the functions to be evaluated have two variables, optimized
ycount = 0 ;
indexing is slightly more subtle. MATLAB provides a direct way to implement
2-D function evaluations via function meshgrid, which has the syntax
'Unlike the C language switch construct. MATLAB's s w i t c h does not "fall through."That is, s w i t c h
executes only the f~rstmatch~ngcase: subsequent matching cases do not execute.Therefore, break state-
ments are not used. [ C , R ] = meshgrid(c, r )
-
56 Chapter 2 Fundamentals 2.10 B Introduction to M-Function Programming 57
This function transforms the domain specified by row vectors c and r into ar- a In this example we write an M-function to compare the implementation of EXAMPLE 2.13:
rays C and R that can be used for the evaluation of functions of two variables the following two-dimensional image function using f o r loops and vectorization: An illustration of
and 3-D surface plots (note that columns are listed first in both the input and the computational
f (x, y) = A sin(uox + voy) advantages of
output of meshgrid). vectorization,and
The rows of output array C are copies of the vector c, and the columns of for x = 0,1,2,. . . , M - 1 and y = 0 , 1 , 2 , . . . , N - 1. We also introduce the intruduction of
the output array R are copies of the vector r. For example, suppose that we the timing
timing functions t i c and t o c .
want to form a 2-D function whose elements are the sum of the squares of the functions t i c and
The function inputs are A, uo, vo, M and N.The desired outputs are the im- toc.
values of coordinate variables x and y for x = 0, 1 , 2 and y = 0, 1. The vec- ages generated by both methods (they should be identical), and the ratio of
tor r is formed from the row components of the coordinates: r = [ 0 1 21. Sim- the time it takes to implement the function with f o r loops to the time it takes
ilarly, c is formed from the column component of the coordinates: c = [O 1 ] to implement it using vectorization. The solution is as follows:
(keep in mind that both r and c are row vectors here). Substituting these two
vectors into m e s h g r i d results in the following arrays: f u n c t i o n [ r t , f , g ] = t w o d s i n ( A , uO, vO, M, N)
%TWOCISIN Compares f o r l o o p s vs. v e c t o r i z a t i o n .
>> [C, R ] = m e s h g r i d ( c , r ) % The comparison i s based on i m p l e m e n t i n g t h e f u n c t i o n
C = % f ( x , y ) = A s i n ( u 0 x + vOy) f o r x = 0, 1, 2, ..., M - 1 and
% y = 0, 1, 2, . . . , N - 1 . The i n p u t s t o t h e f u n c t i o n a r e
% M and N and t h e c o n s t a n t s i n t h e f u n c t i o n .
% F i r s t implement u s i n g f o r l o o p s .
f o r r = 1:M
uOx = u O * ( r - 1 ) ;
for c = l:N
voy = vO*(c - 1 ) ;
The function in which we are interested is implemented as f ( r , c ) = A * s i n ( u O x + vOy);
end
end
tl = toc; % End t i m i n g .
which gives the following result: % Now implement u s i n g v e c t o r i z a t i o n . C a l l t h e image g .
tic % Start timing.
r = O : M - 1;
C = 0:N - 1;
[C, R] = m e s h g r i d ( c , r ) ;
9 = A*sin(uO*R + vO*C);
Note that the dimensions of h are l e n g t h ( r ) x l e n g t h ( c ) . Also note, for ex- t 2 = toc; % End t i m i n g .
ample, that h ( 1 , 1 ) = R ( 1 , 1 ) ^ 2 + C( 1 , 1 ) ^2. Thus, MATLAB automatically % Compute t h e r a t i o o f t h e t w o t i m e s .
took care of indexing h. This is a potential source for confusion when 0s are in-
volved in the coordinates because of the repeated warnings in this book and in r t = t l l ( t 2 + e p s ) ; % Use eps i n case t 2 is c l o s e t o 0.
manuals that MATLAB arrays cannot have 0 indices. As this simple illustra-
tion shows, when forming h, MATLAB used the contents of R and C for com- Running this function at the MATLAB prompt,
putations. The indices of h, R, and C, started at 1. The power of this indexing
scheme is demonstrated in the following example. >> [ r t , f , g ] = t w o d s i n ( 1 , 1 / ( 4 * p i ) , 1 / ( 4 * P i ) , 512, 5 1 2 ) ;
58 Chapter 2 a Fundamentals 2.1 0 &
: Introduction to M-Function Programming 59
FIGURE 2.7
Sinuso~dalimage
generated In where f is the image from which the region is to be extracted.The for loops to
Example 2.13.
the same thing were already worked out in Example 2.12. Imple-
rnenting both methods and timing them as in Example 2.13 would show that
the vectorized code runs on the order of 1000 times faster in this case than the
code based on for loops.
preallocating Arrays
Another simple way to improve code execution time is to preallocate the size
of the arrays used in a program. When working with numeric or logical arrays,
preallocation simply consists of creating arrays of 0s with the proper dimen-
sion. For example, if we are working with two images, f and g, of size
1024 X 1024 pixels, preallocation consists of the statements
yielded the following value of rt:
Figure 2.7 shows the result. &# we establish a foundation for writing such functions. ~nterfaces(GUIJ).
Function disp is used to display information on the screen. Its syntax is
The vectorized code in Example 2.13 runs on the order of 30 times faster
than the implementation based on for loops. This is a significant computation-
al advantage that becomes increasingly meaningful as relative execution times
become longer. For example, if M and N are large and the vectorized program If argument is an array, disp displays its contents. If argument is a text string,
takes 2 minutes to run, it would take over 1 hour to accomplish the same task then d i s p displays the characters in the string. For example,
using for loops. Numbers like these make it worthwhile to vectorize as much of
a program as possible, especially if routine use of the program in envisioned. > ' A = [ I 2; 3 4 1 ;
The preceding discussion on vectorization is focused on computations in- >> disp (A)
volving the coordinates of an image. Often, we are interested in extracting and
processing regions of an image. Vectorization of programs for extracting such
regions is particularly simple if the region to be extracted is rectangular and
encompasses all pixels within the rectangle, which generally is the case in this >> sc = 'Digital Image Processing.';
type of operation.Tne basic vectorized code to extract a region, s,of size m x n >> disp(sc)
and with its top left corner at coordinates (rx, cy) is as follows: Digital Image Processing.
rowhigh = rx + rn - 1; >> disp('This is another way to display text.')
colhigh = cy + n - 1 ; This is another way to display text.
60 Chapter 2 i# Fundamentals 2.10 w Introduction to M-Function Programming 61
Note that only the contents of argument are displayed, without words like
ans =, which we are accustomed to seeing on the screen when the value of a
variable is displayed by omitting a semicolon at the end of a command line.
Function input is used for inputting data into an M-function. The basic
syntax is
ans =
input double
+ i\I
This function outputs the words contained in message and waits for an input
Thus, we see that t is a 1 X 5 character array (the three numbers and the two
from the user, followed by a return, and stores the input in t.The input can be
spaces) and n is a 1 X 3 vector of numbers of class double.
a single number, a character string (enclosed by single quotes), a vector (en-
If the entries are a mixture of characters and numbers, then we use one of
closed by square brackets and elements separated by spaces or commas), a
MATLAB'S string processing functions. Of particular interest in the present
matrix (enclosed by square brackets and rows separated by semicolons), or
discussion is function s t r r e a d , which has the syntax
any other valid MATLAB data structure. The syntax
[ a , b, c , . .. ] = s t r r e a d ( c s t r , ' f o r m a t ' , 'param', 'value' )
' :d4
a,3;;ytrread
,+
-,
,(, 1
outputs the contents of message and accepts a character string whose ele- This function reads data from the character string c s t r , using a specified se,~hehe[ppagefor
ments can be separated by commas or spaces. This syntax is flexible because it format and param/value combinations. In this chapter the formats of interest s t r r e a d fora ltst o f
allows multiple individual inputs. If the entries are intended to be numbers, the are %f and %q,to denote floating-point numbers and character strings, respec- ~ ~ ~
elements of the string (which are treated as characters) can be converted to tively. For param we use d e l i m i t e r to denote that the entities identified in th,sfin,t,on,
numbers of class double by using the function str2num, which has the syntax format will be delimited by a character specified in value (typically a comma
or space). For example, suppose that we have the string
Note the use of a dot to append the various fields to the structure variable.
2.1 0.6 A Brief Introduction to Cell Arrays and Structures Then, for example, typing S. matrix at the prompt, would produce
When dealing with mixed variables (e.g., characters and numbers), we can
Cell arrays and make use of cell arrays. A cell array in MATLAB is a multidimensional array >> S - m a t r i x
are dis-
srrLtctLLres whose elements are copies of other arrays. For example, the cell array ans =
cussed in detail in
Section 11.1.1. 1 0
c = { ' g a u s s ' , [ I 0 ; 0 11, 3)
34 Chapter 2 Fundamentals
which agrees with the corresponding output for cell arrays. The clarity of using
S.mat r i x as opposed to c(2) is evident in this case. This type of readability
can be important if a function has numerous outputs that must be interpreted
by a user.
Summary
tions
The material in this chapter is the foundation for the discussions that follow. At this
point, the reader should be able to retrieve an image from disk, process it via simple
manipulations, display the result, and save it to disk. It is important to note that the key
lesson from this chapter is how to combine MATLAB and IIT functions with pro-
gramming constructs to generate solutions that expand the capabilities of those func-
tions. In fact,this is the model of how material is presented in the following chapters. By
combining standard functions with new code, we show prototypic solutions to a broad
spectrum of problems of interest in digital image processing.
term spatial domain refers to the image plane itself, and methods in this cat-
are based on direct manipulation of pixels in an image. In this chapter we
ghout the book, however, these techniques are general in scope and
umerous other branches of digital image processing.
Background
As noted in the preceding paragraph, spatial domain techniques operate di-
rectly on the pixels of an image. The spatial domain processes discussed in this
chapter are denoted by the expression
where f (x,y ) is the input image, g(x,y ) is the output (~rocessed)image, and
T is an operator on f , defined over a specified neighborhood about point
(x, y). In addition, Tcan operate on a set of images, such as performing the ad-
dition of K images for noise reduction.
The principal approach for defining spatial neighborhoods about a point
( x , y ) is to use a square or rectangular region centered at (x,y), as Fig. 3.1 shows.
The center of the region is moved from pixel to pixel starting, say, at the top, left
66 Chapter 3 a;: Intensity Transformations and Spatial Filtering 3.2 r Intensity Transformation Functions 67
FIGURE 3.1 A Or~gin a b c
ne~ghborhoodof
slze 3 X 3 about d FIGURE 3.2 The
po~nt(x. y ) In an various mappings
Image. available in
functlon
lmad] ust.
slues between low-out and high-out. Values below low-in and above
igh-in are clipped; that is, values below low-in map to low-out, and those
bove high-in map to high-out. The input image can be of class uint8,
t16, or double, and the output image has the same class as the input. All
ts to function imad just, other than f, are specified as values between 0
1,regardless of the class of f. If f is of class uint8, imad j ust multiplies
--ller, and, as it moves, it encompasses different neighborhoods. Operator T 1s values supplied by 255 to determine the actual values to use; if f is of class
appliec, ^.tch locat~on(x,y) to yield the output, g, at that location. Only the t 1 6, the values are multiplied by 65535. Using the empty matrix ( [ 1) for
pixels in the n~,, ' -?hood are used in computing the value of g at ( x , Y ) . [low-in high-in] or for [low-out high-out] results in the default values
The remainder o i t h ~ s, -?ter deals with various implementations of the 0 1 1.If high-out is less than low-out, the output intensity is reversed.
preceding equation. Although LIII, ;-+ion is simple conceptually, its compu- Parameter gamma specifies the shape of the curve that maps the intensity
tational implementation in MATLAB requllcb i i ; ~ : careful attention be paid alues in f to create g. If gamma is less than 1,the mapping is weighted toward
to data classes and value ranges. igher (brighter) output values, as Fig. 3.2(a) shows. If gamma is greater than 1,
e mapping is weighted toward lower (darker) output values. If it is omitted
Intensity Transformation Functions om the function argument, gamma defaults to 1 (linear mapping).
The simplest form of the transformation T is when the neighborhood in
Fig. 3.1 is of size 1 X 1 (a single pixel). In this case, the value of g at (x, y ) de-
a Figure 3.3(a) is a digital mammogram image, f , showing a small lesion, and EXAMPLE 3.1:
Fig. 3.3(b) is the negative image, obtained using the command Using function
pends only on the intensity o f f at that point, and T becomes an intensity or imad j ust.
gray-level transformation function. These two terms are used interchangeably,
when dealing with monochrome (i.e., gray-scale) images. When dealing with
color images, the term intensity is used to denote a color image component in This process, which is the digital equivalent of obtaining a photographic nega-
certain color spaces, as described in Chapter 6. tive, is particularly useful for enhancing white o r gray detail embedded in a
Because they depend only on intensity values, and not explicitly on ( x , y ) , large, predominantly dark region. Note, for example, how much easier it is to
intensity transformation functions frequently are written in simplified form as analyze the breast tissue in Fig. 3.3(b). The negative of an image can be ob-
tained also with IPT function ~mcomplement:
where r denotes the intensity o f f and s the intensity of g, both at any corre-
sponding point (x,y ) in the images.
3.2. ! Function imad j u s t 1 Figure 3.3(c) is the result of using the command
Function imad j ust is the basic IPT tool for intensity transformations of gray- >> 9 2 = ~madjust(f, [0.5 0.751, [ 0 I]);
scale images. It has the syntax
which expands the gray scale region between 0.5 and 0.75 to the full [O, 11
lmad] u s t g = imad]ust(f, [low-in high-in], [low-out high-out], gamma) range. This type of processing is useful for highlighting an intensity band of
interest. Finally, using the command
As illustrated in Fig. 3.2. this function maps the intensity values in image f
to new values in g, such that values between low-in and high-in map to
68 Chapter 3 s Intensity Transformations and Spatial Filtering 3.2 S Intensity Transformation Functions 69
ne of the principal uses of the log transformation is to compress dynamic
. For example, it is not unusual to have a Fourier spectrum (Chapter 4)
FIGURE 3.3 (a) values in the range [O, lo6] or higher. When displayed on a monitor that is
Orlglnal dlgltal linearly to 8 bits, the high values dominate the display, resulting in lost
mammogram detail for the lower intensity values in the spectrum. By computing the
(b) Negatlve
Image. (c) Result g, a dynamic range on the order of, for example, lo6, is reduced to approxi-
of expanding the tely 14, which is much more manageable.
~ntensityrange en performing a logarithmic transformation, it is often desirable to
[0 5.0.751 resulting compressed values back to the full range of the display. For
(d) Result of easiest way to do this in MATLAB is with the statement
enhancing the
Image w ~ t h
gamma = 2.
(Original image
zourtesy of G. E.
Medical Systems.) e of mat2gray brings the values to the range [0, 11 and im2uint8 brings
m to the range [O, 2551. Later, in Section 3.2.3, we discuss a scaling function
t automatically detects the class of the input and applies the appropriate
where r represents the intensities of the input image, s the corresponding in-
tensity values in the output image, and E controls the slope of the function.
This equation is implemented in MATLAB for an entire image as
<A
,4f
produces a result similar to (but with more gray tones than) Fig. 3.3(c) by compress- g = 1 . / ( 1 + (m./(double(f) + eps)).^E) -:e p c
* '
ing the low end and expanding the high end of the gray scale [see Fig. 3.3(d)]. @
-
curve shown in Fig. 3.2(a) with the low values set at 0 and the high values set to
l o g 1 0 (ire tlzr hose 2
bl~se10 logn-
I I ~ Z ~ 1 on both scales. Note, however, that the shape of the gamma curve is variable,
itlltlls, respectively whereas the shape of the log function is fixed. Dark +-+ Light Dark Light
70 Chapter 3 :6 Intensity Transformations and Spatial Filtering 3.2 is Intensity Transformation Functions
Note the use of eps (see Table 2.10) to prevent overflow i f f has any 0 values dling a Variable Number of Inputs and/or Outputs
Since the limiting value of T ( r )is 1.output values are scaled to the range [O,1]
eck the number of arguments input into an M-function we use function
when working with this type of transformation. The shape in Fig. 3.4(a) was
obtained with E = 20.
n = nargin
EXAMPILE 3.2: @ Figure 3.5(a) is a Fourier spectrum with values in the range 0 to 1.5 X lo6*
Using a log displayed on a linearly scaled, 8-bit system. Figure 3.5(b) shdws the result obi ch returns the actual number of arguments input into the M-function. Sim-
transforn'ation tained using the commands
reduce dynamic ly, function n a r g o u t is used in connection with the outputs of an M-
range.
>> g = i m 2 u i n t 8 ( m a t 2 g r a y ( l o g ( l + d o u b l e ( f ) ) ) ) ;
>> imshow(g)
n = nargout ut
a b
FIGURE 3.5 (a) A
Four~erspectrum
(b) Rertult
obta~ncd by
perfornnlng a log
transfo rmallon Typing
>> t e s t h v 2 ( 6 ) ;
which only has one input argument would produce the error
Often, it is useful to be able to write functions in which the number of input This function converts image f to the class specified in parameter newclass
and/or output arguments is variable. For this, we use the variables varargin md outputs it as g. Valid values for newclass are ' u i n t 8 ' , ' u i n t l 6 ' ,
A 'larargln and varargout. In the declaration, varargln and varargout must be lower- a n d ' d ~ ~ b' .l e
,varargOut case. For example, Note m the following M-function, which we call l n t rans, how function op-
tions are formatted in the Help section of the code. how a variable number of
function [m, n ] = t e s t h v 3 ( v a r a r g i n ) inputs is handled, how error checking is interleaved in the code, and how the
class of the output image is matched to the class of the input. Keep in mind
accepts a variable number of inputs into function testhv3, and when studying the following code that varargin is a cell array, so its elements
are selected by using curly braces.
f u n c t i o n [varargout] = testhv4(m, n, p )
function g = ~ntrans(f,varargln) lritrans
a - - -
returns a variable number of outputs from function testhv4. If function %INTRANS Performs lntenslty (gray-level) transformatlons.
t e s t hv3 had, say, one fixed input argument, x, followed by a variable number % G = INTRANS(F, 'neg') computes the negatlve of Input Image F.
of input arguments, then %
% G = INTRANS(F, ' l o g ' , C, CLASS) computes C*log(l + F ) and
% multlplles the result by (posltlve) constant C. If the last t w o
function [ m , n ] = t e s t h v 3 ( x , v a r a r g i n ) % parameters are omltted, C defaults to 1 . Because the l o g 1s used
% frequently to dlsplay Fourler spectra, parameter CLASS offers the
would cause varargin to start with the second input argument supplied by % optlon to speclfy the class of the output as 'ulnt8' or
the user when the function is called. Similar comments apply to varargout. It % 'ulntl6'. If parameter CLASS 1s omltted, the output 1s of the
is acceptable to have a function in which both the number of input and output % same class as the lnput.
arguments is variable. %
When varargin is used as the input argument of a function, MATLAB sets it % G = INTRANS(F, 'gamma', GAM) performs a gamma transformatlon on
to a cell array (see Section 2.10.5) that accepts a variable number of inputs by the % the Input Image uslng parameter GAM ( a requlred ~ n p u t ) .
user. Because varargin is a cell array, an important aspect of this arrangement is %
that the call to the function can contain a mixed set of inputs. For example, as- % G = INTRANS(F, 'stretch', M , E ) computes a contrast-stretching
suming that the code of our hypothetical function t e s t h v 3 is equipped to handle % transformatlon uslng the expression 1. / ( I + ( M . I ( F +
it, it would be perfectly acceptable to have a mixed set of inputs, such as % eps)). ^ E ) . Parameter M must be l n the range [ 0 , I ] . The default
% value for M IS mean2(lm2double(F)),and the default value for E
2> [ m , n] = t e s t h v 3 ( f , [ 0 0.5 1.51, A, 'label'); % is 4.
%
where f is an image, the next argument is a row vector of length 3, A is a ma- % For the ' n e g ' , 'gamma', and 'stretch' transformatlons, double
trix, and ' l a b e l ' is a character string. This is indeed a powerful feature that % lnput lmages whose maxlmum value 1s greater than 1 are scaled
% flrst uslng MAT2GRAY. Other lmages are converted to double flrst
can be used to simplify the structure of functions requiring a variety of differ-
% uslng IM2DOUBLE. For the ' l o g ' transformatlon, double lmages are
ent inputs. Similar comments apply to varargout.
% transformed wlthout berng scaled; other Images are converted to
% double flrst uslng IM2DOUBLE.
Another M-Function for Intensity Transformations %
In this section we develop a function that computes the following transforma- % The output 1s of the same class as the ~nput,except ~ . fa
changeclass is an tion functions: negative, log, gamma and contrast stretching. These transforma- % different class 1s speclfled for the ' l o g ' optlon.
,~nducumeniedIPT
lrility function, Its tions were selected because we will need them later, and also to illustrate the % Verlfy the correct number of lnputs.
:.ode is included in mechanics involved in writing an M-function for intensity transformations. In error(nargchk(2, 4 , nargln))
Appendix C. writing this function we use function changeclass, which has the syntax
% Store the class of the i n p u t for use later
+ changeclass
74 Chapter 3 si Intensity Transformations and Spatial Filtering 3.2 ,3 Intensity Transform;!tio on Functions 75
% If the i n p u t i s of class double, and i t i s outside the range ASan illustration of function i n t rans, consider the image in Fig. 3.6(a), which EXAMPLE 3.3:
% [O, I ] , and the specified transformation i s not ' l o g ' , convert the an ideal candidate for contrast stretching to enhance the skeletal structure.The Illustration of
" a n p u t to the range (0, 11. result in Fig. 3.6(b) was obtained with the following call to i n t r a n s : function intrans.
i f strcmp(class(f), 'double') & max(f(:)) > 1 & . . .
-strcmp(varargin{l), ' l o g ' ) >> g = i n t r a n s ( f , ' s t r e t c h ' , m e a n Z ( i m 2 d o u b l e ( f ) ) , 0 . 9 ) ;
f = mat2gray ( f ) ; f i g u r e , imshow(g)
else % Convert to double, regardless of class(f).
22
f = im2double ( f ) ; m = mean2(A)
A 111ec117
end how function mean2 was used to compute the mean value of f directly C O I ~ I I I M 111e
(average) vallte of
% Determine the type of transformation specified. the function call. The resulting value was used for m. Image f was con- tile c l e ~ ~ ~ eof/ l t s
method = varargin{l) ; to double using im2double in order to scale its values to the range t,?trlrix A.
11 so that the mean would also be in this range, as required for input m. The
% Perform the intensity transformation specified.
switch method ue of E was determined interactively. 1
case ' neg '
g = imcomplement( f ) ; M-Function for Intensity Scaling
case ' l o g ' en working with images, results whose pixels span a wide negative to posi-
i f length (varargin) == 1 range of values are common. While this presents no problems during in-
c = 1; mediate computations, it does become an issue when we want to use an
elseif length(varargin) == 2 it or 16-bit format for saving or viewing an image, in which case it often is
c = varargin{2}; sirable to scale the image to the full, maximum range, [O, 2551 or [O, 655351.
elseif length(varargin) == 3 following M-function, which we call g s c a l e , accomplishes this. In addi-
c = varargin{2); ,the function can map the output levels to a specified range. The code for
classin = vararginI3);
function does not include any new concepts so we d o not include it here.
else
end
error('1ncorrect number of inputs for the log option.') 4 See Appendix C for the listing.
g = c*(log(l + double(f))); a b
case 'gamma' FIGURE 3.6 (a)
i f length(varargin) < 2 Bone scdn lmage
error('Not enough inputs for the gamma option.') (b) Image
end enhanced uslng d
gam = vararginI2); contrast-stretching
g = imadjust(f, [ 1 , [ 1, gam);
transformat~on
(Or~glnalImage
case 'stretch' courtesy of G. E
i f length(varargin) == 1 Medlcal System5 )
% Use defaults.
m = meanZ(f);
E = 4.0;
elseif length (varargin) == 3
m = varargini2);
E = varargin{3);
else error('1ncorrect number of inputs for the stretch option. ' )
end
g = l . / ( l + (m./(f + e p s ) ) . ^ E ) ;
otherwise
error( 'Unknown enhancement method. ' )
end
% Convert to the class of the input image.
g = changeclass(classin, g ) ;
76 Chapter 3 % Intensity Transformations and Spatial Filtering 3.3 r Histogram Processing and Function Plotting 77
Vanous ways to re s i z e is the font size in points. Text can be added to the body of the fig-
4 10000
plot an Image by using function t e x t , as follows:
histogram. 3 8000
(a) l m h l s t , 6000 text(xloc, yloc, ' t e x t s t r i n g ' , ' f o n t s i z e ' , s i z e )
(b) b a r , 2
(c) stem, 1 4000
re x l o c and y l o c define the location where text starts. Use of these three
(dl P l o t 2000
0 ctions is illustrated in Example 3.5. It is important t o note that functions
0 50 100 150 200 250 0' 50 100 150 200 250 set axis values and labels are used after the function has been plotted.
title can be added to a plot using function t i t l e , whose basic syntax is
title('titlestringl)
here v is row vector containing the points to b e plotted, and h o r z is as de- See the stem help
ribed for bar.The argument, page for additional
options available for
this function.
color~linestyle~marker
>> h = i m h i s t ( f ) ;
>> hl = h ( 1 : 1 0 : 2 5 6 ) ; a triplet of values from Table 3.1. For example, stem ( v , ' r- -s ' ) produces
>> horz = 1:10:256; m plot where the lines and markers are red, the lines are dashed, and the
>> bar(horz, h l ) rs are squares. I f f ill is used, and the marker is a circle, square, or dia-
>> a x i s ( [ O 255 0 150001) the marker is filled with the color specified in c o l o r . The default color
>> s e t ( g c a , ' x t i c k ' , 0:50:255) ack, the line default is s o l i d , and the default marker is a c i r c l e . The
ytick >> s e t ( g c a , ' y t i c k ' , 0:2000:15000) m graph in Fig. 3.7(c) was obtained using the statements
Figure 3.7(b) shows the result.The peak located at the high end of the intensi- h = imhist ( f ) ;
ty scale in Fig. 3.7(a) is missing in the bar graph as a result of the larger hori- hl = h ( 1 : 1 0 : 2 5 6 ) ;
zontal increments used in the plot.
The fifth statement in the preceding code was used to expand the lower
range of the vertical axis for visual analysis, and to set the orizontal axis to the
same range as in Fig. 3.7(a).The a x i s function has the syntax TABLE 3.1
Symbol Color Symbol Line Style Symbol Marker Attributes for
axis([horzmin horzmax vertmin vertmax]) k Black - Solid + Plus sign functions stem and
w White -- Dashed o Circle plot.The none
I- Red Dotted * Asterisk attribute is
which sets the minimum and maximum values in the horizontal and vertical
9 Green - Dash-dot Point applicable only to
axes. In the last two statements, gca means "get current axis," (i.e., the axes of function p l o t , and
the figure last displayed) and x t i c k and y t i c k set the horizontal and vertical b Blue none No line x Cross
c Cyan s Square must be specified
axes ticks in the intervals shown. individually. See the
Y Yellow d Diamond
Axis labels can be added to the horizontal and vertical axes of a graph using m Magenta none No marker syntax for function
the functions p l o t below.
80 Chapter 3 Intensity Transformationsand Spatial Filtering 3.3 Histogram Processing and Function Plotting 81
>> horz = 1:10:256; ping hold on at the prompt retains the current plot and certain axes
>> stem(horz, h l , 'fill') rties so that subsequent graphing commands add to the existing graph. on
>> axis([O 255 0 150001) e Example 10.6 for an illustration.
>> set(gca, 'xtick', [0:50:255])
>> set(gca, 'ytick', [0:2000:15000])
.2 Histogram Equalization
Finally, we consider function plot,which plots a set of points by linkin e for a moment that intensity levels are continuous quantities normal-
them with straight lines. The syntax is o the range [0, 11, and let p,(r) denote the probability density function
f i A ~ ~
) of the intensity levels in a given image, where the subscript is used for
,26, ,. ,\ rentiating between the PDFs of the input and output images. Suppose
~:,:$"
>A;.,' pldt plot(horz, v, 'color-linestyle-marker')
., , . .
.> we perform the following transformation on the input levels to obtain
where the arguments are as defined previously for stem plots. The values 0 put (processed) intensity levels, s,
seetl,e ,,lothelp
page for additional color,linestyle,and marker are given in Table 3.1.As in stem,the attribut
options available for
this firnction.
in plot can be specified as a triplet. When using none for linestyle or
marker,the attributes must be specified individually.For example, the comrn
s = T(r)=
.Ir
p,(w) dw
plots green squares without connecting lines between them. The defaults fo
1 forOSss1
plot are solid black lines with no markers.
The plot in Fig. 3.7(d) was obtained using the following statements: ps(s) = { 0 otherwise
>> h = imhist(f);
ther words, the preceding transformation generates an image whose in-
>> plot(h) % Use the default values. ity levels are equally likely, and, in addition, cover the entire range [O,l].
>> axis([O 255 0 150001) net result of this intensity-level equalization process is an image with in-
>> set(gca, 'xtick', [0:50:255]) ased dynamic range, which will tend to have higher contrast. Note that
>> set(gca, 'ytick', [0:2000:15000]) ransformation function is really nothing more than the cumulative dis-
tion function (CDF).
Function plot is used frequently to display transformation functions (see hen dealing with discrete quantities we work with histograms and call
Example 3.5). preceding technique histogram equalization, although, in general, the
togram of the processed image will not be uniform, due to the discrete na-
In the preceding discussion axis limits and tick marks were set manually. It re of the variables. With reference to the discussion in Section 3.3.1, let
is possible to set the limits and ticks automatically by using functions ylim and (r,), j = 1 , 2 , .. . ,L, denote the histogram associated with the intensity lev-
xlim,which, for our purposes here, have the syntax forms s of a given image, and recall that the values in a normalized histogram are
pproximations to the probability of occurrence of each intensity level in the
ylim('autol) mage. For discrete quantities we work with summations, and the equaliza-
xlim('autol) tion transformation becomes
Among other possible variations of the syntax for these two functions (see on- Sk = T ( r k )
line help for details), there is a manual option, given by k
= C, pr(rj)
ylim([yrnin ymax]) j= I
xlim([xmin xmaxl)
= 2;ni
k
]=I
which allows manual specification of the limits. If the limits are specified for
only one axis, the limits on the other axis are set to ' auto ' by default. We US .
k = 1.2,. . . L. where sk is the intensity value in the output (processed)
image corresponding to value rk in the input image.
these functions in the following section.
82 Chapter 3 4: ltensity Transformations and Spatial Filtering 3.3 &Histogram
!
i Processing and Func:tion Plotting 83
g = h i s t e q ( f , hspec)
Input intensity values
Phobos. = 2 * (sigl a 2 ) ;
(b) Hlstogram. = A2 * (1 1 ( ( 2 * pi) 0.5) * sig2);
A
(c) Histogram-
equalized Image.
= 2 (sig2 2 ) ;
(d) Hlstogram = linspace(0, 1, 256) ;
of (c). = k t cl exp(-((2 - mi) . ^ 2) .I kl) + ...
(Original image
courtesy of
NASA).
following interactive function accepts inputs from a keyboard and plots
ulting Gaussian function. Refer to Section 2.10.5 for a n explanation of
unctions i n p u t and str2num. Note how the limits of the plots are set.
disp('1ncorrect number of inputs. ' ) l l ( b ) shows the result. The improvement over the histogram-
continue
end
p = twomodegauss(v(l), v ( 2 ) , v(3), v ( 4 ) , v(5), v(6), v ( 7 ) ) ;
% Start a new figure and scale the axes. Specifying only x l i m
% leaves y l i m on auto.
figure, plot ( p )
x l i m ( [O 2551 )
end
as extreme as the shift in the histogram shown in Fig. 3.10(d), which corre-
n d to
~ the poorly enhanced image of Fig. 3.10(c). El
a b
C
FIGURE 3.1 1
(a) Specified
histogram.
(b) Result of 4.1 Linear Spatial Filtering
enhancement by
h~stogram
matching. processing in the frequency domain, a topic discussed in detail in
( c ) Histogram . In the present chapter, we are interested in filtering operations that
of (b).
0 50 100 150 200 250
e linear operations of interest in this chapter consist of multiplying each
in the neighborhood by a corresponding coefficient and summing the re-
to obtain the response at each point (x, y). If the neighborhood is of size
n , m n coefficients are required.The coefficients are arranged as a matrix,
m e l , template, or window, with the first three
r reasons that will become obvious shortly,
90 Chapter 3 Lntensity Transformations and Spatial Filtering 3.4 ioa Spatial Filtering 91
FIGURE 3.12 The ure 3.13(a) shows a one-dimensional function, f , and a mask, w. The ori-
mechanics of linear f is assumed to be its leftmost point. To perform the correlation of the
spatial filtering. unctions, we move w so that its rightmost point coincides with the origin
The magnified
drawing shows a ,as shown in Fig. 3.13(b). Note that there are points between the two func-
3 x 3 mask and s that do not overlap. The most common way to handle this problem is to
the corresponding f with as many 0s as are necessary to guarantee that there will always be
image ursion of w past f . This situation is shown
neighborhood
directly under it.
The neighborhood elation. The first value of correlation
is shown displaced o functions in the position shown in
out from under the case. Next, we move w one location
mask for ease of .13(d)]. The sum of products again is
ncounter the first nonzero value of the
74-1. -1) ~(-1,oj lo(-^, I ) proceed in this manner until w moves
is shown in Fig. 3.13(f)] we would get
s is the correlation of w and f . Note
W(O.- 1) ~(0.0) ~(0.1) ved f past w instead, the result
Id have been different, so the order matters.
0 0 0 1 0 0 0 0 Ci)
0 2 3 2 1
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 (k)
0 2 3 2 1
0 0 0 0 0 1 0 0 0 0 0 0 0 0 (I)
0 2 3 2 1
t Position after one shift
)OOOOOOO1OOOOOOOO OOOOOOO1OOOOOOOO(m)
are nonnegative integers.Al1 this says is that our principal focus is on mask 1 2 3 2 0 0 2 3 2 1
odd sizes, with the smallest meaningful size being 3 x 3 (we exclude from t. Position after four shifts
discussion the trivial case of a 1 X 1 mask). Although it certainly is not a
quirement, working with odd-size masks is more intuitive because they ha 0 0 0 0 0 1 0 0 0 0 0 0 0 0 (n)
unique center point. 0 2 3 2 1
There are two closely related concepts that must be understood cl
when performing linear spatial filtering. One is correlation; the other ' f u l l ' correlation result ' f u l l ' convolution result
convolution. Correlation is the process of passing the mask w by the 0 0 0 1 2 3 2 0 0 0 0 0 (0)
array f in the manner described in Fig. 3.12. Mechanically, convolution
same process, except that w is rotated by 180" prior to passing it by f . ' same ' correlation result ' same ' convolution result
two concepts are best explained by some simple examples. 0 0 2 3 2 1 0 0 0 1 2 3 2 0 0 0 (PI
92 Chapter 3 @ Intensity Transformations and Spatial Filtering 3.4 &! Spatial Filtering 93
The label ' f u l l ' in the correlation shown in Fig. 3.13(g) is a flag (to be di Padded f FIGLYRE 3.1 4
cussed later) used by the toolbox to indicate correlation using a pad o 11 o o o 11 0 11 o Illustrat~onof
I) 0 11 o (I 0 (I o 11 two-dimens~onal
and computed in the manner just described.The toolbox provides another
0 !I il 0 !j 11 0 0 0 correlation and
tion, denoted by ' same ' [Fig. 3.13(h)] that produces a correlation that is 0 (I !I i~(I (I (I o (I convolution.The
same size as f . This computation also uses zero padding, but the starting p 0 11 o o 1 I! o o o 0s are shown In
tion is with the center point of the mask (the point labeled 3 in w) aligned i j 0 1 0 il 12 3 0 0 il C! ii 0 ti O 0 gray to simplify
the origin of f . The last computation is with the center point of the (1 i 0 1 i 4 5 6 0 li O O 0 0 0 !I 0 viewing.
aligned with the last point in f . 11 o o ti 7 8 9 11 o (J [I 11 (I (I o
To perform convolution we rotate w by 180" and place its rightmost point (b)
the origin o f f , as shown in Fig. 3.13(j). We then repeat the sliding/computin
process employed in correlation, as illustrated in Figs. 3.13(k) through (n ' correlation result ' same ' correlation result
O 0 I1 i) 0 0 0 I! 0 0 0 0 0 !I 0
' f u l l ' and ' same ' convolution results are shown in Figs. 3.13(0) and ( 14 5 61 11 0 0 i! 0 i! 11 0 (1 il 0 [j IJ 0 i 1 0 9 8 7 0
spectively. i7-8-9; 0 ij 0 0 0 0 0 11 I) 0 0 0 I) 0 [I 0 6 5 4 0
Function f in Fig. 3.13 is a discrete unit impulse function that is 1 at on 0 I) 0 0 0 0 0 0 0 o o !I 9 8 7 (I (i i) 0 3 2 1 0
i) 0 0 0 1 0 0 i) 11 0 0 11 6 5 4 11 (I II 0 0 0 0 0
location and 0 everywhere else. It is evident from the result in Figs. 3.13 OIJOO!lllOOO 0 I l 0 3 2 1 0 0 0
(p) that convolution basically just "copied" w at the location of the impul O O I ) O ~ I O I I O ~ ~ ) ( ] I ~ O I I ~ ~ ( J I I
This simple copying property (called sifting) is a fundamental concept in 1 OII!)IIOII~)~IO (IOOOOI'IO~IO
J 0 0 1 1 0 l ~ O ~ l 0 O i l ~ O i ~
ear system theory, and it is the reason why one of the functions is alw
tated by 180" in convolution. Note that, unlike correlation, reversing th (4 (el
order of the functions yields the same convolution result. If the functio ' full ' convolution result ' same ' convolution result
being shifted is symmetric, it is evident that convolution and correlatio 7 0 O I I O I I O I ~ ~ I I ~ 11 !I o ri o
yield the same result. 16 5 41 1) 11 oo 0 11 I> 0 II o
o 0 o 0 0 1 2 3 11
The preceding concepts extend easily to images, as illustrated in Fi L3- ~ - 1 ~ ~ 1 0 1 1 i I i 1 0~ ~ I I ~ ) I J ( ] o ~ ) o oo 4 5 6 0
li (1 0 O 0 O 0 0 O 0 0 11 1 2 3 0 0 0 0 7 8 9 11
The origin is at the top, left corner of image f ( x , y) (see Fig. 2.1). To perfo 0 I 01 1I 0 0 0 0 II 0 4 5 6 0 0 (1 0 0 0 0 0
correlation, we place the bottom, rightmost point of w(x, y) so that it coin 0 0 I) 0 (I i) 0 i)11 0 i) o 7 8 9 il (I i)
cides with the origin o f f (x, y), as illustrated in Fig. 3.14(c). Note the use 0 11 o 11 (I i j i>(I (1 (I o o [I { J CJ 9 (I !i
11 0 0 (J 0 i l 0 0 0 11 0 (1 il 0 O 0 (! ()
padding for the reasons mentioned in the discussion of Fig. 3.13. 0 (1 0 11 i) (I I) (I (I i) ii t i o i t 0 o (J (I
correlation, we move w(x, y) in all possible locations so that at least o (9) (h)
pixels overlaps a pixel in the original image f (x, y). This ' f u l l ' correlatio
shown in Fig. 3.14(d). To obtain the ' same ' correlation shown in Fig. 3.14 re f is the input image, w is the filter mask, g is the filtered result, and the
we require that all excursions of w(x, y) be such that its center pixel over1 ummarized in Table 3.2.The f iltering-mode specifies
the original f ( x , y). correlation ( ' c o r r ' ) or convolution conv The ( I I).
For convolution, we simply rotate w(x, y) by 180" and proceed in t 1 with the border-padding issue, with the size of the
manner as in correlation [Figs. 3.14(f) through (h)].As in the one- by the size of the filter. These options are explained
example discussed earlier, convolution yields the same result r e size-options are either ' same or ' f u l l as I,
,as before, m and n are the dimensions of the filter region, ' s l i d i n g ' in-
Figure 3.15(f) shows the result of these operations. Here, when the output was s that the process is one of sliding the m X n region from pixel to pixel
converted to the class of the input ( u i n t 8 ) by i m f i l t e r , clipping caused e input image f , @funreferences a function, which we denote arbitrarily
some data loss. The reason is that the coefficients of the mask did not sum to un, and parameters indicates parameters (separated by commas) that
the range [O,l], resulting in filtered values outside the [O, 2551 range. Thu be required by function f u n . The symbol @ is called a function handle, a
d24:p,,,
.-.$$jfGach~nhandle)
avoid this difficulty, we have the option of normalizing the coefficients so t AB data type that contains information used in referencing a function. .
v.g:,'''
their sum is in the range [O,1] (in the present case we would divide the coe be demonstrated shortly, this is a particularly powerful concept.
cients by (31)~,so the sum would be I), or inputting the data in double for- ecause of the way in which matrix A is organized, function fun must oper-
mat. Note, however, that even if the second option were used, the data usually on each of the columns of A individually and return a row vector, v, con-
would have to be normalized to a valid image format at some point (e.g., for ing the results for all the columns. The kth element of v is the result of the
storage) anyway. Either approach is valid; the key point is that data ranges ation performed by fun on the kth column of A. Since there can be up to
have to be kept in mind to avoid unexpected results. columns in A, the maximum dimension of v is 1 x MN.
3.4.2 Nonlinear Spatial Filtering
Nonlinear spatial filtering is based on neighborhood operations also, and
mechanics of defining m X n neighborhoods by sliding the
through an image are the same as discussed in the previous sect
whereas linear spatial filtering is based on computing the sum
(which is a linear operation), nonlinear spatial filtering is based, fp = p a d a r r a y ( f , [ r c l , method, d i r e c t i o n ) -...:s>:;pih:frray
..:I +ihy
implies, on nonlinear operations involving the pixels of a neighb , ,
\
' i
example, letting the response at each center point be equal to th e f is the input image, f p is the padded image, [ r c ] gives the number of
pixel value in its neighborhood is a nonlinear filtering operati and columns by which to pad f , and method and d i r e c t i o n are as ex-
basic difference is that the concept of a mask is not as prevalent ined in Table 3.3. For example, if f = [ 1 2 ; 3 41, the command
processing. The idea of filtering carries over, but the "filter" should be visu
ized as a nonlinear function that operates on the pixels of a neighborhood, a f p = p a d a r r a y ( f , [ 3 21, ' r e p l i c a t e ' , ' p o s t ' )
whose response constitutes the response of the operation at the center pixel
the neighborhood.
The toolbox provides two functions for performing general no
ing: nlf i l t e r and colf ilt. The former performs operations directly in 2
while colf i l t organizes the data in the form of columns. Although colf i
requires more memory, it generally executes significantly faster th
98 Chapter 3 Intensity Transformationsand Spatial Filtering 3.5 a Image Processing Toolbox Standard Spatial Filters 99
TABLE 3.3
Options for
function
g = c o l f i l t ( f , [m n ] , ' s l i d i n g ' , Qgmean);
padarray. ' symmetric ' The size of the image is extended by mirror-reflecting it across its
re are several important points at play here. First, note that, although
' replicate ' The size of the image is extended by replicating the values in its A is part of the argument in function gmean, it is not included in the
outer border.
' circular ' The size of the image is extended by treating the image as one
period of a 2-D periodic function.
ically by colf i l t , the number of columns in A is variable (but, as noted ear-
the number of rows, that is, the column length, is always mn). Therefore, the
Pad before the first element of each dimension.
e of A must be computed each time the function in the argument is called by
Pad after the last element of each dimension.
l f i l t . T h e filtering process in this case consists of computing the product of
Pad before the first element and after the last element of each
dimension.This is the default.
EXAMPLE 3.8: As an illustration of function colf i l t , we implement a nonlinear filter Image Processing Toolbox Standard Spatial Filters
Using function whose response at any point is the geometric mean of the intensity values of In this section we discuss linear and nonlinear spatial filters supported by IPT.
colf ilt to the pixels in the neighborhood centered at that point.The geometric mean in a
implement a Additional nonlinear filters are implemented in Section 5.3.
nonlinear spatial neighborhood of size m X n is the product of the intensity values in the neigh-
filter. borhood raised to the power llmn. First we implement the nonlinear filter
3.5.1 Linear Spatial Filters
function, call it gmean:
The toolbox supports a number of predefined 2-D linear spatial filters, ob-
function v = gmean(A) tained by using function f s p e c i a l , which generates a filter mask, w, using the
mn = size(A, 1 ) ; % The length of the columns of A i s always mn. syntax
V = prod(A, l ) . ^ ( l / m ~ ) ;
w = f s p e c i a l ( ' t y p e l , parameters)
prod ( A ) returns rhe To reduce border effects, we pad the input image using, say, the ' r e p l i c a t e '
produc~of the ele- option in function padarray: where ' t y p e ' specifies the filter type, and parameters further define the
menls of A prod
( A , d i m ) returns the specified filter. The spatial filters supported by f s p e c i a l are summarized in
producr of the >> f = p a d a r r a y ( f , [m n ] , 'replicate'); Table 3.4, including applicable parameters for each filter.
elentenrs o,fA along
dinzension dim.
100 Chapter 3 a Intensity Transformations and Spatial Filtering 3.5 e Image Processing Toolbox Standard Spatial Filters 101
TABLE 3.4
Spatial filters
supported by I average ' f s p e c i a l ( ' average' , [ r c ] ) . A rectangular averaging filter of [ f ( x f 1 , ~ +) f ( x - 1 , ~ +) f ( x , Y + 1 ) + f ( - r , y - I ) ] - 4 f ( x , y )
function size r x c. The default is 3 x 3. A single number instead of
fs~ecial. [ r C ] specifies a square filter. xpression can be implemented at all points ( x , y ) in an image by con-
f special ( ' disk ' , r ) .A circular averaging filter (within a g the image with the following spatial mask:
square of size 2 r + 1) with radius r.The default radius is 5.
'gaussian' f s p e c i a l ( ' g a u s s i a n ' , [ r c ] , s i g ) . A Gaussian lowpass filter 0 1 0
of size r x c and standard deviation s i g (positive).The defaults 1 -4 1
are 3 x 3 and 0.5. A single number instead of [ r c ] specifies a
0 1 0
' laplacian ' f special( ' laplacian ' , a l p h a ) . A 3 X 3 Laplacian filter whose lternate definition of the digital second derivatives takes into account di-
shape is specified by alpha, a number in the range [O,l].The
a1 elements, and can be implemented using the mask
default value for alpha is 0.5.
f s p e c i a l ( ' l o g ' , [ r c ] , sig).Laplacian of a Gaussian (LOG) 1 1 1
filter of size r x c and standard deviation s i g (positive).The
defaults are 5 X 5 and 0.5. A single number instead of [ r c ] 1 -8 1
specifies a square filter. 1 1 1
'motion' f s p e c i a l ( ' m o t i o n ' , l e n , t h e t a ) . Outputs a filter that,when
convolved with an image, approximates linear motion (of a derivatives sometimes are defined with the signs opposite to those shown
camera with respect to the image) of len pixels.The direction of ulting in masks that are the negatives of the preceding two masks.
motion is t h e t a , measured in degrees, counterclockwise from the ncement using the Laplacian is based on the equation
horizontal. The defaults are 9 and 0, which represents a motion of
9 pixels in the horizontal direction.
'prewitt ' f s p e c i a l ( ' p r e w i t t ' ).Outputs a 3 x 3 Prewitt mask, wv, that g(x9 Y ) = f ( x , Y ) + c [ V 2 f ( x Y3 ) ]
approximates a vertical gradient. A mask for the horizontal
gradient is obtained by transposing the result: wh = wv ' . re f ( x , y) is the input image, g ( x , y ) is the enhanced image, and c is 1 if the
f s p e c i a l ( ' s o b e l ' ) . Outputs a 3 X 3 Sobel mask, sv, that r coefficient of the mask is positive, or -1 if it is negative (Gonzalez and
approximates a vertical gradient. A mask for the horizontal s [2002]).Because the Laplacian is a derivative operator, it sharpens the
gradient is obtained by transposing the result: sh = sv ' . but drives constant areas to zero. Adding the original image back re-
'unsharp' f s p e c i a l ( ' u n s h a r p ' , a l p h a ) . Outputs a 3 X 3 unsharp filter. s the gray-level tonality.
unction f s p e c i a l ( ' l a p l a c i a n ' , alpha) implements a more general
a
- 1-a -
- a
EXAMPLE 3.9: a We illustrate the use of f s p e c i a l and irnf i l t e r by enhancing an ima 1+a 1+a 1+a
Using function with a Laplacian filter. The Laplacian of an image f ( x ,y ) , denoted V2f( x , y
imf i l t e r . is defined as -
1-a -
-4 -
1-a
a b
c d
FIGURE 3.16
(a) Image of the
North Pole of the
moon.
(b) Laplaclan
flltered Image,
uslng u l n t 8
formats
(c) Laplaclan
f~lteredImage
obtalned uslng
d o u b l e formats.
(d) Enhanced
result, obtained
by subtracting (c)
from (a).
(Or~glnalimage
courtesy of
NASA.)
EXAMPLE 3.10:
Manually
specifying filters
and comparing
enhancement
techniques.
to implement this filter manually, and also to compare the results ob-
by using the two Laplacian formulations. The sequence of commands is
>> w = fspecial('laplacian',0)
W =
0.0000 1 .OOOO 0.0000 = imread('moon.tifl);
1 .0000 -4.0000 1 .0000 4 = fspecial('laplacian', 0); % Same as w in Example 3.9.
8 = [ I 1 1 ; 1 -8 1 ; 1 1 11;
0.0000 1 .oooo 0.0000 = im2double(f);
Note that the filter is of class double,and that its shape with alpha = 0 is 94 = f - irnfilter(f, w4, 'replicate');
Laplacian filter discussed previously. We could just as easily have specified t g8 = f - irnfilter(f, w8, 'replicate');
shape manually as figure, imshow(g4)
figure, imshow(g8)
>>w= [O 1 0 ; 1-4 1 ; 0 1 0 1 ;
3.5 a Image Processing Toolbox Standard Spatial Filters
104 Chapter 3 Intensity Transformationsand Spatial Filtering
ere v is vector whose elements are the median of A along dimension dim.
3.5.2 Nonlinear Spatial Filters r example, if dim = I , each element of v is the median of the elements along
A commonly-used tool for generating nonlinear spatial filters in IPT is f corresponding column of A.
tion ordf ilt2, which generates order-statisticfilters (also called rank filt
These are nonlinear spatial filters whose response is based on ordering (ran
ing) the pixels contained in an image neighborhood and then replacing th ecall that the median. 5,of a set of values is such that half the values in the set are less than or equal
value of the center pixel in the neighborhood with the value determined by th and half are greater than or equal to 5.
106 Chapter 3 Intensity Transformations and Spatial Filtering fa Summary 107
Figure 3.18(c) is the result of median filtering this noisy image, using the
statement:
ddition to dealing with image enhancement, the material in this chapter is the foun-
n for numerous topics in subsequent chapters. For example, we will encounter spa-
rocessing again in Chapter 5 in connection with image restoration, where we also
Considering the level of noise in Fig. 3.18(b), median filtering using the de- a closer look at noise reduction and noise-generating functions in MATLAB.
fault settings did a good job of noise reduction. Note, however, the black e of the spatial masks that were mentioned briefly here are used extensively in
apter 10 for edge detection in segmentation applications. The concept of convolu-
specks around the border. These were caused by the black points surrounding
n and correlation is explained again in Chapter 4 from the perspective of the fre-
the image (recall that the default pads the border with 0s). This type of effect quency domain. Conceptually, mask processing and the implementation of spatial
can often be reduced by using the ' symmetric ' option: ters will surface in various discussions throughout the book. In the process, we will
xtend the discussion beeun here and introduce additional aspects of how spatial filters
>> gms = m e d f i l t 2 ( f n , 'symmetric');
The result, shown in Fig. 3.lS(d), is close to the result in Fig. 3.18(c), except that
the black border effect is not as pronounced. B
4.1 1The 2-D Discrete Fourier Transform 109
h u and v as (frequency) variables.
died in the previous chapter, which
e coordinate system spanned by f ( x ,y ) , with x and y as (spatial) variables.
If f ( x , y ) is real, its Fourier transform is conjugate symmetric about t alues from M / 2 to M - 1are repetitions of the values in the half period to
origin; that is,
F(u,v ) = F*(-11, -v)
which implies that the Fourier spectrum also is symmetric about the origin:
IF(u, v)l = IF(-u, -v)l
It can b e shown by direct substitution into the equation for F ( u , v ) that
F(u,v ) = F(u + M , v ) = F(u,v + N ) = F(u + M , v + N )
In other words, the DFT is infinitely periodic in both the u and v directio
with the periodicity determined by M and N. Periodicity is also a property
the inverse DFT
f(x,y) = f(x + M,y) = f(x,y +N) = f ( x + M,Y + N )
a
b
FIGURE 4.1
(a) Fourier
spectrum showlng
back-to-back half
per~odsln the
interval
[0, M - 11.
(b) Centered
spectrum in the
same Interval,
obtalned by
mult~ply~ng f (x)
by (-11,' prlor to
computing the
Four~er
transform.
. .
4.2 ;BI Computing and Visualizing the 2-D DFT
112 Chapter 4 1 Frequency Domain Processing
a b
c d
FIGURE 4.3
(a) A simple Image.
(b) Four~er
spectrum.
(c)Centered
spectrum.
(d) Spectrum
visually enhanced
by a log
transformation.
which computes the magnitude (square root of the sum of the squares o f t
real and imaginary parts) of each element of the array. re F is the transform computed using f f t 2 and FC is the centered trans-
Visual analysis of the spectrum by displaying it as an image is an import .Function f f t s h i f t operates by swapping quadrants of F. For example, if
aspect of working in the frequency domain. As an illustration, consider [ 1 2; 3 4 l , f f t s h i f t ( a ) = [ 4 3; 2 l].Whenappliedtoatransform
simple image, f, in Fig. 4.3(a). We compute its Fourier transform and disp it has been computed, the net result of using f f t s h i f t is the same as if
the spectrum using the following sequence of steps: put image had been multiplied by (-l)"+y prior to computing the trans-
. Note, however, that the two processes are not interchangeable. That is,
>> F = f f t 2 ( f ) ; ng 3[.]denote the Fourier transform of the argument, we have that
>> S = a b s ( F ) ; - l ) " + y f ( ~ y, ) ] is equal to f f t s h i f t ( f f t 2 ( f ) ), but this quantity is not
>> imshow(S, [ ] ) altofft2(fftshift(f)).
n the present example, typing
Figure 4.3(b) shows the result. The four bright spots in the corners of
image are due to the periodicity property mentioned in the previous sec Fc = f f t s h i f t ( F ) ;
IPT function f f t s h i f t can be used to move the origin of the transfor imshow(abs(fc), [ I)
the center of the frequency rectangle. The syntax is
ded the image in Fig. 4.3(c). The result of centering is evident in this
Fc = f f t s h i f t ( F )
114 Chapter 4 B Frequency Domain Processing 4.3 Filtering in the Frequency Domain 115
Although the shift was accomplished as expected, the dynamic range o f t utput of i f f t 2 often has very small imaginary components resulting
values in this spectrum is so large ( 0 to 204000) compared t o the 8 bits of t round-off errors that are characteristic of floating point computations.
display that the bright values in the center dominate the result. A s discussed it is good practice to extract the real part of the result after computing
Section 3.2.2, this difficulty is handled via a log transformation. Thus, t se to obtain an image consisting only of real values. The two opera-
commands
>> S2 = l o g ( 1 + a b s ( F c ) ) ; = real(ifft2(F));
>> imshow(S2, [ I )
the forward case, this function has the alternate format i f f t 2 (F, P , a), real ( a r g ,
resulted in Fig. 4.3(d).The increase in visual detail is evident in this image. h pads F with zeros s o that its size is P X Q before computing the inverse. imag ( a r g ) e.rrmcr
Function i f f t s h i f t reverses the centering. Its syntax is option is not used in the book. the real ant1 i111agi-
17or.y ports o,f arg,
respectively.
F = ifftshift(Fc)
Filtering in the Frequency Domain
This function can be used also to convert a function that is initially centere
in the frequency domain is quite simple conceptually. In this section
a rectangle to a function whose center is at the top, left corner of the rectan
a brief overview of the concepts involved in frequency domain filter-
We make use of this property in Section 4.4.
and its implementation in MATLAB.
While on the subject of centering, keep in mind that the center of the
quency rectangle is at ( M / 2 , N / 2 ) if the variables u and v run from 0 to M
and N - 1,respectively. For example, the center of an 8 x 8 frequency s 1 Fundamental Concepts
is at point ( 4 . 4 ) ,which is the 5th point along each axis,counting u p from foundation for linear filtering in both the spatial and frequency domains is
If, as in MATLAB, the variables run from 1 to M and 1 to N, respective1 convolution theorem, which may be written ast
the center of the square is at [ ( M / 2 )+ 1, ( N / 2 ) + 11. In the case
8 x 8 example, the center would be at point ( 5 , 5 ) , counting u p from f ( x ,Y ) * h ( h ,Y ) e H ( u , v)F(ld,v )
Obviously, the two centers are the same point, but this can b e a source of c
fusion when deciding how to specify the location of D F T centers in MATL
computations. f ( x , y ) h ( h ,Y ) e H ( u , v ) * G ( u , v )
If M and N are odd, the center for MATLAB computations is obtain
rounding M/2 and N / 2 down to the closest integer. The rest of the analy e, the symbol "*" indicates convolution of the two functions, and the ex-
= (A) ssions on the sides of the double arrow constitute a Fourier transform pair.
I I as in the previous paragraph. For example, the center of a 7 X 7 region is
u f A to rile neare.s[ ( 3 . 3 ) if we count up from (0,O)and at ( 4 , 4 ) if we count up from ( 1 , l ) .In example, the first expression indicates that convolution of two spatial
r~zre,yerless rllan or
ther case, the center is the fourth point from the origin. If only one of the tions can be obtained by computing the inverse Fourier transform of the
etlllci, irr vcllllc,
t of the Fourier transforms of the two functions. Conversely, the for-
f - ~ t ~ ~ c rci oe ii~l mensions is odd, the center along that dimension is similarly obtained
rO1trldsm nerrresf rounding down in the manner just explained. Using MATLAB's functi ourier transform of the convolution of two spatial functions gives the
rl~regergreater rhrm
f l o o r , and keeping in mind that the origin is at (1, I ) , the center of the f t of the transforms of the two functions. Similar comments apply to the
o r E q l l a l l olllevoll,e
ofeach elemerlr c f A . quency rectangle for MATLAB computations is at
terms of filtering, we are interested in the first of the two previous ex-
[floor(M/2) + 1 , floor(Nl2) + I ] ions. Filtering in the spatial domain consists of convolving an image
y ) with a filter mask, h ( x . y ) . Linear spatial convolution is precisely as ex-
ed in Section 3.4.1. According to the convolution theorem, we can obtain
The center given by this expression is valid both for odd and even values of
result in the frequency domain by multiplying F ( u , v ) by H ( u , v ) ,
and N.
er transform of the spatial filter. It is customary to refer to H ( u . v) as
Finally, we point out that the inverse Fourier transform is computed usin
function i f f t 2 . which has the basic syntax er transfer function.
asically, the idea in frequency domain filtering is to select a filter transfer
tion that modifies F ( u , v) in a specified manner. For example, the filter in
2 f = ifft2(F)
where F is the Fourier transform and f is the resulting image. If the input us kital images, these cxprcssions are strictly valid only when f(s.y) and 11i.r..v) have been proper-
to compute F is real, the inverse in theory should be real. In practice, howeve dded with zeros. as discussed later in this section.
116 Chapter 4 r Frequency Domain Processing 4.3 B Filtering in the Frequency Domain 117
a b e following function, called paddedsize. computes the minimum event
FIGURE 4.4 s of P and Q required to satisfy the preceding equations. It also has an
Transfer functions n to pad the inputs to form square images of size equal to the nearest in-
of (a) a centered
lowpass filter, and
(b) the format
used for DFT
filtering. Note
that these are
frequency domain ce of the input parameters.
function paddedsize, the vectors AB, CD, and PQ have elements [ A B ] ,
1, and [ P Q], respectively, where these quantities are as defined above.
paddedsize
..- .--
:=. .
is image blurring (smoothing). Figure 4.4(b) shows the same filter after it
processed with f f t s h i f t. This is the filter format used most frequently in
= PADDEDSIZE(AB, 'PWR2') computes the vector PQ such that
( 1 ) = P Q ( 2 ) = 2*nextpow2(2*m),where m i s MAX(AB).
Based on the convolution theorem, we know that to obtain the corr PQ = PADDEDSIZE(AB, C D ) , where AB and CD are two-element size
ing filtered image in the spatial domain we simply compute the inverse ectors, computes the two-element size vector PQ. The elements
transform of the product H ( u , v ) F ( u f PQ are the smallest even integers greater than or equal to
the process just described is identical
lution in the spatial domain, as long as the filter mask, h ( x , y ) , is the inver
Fourier transform of H ( u , v). In practice, spatial convolution general1 Q = PADDEDSIZE(AB, CD, 'PWR2') computes the vector PQ such that
plified by using small masks that attempt to capture the salient fea Q ( 1 ) = P Q ( 2 ) = 2^nextpow2(2*m),where m i s MAX([AB C D ] ) .
their frequency domain counterparts.
wraparound error, can be avoided by padding the functions with zeros, in t = max(A13); % Maximum dimension.
following manner.
Assume that functions f ( x , y ) and h ( x , y ) are of size A X B and C X
respectively. We form two extended (padded) functions, both of size P x Q
appending zeros to f and g. It can be shown that wraparound error is avoid p = nextpowZ(n)
by choosing rerrrrns the smallest
2^nextpow2(2*m); integer power of 2
P 2 A + C - 1 that is grenter thnn or
eqrinl to the crbsohite
and vnlrre o f n.
or( 'Wrong number of inputs. ' )
Q?B+D-l .-..a
Most of the work in this chapter deals with functions of the same size, M X
in which case we use the follow
Q 2 2N - 1. ustornary to work with arrays of even dimensions to speed-up FFT computations.
118 Chapter 4 18 F'requency Domain Processing 4.3 4 Filtering in the Frequency Domain 119
Figure 4.5(b) shows image g. A s expected, the image is blurred, but nott
that the vertical edges are not. The reason can be explained with the aid 0
Fig. 4.6(a), which shows graphically the implied periodicity in DFT computa
- -
a b c ge itself and also the' bottom part of the periodic component right above it.
FIGURE 4.5 (a) A s~mpleImage of slze 256 x 256 (b) Image lowpass-f~lteredin the frequency domaln + U4when a light and a dark region reside under the filter, the result will be
out padd~ng(c) Image lowpass-flltered In the trequency doma~nw~thpaddlng. Compare the l~ghtport1
the vertical edgeb In (b) and (c). mid-gray, blurred output. Thls is precisely what the top of the image In
120 Chapter 4 Frequency Domain Processing 4.3 ia Filtering in the Frequency Domain 121
Fig. 4.5(b) shows. On the other hand, when the filter is on the light sides oft 1 from Section 3.4.1 that this call to function i m f i l t e r pads the border
dashed image, it will encounter an identical region on the periodic compon image with 0s by default. I
Since the average of a constant region is the same constant, there is no b
ring in this part of the result. Other parts of the image in Fig. 4.5(b) are Basic Steps in DFT Filtering
plained in a similar manner.
Consider now filtering with padding: 'scussion in the previous section can be summarized in the following
-step procedure involving MATLAB functions, where f is the image to
>> PQ = p a d d e d s i z e ( s i z e ( f ) ) ;
red, g is the result, and it is assumed that the filter function H ( u , v) is of
>> Fp = f f t 2 ( f , P Q ( l ) , P Q ( 2 ) ) ; % Compute t h e FFT w i t h paddin me size as the padded image:
>> Hp = l p f i l t e r ( ' g a u s s i a n ' , P Q ( l ) , P Q ( 2 ) , 2 * s i g ) ;
>Z Gp = Hp.*Fp; btain the padding parameters using function paddedsize:
>> gp = r e a l ( i f f t 2 ( G p ) ) ; = paddedsize(size(f));
>> gpc = g p ( l : s i z e ( f , l ) , l : s i z e ( f , 2 ) ) ;
>> imshow(gp, [ I )
tain the Fourier transform with padding:
= f f t 2 ( f , PQ(I), PQ(2));
where we used 2*sig because the filter size is now twice the size of the fil nerate a filter function, H, of size PQ(1) x PQ(2) using any of the
used without padding. thods discussed in the remainder of this chapter.The filter must be in
Figure 4.7 shows the full, padded result, gp.The final result in Fig. 4.5(c) format shown in Fig. 4.4(b). If it is centered instead, as in Fig. 4.4(a),
obtained by cropping Fig. 4.7 to the original image size (see the next-to-1 H = f f t s h i f t ( H ) before using the filter.
command above). This result can be explained with the aid of Fig. 4.6(
which shows the dashed image padded with zeros as it would be set up int ltiply the transform by the filter:
nally in f f t 2 ( f , P Q ( 1 ) , PQ(2)) prior to computing the transform.The i
plied periodicity is as explained earlier. The image now has a uniform bl btain the real part of the inverse FFT of G:
border all around it, so convolving a smoothing filter with this infinite = real(ifft2(G));
quence would show a gray blur in the light edges of the images. A similar res
would be obtained by performing the following spatial filtering, rop the top, left rectangle to the original size:
= g(l:size(f, I ) , l:size(f, 2));
>> h = f s p e c i a l ( ' g a u s s i a n ' , 15, 7 ) ;
>> gs = i m f i l t e r ( f , h ) ; is filtering procedure is summarized in Fig. 4.8. The preprocessing stage
ompass procedures such as determining image size, obtaining the
arameters, and generating a filter. Postprocessing entails computing
the result, cropping the image, and converting it to class u i n t 8
FIGURE 4.7 Full
padded image
resulting from
~f f t2 after
filtering.This Frequency domain filtering operations FIGURE 4.8
image is of size Basic steps for
512 x 512 pixels. filtering in the
frequency
domain.
g(*, Y)
Filtered
image
122 Chapter 4 :& Frequency Domain Processing 4.4 % Obtaining Frequency Domain Filters from Spatial Filters 123
The filter function H ( u , v) in Fig. 4.8 multiplies both the ne and algorithms used and on issues such the sizes of buffers, how well
parts of F(r1, v). If H ( u , u ) is real, then the phase of the resul
fact that can be seen in the phase equation (Section 4.1) by noting that,if the m
tipliers of the real and imaginary parts are equa
phase angle unchanged. Filters that operate in this
shiftfilters.These are the only types of linear filters considered in this chapter. arge. Thus, it is useful to know how to convert a spatial filter into an
It is well known from linear system theory that, under certain mild con nt frequency domain filter in order to obtain meaningful comparisons
tions, inputting an impulse into a linear system completely characterizes t
system. When working with finite, discrete data
sponse of a linear system, including the response ponds to a given spatial filter, h, is to let H = f f t 2 ( h , PQ(1 ) , PQ( 2 ) ) ,
the linear system is just a spatial filter, then we can completely determine the values of vector PQ depend on the size of the image we want to fil-
filter simply by observing its response to an impu1se.A filter determined in t discussed in the last section. However, we are interested in this section
manner is called a fitzite-inzp~~lse-response (FIR) filter. All the linear spatial
ters in this book are FIR filters.
4.3.3 An M-function for Filtering in the Frequency Domain techniques discussed in the previous section. Because, as explained
The sequence of filtering steps described in the previous
throughout this chapter and parts of the next, so it will be co
available an M-function that accepts as inputs an image and
handles all the filtering details, and outputs the
following function does this.
:,>*d;"
d f t f lit
... .
function g = d f t f i l t ( f , H )
%DFTFILT Performs frequency domain f i l t e r i n g .
% G = DFTFILT(F, H ) f i l t e r s F i n the frequency domain using the t in the present discussion is
% f i l t e r t r a n s f e r function H . The output, G, i s the f i l t e r e d
% image, which has the same s i z e as F. DFTFILT automatically pads H = f r e q z 2 ( h , R, C )
% F t o be the same s i z e as H . Function PADDEDSIZE can be used
% t o determine an appropriate s i z e f o r H .
/o
FIGURE 4.9
(a) A gray-scale FIGURE 4.10
image. (b) Its (a) Absolute
Fourler spectrum. value of the
frequency
domaln filter
corresponding to
a vertical Sobel
mask. (b) The
same filter after
processing with
function
f f t s h l f t . Figures
(c) and (d) are the
filters in (a) and
(b) shown as
Images.
how(abs(H), [ I )
ure, imshow(abs(HI), [ I )
7> PQ = paddedsize(size(f)); ures 4.11(a) and (b) show the result of the commands:
>> H = freqz2(h, PQ(l), PQ(2));
7> HI = ifftshift(H);
lgure, irnshow(gf, [ I )
where, as noted earlier, ifftshift is needed to rearrange the data so that t
origin is at the top, left of the frequency rectangle. Figure 4.10(b) shows a ality in the images is due to the fact that both gs and g f have neg-
of abs (HI ) . Figures 4.10(c) and (d) show the absolute values of H and H , which causes the average value of the images to be increased by
image form, displayed with the commands imshow command. As discussed in Sections 6.6.1 and 10.1.3, the
126 Chapter 4 id Frequency Domain Processing 4.5 Generating Filters Directly in the Frequency Domain 127
EXAMPLE 4.3: 3 As an illustration, the following commands compute the distance W a r ) of order n, with a cutoff frequency at a
Using function from every point in a rectangle of size 8 x 5 to the origin of the rectangle: e Do from the origin, has the transfer function
d f tuv.
>r [U, V] = d f t u v ( 8 , 5 ) ;
>> D = U . ^ 2 + V . ^ 2
D =
0 1 4 4 1
1 2 5 5 2
4 5 8 8 5
9 10 13 13 10
16 17 20 20 17
9 10 13 13 10
4 5 8 8 5
1 2 5 5 2
130 Chapter 4 .r Frequency Domain Processing 4.5 aB Generating Filters Directly in the Frequency Domain 131
where u is the standard deviation. By letting u = DO,we obtain the followi can view the filter as an image [Fig. 4.13(b)] by typing
expression in terms of the cutoff parameter Do:
figure, imshow(fftshift(H), [ 1 )
H ( ~.U)
~ =, e - ~ 2 ( u .(!)/2Di
Begin f i l t e r computations.
itch type
se ' ideal '
H = double(D <= D O ) ;
Case ' btw'
rl 3 1 aaa3 if nargln == 4
- ,
132 Chapter 4 a Frequency Domain Processing 4.5 1Generating Filters Directly in the Frequency Domain 133
mesh (H)
This function draws a wireframe for x = I :M and y = 1 :N, where [ M , N ] determine the current viewing geometry, we type
s i z e ( H ) . Wireframe plots typically are unacceptably dense if M and N ar
large, in which case we plot every kth point using the syntax [ a z , e l ] = view;
colormap([O 0 01 )
sets the wireframe to black (we discuss function colormap in Chapter an coordinates, (x, y, z ) , which is ideal when working with RGB data.
MATLAB also superimposes a grid and axes on a mesh plot. These can
turned off using the commands
g r i d off Consider a Gaussian lowpass filter similar to the one used in Example 4.4: EXAIMPLE 4.5:
a x i s off Wireframe
H = fftshift(lpfilter('gaussian', 500, 500, 5 0 ) ) ; plotting.
They can be turned back on by replacing off with on in these two statement
Finally, the viewing point (location of the observer) is controlled by functio ure 4.15(a) shows the wireframe plot produced by the commands
view, which has the syntax
mesh(H(I:10:500, 1:10:500))
view(az, e l ) a x i s ( [ O 50 0 50 0 I ] )
As Fig. 4.14 shows, az and e l represent azimuth and elevation angles (in d here the a x i s command is as described in Section 3.3.1, except that it con-
grees), respectively. The arrows indicate positive direction. The default valu ins a third range for the z axis.
134 Chapter 4 % Frequency Domain Processing
4.5 ap Generating Filters Directly in the Frequency Domain 135
a. b
c d. function produces a plot identical to mesh, with the exception that the
1 erals in the mesh are filled with colors (this is called faceted shading).
FIGURE 4.1 5 0.8
(a) A plot rt the colors to gray, we use the command
obtained using 0.6
function mesh. 0.4 colormap(gray)
(b) Axes and grid 0.2
removed. (c) A
0 axis,grid,and view functions work in the same way as described ear-
different 50
perspective view 0 mesh.For example, Fig. 4.16(a) is the result of the following sequence
obtained using
function view.
(d) Another view = fftshift(lpfilter('gaussian', 500, 500, 50));
urf (H(l :10:500, 1:10:500))
xis([O 50 0 50 0 11)
e faceted shading can be smoothed and the mesh lines eliminated by in-
lation using the command
,dz.:.
Z>( \
shading interp .y$$*ghadjng i n t e r p
. ./,,'">
, ,.
~
>> view(-25, 30) n use mesh ( Z ) or surf (Z) as before. Recall from the discussion in
2.10.4 that that columns ( Y ) are listed first and rows (X) second in
which moved the observer slightly to the right, while leaving the elevation c
stant. Finally, Fig. 4.15(d) shows the result of leaving the azimuth at -25
setting the elevation to 0:
>> view(-25, 0)
a b
This example shows the significant plotting power of the simple function mesh FIGURE 4.1 6
(a) Plot obtained
using function
Sometimes it is desirable to plot a function as a surface instead of as a w s u r f . (b) Result
frame. Function surf does this. Its basic syntax is of using the
command
surf surf ( H ) shading i n t e r p .
136 Chapter 4 Frequency Domain Processing 4.6 t8 Sharpening Frequency Domain Filters 137
EXAMPLE 4.6: B Figure 4.17 shows plots and images of ideal, Butterworth, and Gaussi
Highpass filters. highpass filters. The plot in Fig. 4.17(a) was generated using the commands
the image in this example. Note the clarity of the bone structure and ot
tails that simply are not visible in any of the other three images. The
hanced image appears a little noisy, but this is typical of X-ray ima
their gray scale is expanded.The result obtained using a combination of
frequency emphasis and histogram equalization is superior to the result th
would be obtained by using either method alone.
Summary
In addition to the image enhan
and the preceding chapter, the
ters provide the basis for other
cussions in the book. Intensity t
and spatial filtering is used extensively for image restoration in the next chapter
color processing in Chapter 6, for image segmentation in Chapter 10,and for extrac
descriptors from an image in Chapter 11. The Fourier techniques developed in
chapter are used extensively in the next chapter for image restoration, in Chapter 8
image compression, and in Chapter 11 for image description.
obtain an estimate, f ( x , y ) , of the original image. We want the estimate to be Adding Noise with Function imnoise
close as possible to the original input image. In general, the more we know abo olbox uses function imnoise to corrupt an image with noise. This func-
Hand 77, the closer j ( x , y ) will be to f ( x , y). has the basic syntax
If H i s a linear, spatially invariant process, it can be shown that the degrad &$>?:,I
image is given in the spatial domain by g = i m n o i s e ( f , t y p e , parameters) z+$>:$% fmnoi
G,~:", , . %
se
,* _,
Followrng conven- g ( x >Y ) = h ( x 7Y ) * f ( x , Y ) + V ( X ,Y ) re f is the input image, and type and parameters are as explained later.
tion, we use an
in-line asterisk in converts the input image to class double in the range [O,1]
erlrlariotw to denote e adding noise to it. This must be taken into account when specifying
convol~~tion and a parameters. For example, to add Gaussian noise of mean 64 and variance
slrperscript asterisk
to denote the corn- o an u i n t 8 image, we scale the mean to 64/255 and the variance to
plex cotlj~[gate.A s the preceding model in an equivalent frequency domain representation: t into imnoise. The syntax forms for this function are:
reqrrired, we also Lrse
nn asterisk in MAT-
LAB expressions to
G(u,V ) = H ( u , v ) F ( u ,V ) + N(u, V )
denote mr~ltiplica-
tior'. Cure shOrrld be
taken not to conjirse
t,7ese [,nrelafed [lses
o f the same synzbol.
sf
tf
tion process is sometimes referred to as deconvolution. imnoise ( f , ' s a l t & pepper' ,d) corrupts image f with salt and
A = randn(M, N ) randn
st form returns in I all the indices of array A that point to rlonzero ele-
If none is found, f i n d returns an empty matrix. The second form
the row and column indices of the nonzero entries in the matrix A. In
n to returning the row and column indices, the third form also returns
onzero values of A as a column vector, v.
first form treats the array A in the format A ( : ) , s o I is a column vector.
rm is quite useful in image processing. For example, to find and set to 0
Is in an image whose values are less than 128 we write
I1 that the logical statement A < 128 returns a 1for the elements of A that
the logical condition and 0 for those that d o not.To set to 128 all pixels
closed interval [64,192] we write
f u n c t i o n R = imnoise2(type, M, N, a, b)
%IMNOISE2 Generates an a r r a y o f random numbers w i t h s p e c i f i e d PDF.
% R = IMNOISE2(TYPE, M, N, A, 8) generates an a r r a y , R, o f s i z e
% MM-y-N, whose elements a r e random numbers o f t h e s p e c i f i e d TYP
% w i t h parameters A and B. I f o n l y TYPE i s i n c l u d e d i n t h e
% i n p u t argument l i s t , a s i n g l e random number o f t h e s p e c i f i e d
% TYPE and d e f a u l t parameters shown below i s generated. Ifo n l y
% TYPE, M, and N a r e provided, t h e d e f a u l t parameters shown b e l o
% a r e used. I f M = N = 1, IMNOISE2 generates a s i n g l e random
% number o f t h e s p e c i f i e d TYPE and parameters A and B.
%
% V a l i d values f o r TYPE and parameters A and B a r e : e r r o r ( 'The sum Pa + Pb must n o t exceed 1 . ' )
%
% 'uniform' U n i f o r m random numbers i n t h e i n t e r v a l (A, 0 ) .
% The d e f a u l t v a l u e s a r e (0, 1). eflerate an M-by-N a r r a y o f u n i f o r m l y - d i s t r i b u t e d random numbers
% 'gaussian ' Gaussian random numbers w i t h mean A and standa n t h e range (0, 1 ) . Then, Pa*(M*N) o f them w i l l have values <=
% d e v i a t i o n B. The d e f a u l t values a r e A = 0 , B . The c o o r d i n a t e s o f these p o i n t s we c a l l 0 (pepper
% ' s a l t & p e p p e r ' S a l t and pepper numbers o f amplitude 0 w i t h o i s e ) . S i m i l a r l y , Pb*(M*N) p o i n t s w i l l have values i n t h e range
% p r o b a b i l i t y Pa = A, and a m p l i t u d e 1 w i t h c a l l 1 ( s a l t noise).
% p r o b a b i l i t y Pb = B. The d e f a u l t v a l u e s a r e Pa
% Pb = A = B = 0.05. Note t h a t t h e n o i s e has
% values 0 ( w i t h p r o b a b i l i t y Pa = A) and 1 ( w i t
% p r o b a b i l i t y Pb = B ) , so s c a l i n g i s necessary
% values o t h e r t h a n 0 and 1 a r e r e q u i r e d . The n
% m a t r i x R i s assigned t h r e e values. If R ( x , y )
% 0, t h e n o i s e a t ( x , y ) i s pepper ( b l a c k ) . I f
% R(x, y ) = 1, t h e n o i s e a t (x, y ) i s s a l t
% ( w h i t e ) . I f R(x, y ) = 0.5, t h e r e i s no n o i s e a = 1; b = 0.25;
% assigned t o c o o r d i n a t e s ( x , y ) .
% 'lognormal' Lognormal numbers w i t h o f f s e t A and shape
% parameter B. The d e f a u l t s a r e A = 1 and B =
% 0.25.
% ' rayleigh' R a y l e i g h n o i s e w i t h parameters A and B. The
% d e f a u l t values a r e A = 0 and B = 1.
% 'exponential' E x p o n e n t i a l random numbers w i t h parameter A. Th
% d e f a u l t i s A = 1.
% ' erlang ' E r l a n g (gamma) random numbers w i t h parameters
-6 and B. B must be a p o s i t i v e i n t e g e r . The e r r o r ( 'parameter a must be p o s i t i v e f o r e x p o n e n t i a l t y p e . I )
d d e f a u l t s a r e A = 2 and B = 5. E r l a n g random
0,
numbers a r e approximated as t h e sum o f B
% e x p o n e n t i a l random numbers.
1
1
150 Chapter 5 Image Restoration ~ i s Models
e 151
p = h i s t ( r , bins)
B = roipoly(f, c, r )
where f is the image of interest, and c and r are vectors of corresponding -.--.~mw
quential) column and row coordinates of the vertices of the polygon (n
columns are specified first). The output, 8, is a binary image the same
with 0's outside the region of interest and 1's inside. Image B is used as a m 5.4(a) shows a noisy image, denoted by f in the following discussion. EXAMPLE 5.4:
to limit operations to within the region of interest. tive of this example is to estimate the noise type and its Parameters Estimating noise
ed thus far. Figure 5.4(b) shows the parameters.
To specify a polygonal ROI interactively, we use the syntax
B = roipoly(f)
, c, r ] = roipoly(f);
which displays the image f on the screen and lets the user specify the
using the mouse. I f f is omitted, r o i p o l y operates on the last image di 5.4(~)was generated using the commands
Using normal button clicks adds vertices to the polygon. Pressing Bac
or Delete removes the previously selected vertex. A shift-click, rig npix] = h i s t r o i ( f , C , r ) ;
double-click adds a final vertex to the selection and starts the fill of the ure, bar(p, 1)
onal region with Is. Pressing Return finishes the selection withou
a vertex.
a b
To obtain the binary image and a list of the polygon vertices, we use :c d
construct FIGURE 5.4
(a) Noisy
(b) ROI image.
[B, c , r l = r o i p o l y ( . . .)
generated
.
where r o i p o l y ( . . ) indicates any valid syntax for this functi interactively.
(c) Histogram of
fore, c and r are the column and row coordinates of the vertices. ROI.
(d) Histogram of
particularly useful when the ROI is specified interactively because it
coordinates of the polygon vertices for use in other programs or for Gaussian data
plication of the same ROI. generated using
The following function computes the histogram of an image wit function
1mnolse2.
onal region whose vertices are specified by vectors c and r , as in t (Orig~nalimage
discussion. Note the use within the program of function r o i p o l y to courtesy of L l x ~
the polygonal region defined by c and r . Inc.)
When the only degradation present is noise, then it follows from the mode F = SPFILT(G, 'amean ' , M I N ) Arithmetic mean filtering.
Section 5.1 that F = SPFILT(G, 'gmean' , MI N ) Geometric mean filtering.
F = SPFILT(G, 'hmean' , M, N ) Harmonic mean filtering.
g(xt Y ) = f (-r,Y ) + 7 7 ( ~ ,Y ) F = SPFILT(G, 'chmean' , M I N , Q ) Contraharmonic mean
The method of choice for reduction of noise in this case is spatial filtering, us' filtering of order Q. The
default i s Q = 1.5.
techniques similar to those discussed in Sections 3.4 and 3.5. In this section we F = SPFILT(G, 'median', MI N ) Median filtering.
marize and implement several spatial filters for noise reduction. Additional F = SPFILT(G, 'max', M I N ) Max filtering .
on the characteristics of these filters are discussed by Gonzalez and Wo F ISPFILT(G, ' m i n ' , M , N ) Min filtering.
5.3 Restoration in the Presence of Noise Only-5 atial Filtering
-
M
.C
F = SPFILT(G, ' m i d p o i n t ' , M, N) Midpoint f i l t e r i n g .
F = SPFILT(G, 'atrimmed' , MI N, D) Alpha-trimmed mean f i l t e r i n g .
-G
3
4-
Parameter D must be a nonnegative
.-
C even i n t e g e r ; i t s d e f a u l t
E value i s D = 2.
a
m
x he d e f a u l t values when only G and TYPE are i n p u t are M = N = 3,
m
cu
+
N
E = 1.5, and D = 2.
I-'
rl
d
.r(
w
.r(
't + . S
'0 n - W
L - 0
E 0 C
E
-
.B
42
. E a 3; n = 3 ; Q = 1.5; d = 2;
2 A
F W
25
M
." .eM ," 2
3: 3;; 1 r o r ( 'Wrong number o f i n p u t s . ' ) ;
-a I-' a +
B
2 w
C . d + v j
% 5't 5 5
-g g
0
-E
e, E
0 5.2
Z 3
z II
s't
all
5%
't
-
- h
- %
Y
bq
.' -5
V
Y - harmean(g, m, n) ;
E
-..* -+- h
h
..
h
G-
= m e d f i l t 2 ( g , [m n ] , 'symmetric');
-
A
w
w v; 00
a0 V
= o r d f i l t 2 ( g , m*n, ones(m, n ) , 'symmetric' ) ;
V
-
c Y
Do
x wi
m
.z 5
Q-
0:
2.
.-c 5
E:
EZ -
- II
6
II
A
II
-
h h h
g i 4
w
'< << ' h
(d < 0) I ( d l 2 -= r o u n d ( d l 2 ) )
e r r o r ( ' d must be a nonnegative, even i n t e g e r . ' )
= alphatrim(g, m, n, d ) ;
C .-0c
Q
5 .c- E ......................
5 E: 2 %
162 Chapter 5 a Image Restoration toration in the Presence of Noise Only-Spa ha1 Filtering 163
function f = gmean(g, m, n)
% Implements a geometric mean filter.
inclass = class(g) ;
g = im2double(g); FIGURE 5.5
% Disable log(0) warning. ( a ) Image
warning off; corrupted by
f = exp(infilter(log(g), ones(m, n), 'replicate')). ^ ( I Im I n) ; pepper noise with
warning on; probability 0.1.
f = changeclass(inclass, f); (b) Image
corrupted by salt
noise with the
function f = harmean(g, m, n) same probability.
% Implements a harmonic mean filter. (c) Result of
inclass = class(g) ; filtering (a) with a
g = im2double(g) ; 3x 3
f = m * n . I imfilter(l./(g + eps), ones(m, n), 'replicate'); contraharmonic
filter of order
f = changeclass (inclass, f) ; Q = 1.5. (d)
Result of filtering
function f = charmean(g, m, n, q) (b) with
% Implements a contraharmonic mean filter. Q = -1.5.
inclass = class(g) ; (e) Result of
g = im2double(g); filtering (a) with a
f = imfllter(g.A(q+l), ones(m, n), 'replicate'); 3 X 3 max filter.
( f ) Result of
f = f ./ (imfilter(g.^q, ones(m, n), 'replicate') + eps); filtering (b) with a
f = changeclass (inclass, f ) ; 3 X 3 ~ninfilter.
function f = alphatrim(g, m, n, d)
% Implements an alpha-trimmed mean filter.
inclass = class(g) ;
g = im2double(g);
f = imfilter(g, ones(m, n), 'symmetric');
for k = 1:d/2
f = imsubtract (f, ordfilt2(g1 k, ones(m, n) , 'symmetric'));
end
for k = (m*n - (d/2) + l):m*n
f = imsubtract(f, ordfilt2(g1 k, ones(m, n), 'symmetric'));
end
f = f / (m*n - d);
f = changeclass (inclass, f) ; --
>> f p = s p f i l t ( g p , 'chmean', 3, 3, 1 . 5 ) ;
>> f s = s p f i l t ( g s , 'chmean', 3 , 3 , - 1 . 5 ) ;
Figure 5.5(d) shows the result. Similar results can be obtained using max
min filters. For example, the images in Figs. 5.5(e) and (f) were generated f
Figs. 5.5(a) and (b), respectively, with the following commands:
noise with density 0.25. (b) Result obtained using a
>> fpmax = s p f i l t ( g p , ' m a x ' , 3 , 3 ) ;
adaptive median filtering with S,,, = 7.
>> fsmin = s p f i l t ( g s , ' m i n ' , 3 , 3 ) ;
Other solutions using spf ilt are implemented in a similar manner. bedded in a constant background having the same value as pepper
5.3.2 Adaptive Spatial Filters
-function that implements this algorithm, which we call adpmedian, is
The filters discussed in the previous section are applied to an image w in Appendix C. The syntax is
regard for how image characteristics vary from one location to an
some applications, results can be improved by using filters capable of ad f = adpmedian(g, Smax)
their behavior depending on the characteristics of the image in the area
filtered. As an illustration of how to implement adaptive spatial
MATLAB, we consider in this section an adaptive median filter. As befo g is the image to be filtered, and, as defined above, Smax is the maxi-
denotes a subimage centered at location (x, y) in the image being proc llowed size of the adaptive filter window.
The algorithm, which is explained in detail in Gonzalez and Woods [2002
follows: Let ure 5.6(a) shows the circuit board image, f , corrupted by salt-and- EXAMPLE 5.6:
noise, generated using the command Adaptive median
zmin = minimum intensity value in S,y, filtering.
z,,, = maximum intensity value in Sxy imnoise(f, ' s a l t & p e p p e r ' , . 2 5 ) ;
z,, = median of the intensity values in S,,
z,, = intensity value at coordinates ( x , y ) .6(b) shows the result obtained using the command (see Section 3.5.2
the use of medf i l t 2 ) :
The adaptive median filtering algorithm works in two levels, denoted lev
and level B:
m e d f i l t 2 ( g , [ 7 71, ' s y m m e t r i c ' ) ;
Level A: If zmin < zn,,, < zmax,go to level B
Else increase the window size
If window size 5 S ,,,, repeat level A
Else output zmed
Level B: If z,in < z,, < z , output z.,,
Else output zmed = adpmedian(g, 7 ) ;
where S,, denotes the maximum allowed size of the adaptive filter
Another option in the last step in Level A is to output z,, instead of the the image in Fig. 5.6(c), which is also reasonably free of noise, but is
This produces a slightly less blurred result but can fail.to detect salt ( rably less blurred and distorted than Fig. 5.6(b). %
166 Chapter 5 # Image Restoration 5.5 :it Modeling the Degradati on Function 167
Ecd Periodic Noise Reduction Another important degradation model is image blur due to uniform lin-
tion between the sensor and scene during image acquisition. Image blur
by Frequency Domain Filtering modeled using IPT function f s p e c i a l :
As noted in Section 5.2.3, periodic noise manifests itself as impulse-like bu
that often are visible in the Fourier spectrum. The principal approach for PSF = f s p e c i a l ( ' m o t i o n l , l e n , t h e t a )
tering these components is via notch filtering. The transfer function of a
terworth notch filter of order tz is given by all to f s p e c i a l returns a PSF that approximates the effects of linear
of a camera by l e n pixels. Parameter t h e t a is in degrees, measured
1 pect to the positive horizontal axis in a counter-clockwise direction.
H ( u , v) =
en is 9 and the default t h e t a is 0, which corresponds to
n of 9 pixels in the horizontal direction.
use function imf i l t e r to create a degraded image with a PSF that is
where known o r is computed by using the method just described:
Dl(u, V ) = [(L( - M/2 - + (v - N / 2 - v~))~]'/* = i m f i l t e r ( f , PSF, ' c i r c u l a r ' ) ;
and
e ' c i r c u l a r ' (Table 3.2) is used to reduce border effects. We then com-
D~(zr,V ) = [([I - M/2 f no)' + (v - N / 2 + v~)']'/~ the degraded image model by adding noise, as appropriate:
where ([LO,vo)(and by symmetry) ( - u o , -vo) are the locations of the "notch
and Do is a measure of their radius. Note that the filter is specified with res
to the center of the frequency rectangle, so it must be preprocessed with f
tion f f t s h i f t prior to its use, as explained in Sections 4.2 and 4.3. n o i s e is a random noise image of the same size as g, generated using
Writing a n M-function for notch filtering follows the same principles us the methods discussed in Section 5.2.
in Section 4.5. It is good practice to write the function so that multiple notch en comparing in a given situation the suitability of the various ap-
can be input, as in the approach used in Section 5.2.3 to generate multiple es discussed in this and the following sections, it is useful to use the
nusoidal noise patterns. Once H has been obtained, filtering is done us ttern so that comparisons are meaningful. The test pat-
function df t f i l t explained in Section 4.3.3. d by function checkerboard is particularly useful for this pur-
its size can be scaled without affecting its principal features. The
%%@Modeling the Degradation Function
When equipment similar to the equipment that generated a degraded ima C = checkerboard(NP, M , N ) checkerboard
available, it is generally possible to determine the nature of the degradatio
experimenting with various equipment settings. However, relevant ima e NP is the number of pixels on the side of each square, M is the number of
equipment availability is the exception, rather than the rule, in the solution ,and ti is the number of columns. If N is omitted, it defaults to M. If both M
image restoration problems, and a typical approach is to experiment by gen are omitted, a square checkerboard with 8 squares o n the side is gener-
ating PSFs and testing the results with various restoration algorithms.Ano is omitted, it defaults to 10 pixels. The light squares on
approach is to attempt to model the PSF mathematical1y.This approach is half of the checkerboard are white. The light squares o n the right half
side the mainstream of our discussion here; for an introduction to this to checkerboard are gray. To generate a checkerboard in which all light
see Gonzalez and Woods [2002].Finally, when no information is ayailab es are white we use the command
about the PSF, we can resort to "blind deconvolution" for inferring th U.rirlg ill? > operrtror
This approach is discussed in Section 5.10. The focus of the remainder K = im2double(checkerboard(NP, M , N ) ) > 0 . 5 ; l~rotirlce.~ rr l o g i c a l
present section is on various techniques for modeling PSFs by using fu reslllt; i m 2 d o u b l e i.7
llserl ro prurilrcc. on
imf i l t e r and f s p e c i a l , introduced in Sections 3.4 and 3.5, respectively, an ages generated by function checkerboard are of class double with val- ilnrtgr o f cili.>.s
the various noise-generating functions discussed earlier in this chapter. d o u b l e . 1vllic11is
ecause some restoration algorithms are slow for large images, a good ap- c o ~ z ~ i s 111ith
~ c ~the~r
One of the principal degradations encountered in image restoration pro o ~ i i p ~ r i f o r t ~o~fr r r
lems is image blur. Blur that occurs with the scene and sensor at rest with r ch is to experiment with small images to reduce computation time and ~'IIIIC~~OI?
spect to each other can be modeled by spatial or frequency domain lowpas improve interactivity. In this case, it is useful for display purposes to be checkerboard.
168 Chapter 5 ~ Image Restoration 5.6 a Direct Inverse Filtering 169
able to zoom an image by pixel replication. The following function does th ate that the PSF is just a spatial filter. Its values are
(see Appendix C for the code):
B = pixeldup(A, m , n)
0 0 0 0 0 0.0145 0
0 0 0 0 0.0376 0.1283 0.0145
This function duplicates every pixel in A a total of rn times in the vertical dire 0
0 0 0 0.0376 0.1283 0.0376
tion and n times in the horizontal direction. If n is omitted, it defaults to m. 0 0 0.0376 0.1283 0.0376 0 0
0 0.0376 0.1283 0.0376 0 0 0
EXAMPLE 5.7: B Figure 5.7(a) shows a checkerboard image generated by the command 0.0145 0.1283 0.0376 0 0 0 0
Modeling a 0 0.0145 0 0 0 0 0
blurred, noisy >> f = checkerboard(8);
image.
noisy pattern in Fig. 5.7(c) was generated using the command
The degraded image in Fig. 5.7(b) was generated using the commands
oise = imnoise(zeros(size(f)), 'gaussian',0, 0.001);
>> PSF = fspecial('motion',7, 45);
>> gb = imfilter(f, PSF, 'circular'); ally, we would have added noise to gb directly using imnoise(gb,
ssian ' , 0, 0.001 ) . However, the noise image is needed later in this
FIGURE 5.7
(a) Original
image. (b) Image
blurred using
f speclal w ~ t h
len = 7 , and
theta = -45
degrees
(c) No~seImage.
(d) Sum of (b)
and (c).
170 Chapter 5 r Image Restoration 5.7 A.. Wiener Filtering 171
edgetaper J = e d g e t a p e r ( 1 , PSF)
ratio, R, discussed earlier in this section, was obtained using the original
This function blurs the edges of the input image, I , using the point spread fu ise images from Example 5.7:
tion, PSF.The output image, J , is the weighted sum of I and its blurred j~ersi
The weighting array, determined by the autocorrelation function L . P n = abs(fft2(noise)).^2; % noise power spectrum
makes J equal to I in its central region, and equal to the blurred version o A = sum(Sn(:))/prod(size(noise)); % noise average power
near the edges. f = abs(fft2(f))."2; % image power spectrum
A = sum(Sf(:))/prod(size(f)); % image average power
EXAMPLE 5.8: Figure
@ i 5.8(a) is the same as Fig. 5.7(d), and Fig. 5.8(b) was obtained u
Using function the command
deconvwnr to
restore a blurred.
noisy image. >> f r l = deconvwnr(g, PSF) ;
2 = deconvwnr(g, PSF, R);
FIGURE 5.8
(a) Blurred, noisy
image. (b) Result
of inverse ORR = fftshift(real(ifft2(Sn)));
filtering. ORR = fftshift(real(ifft2(Sf)));
(c) Result of
Wiener filtering r3 = deconvwnr(g, PSF, NCORR, ICORR);
using a constant
ratio. (d) Result
of Wiener filtering
using
autocorrelation
functions. accomplished with Wiener deconvolution in this case. The challenge in
e, when one (or more) of these quantities is not known, is the intelligent
ns used in experimenting, until an acceptable result is
I
1 M-1 N-1
h ( x , y ) * f ( x , Y ) = -C, C, f ( m ,n ) h ( x - m, Y - n )
MNm=o n=O
a b
FIGURE 5.9
(a) The image in
Fig. 5.7(d)
restored using a
regularized filter
with NOISEPOWER
equal to 4. (b) The
same image
form of the function restored with
NOISEPOWERequal
to 0.4 and a RANGE
of [Ie-7 l e 7 ] .
176 Chapter 5 r Image Restoration 5.9 sr Iterative Nonlinear Restoration Using the Lucy-Ricl~ardso~
where g and PSF are from Example 5.7.The image was improved so he estimate of the undegraded
the original, but obviously this is not a particularly good value for NO
After some experimenting with this parameter and parameter RANGE,
at the result in Fig. 5.9(b), which was obtained using the command
most nonlinear methods, the question of when to stop the L-R al-
>> f r = deconvreg(g, PSF, 0 . 4 , [le-7 l e 7 1 ) ; difficult to answer in general.The approach often followed is to ob-
stop the algorithm when a result acceptable in a given
Thus we see that we had to go down one orde
n has been obtained.
and RANGE was tighter than the
-R algorithm is implemented in IPT by function deconvlucy, which
Fig. 5.8(d) is much better, but we o
the noise and image spectra. Without that information, the results obtai
by experimenting with the two filters often are comparable.
f r = deconvlucy(g, PSF, NUMIT, DAMPAR, WEIGHT) deconvlucy
If the restored image exhibits ringing introduced by the discrete Fou
transform used in the algorithm, it usually helps to use function edgeta f r is the restored image, g is the degraded image, PSF is the point
(see Section 5.7) prior to calling deconvreg. function, NUMIT is the number of iterations (the default is lo), and
and WEIGHT are defined as follows.
AR is a scalar that specifies the threshold deviation of the resulting
Iterative Nonlinear Restoration Using the rom image g. Iterations are suppressed for the pixels that deviate
Lucy-Richardson Algorithm the DAMPAR value from their original value. This suppresses noise gen-
preserving necessary image details.The default is 0 (no
The image restoration methods discuss
linear. They also are "direct" in the sens
specified, the solution is obtained via one ap
ity of implementation, coupled with modes can be excluded from the solution by assigning to it a zero weight
a well-established theoretical base, have made linear techniques a fundam other useful application of this array is to let it adjust the weights of
tal tool in image restoration for many years.
During the past two decades, nonlinear iterative techniques have been
ing acceptance as restoration tools that often yield results superio
obtained with linear methods. The principal objections to nonlinea
are that their behavior is not always predictable and that they generally
quire significant computational resources. The first objection often loses i
portance based on the fact that nonlinear methods have been shown to
superior to linear techniques in a broad spectrum of applications (Jans inging introduced by the discrete Fourier
[1997]).The second objection has become less of an issue due to th rithm, it sometimes helps to use function edgetaper
increase in inexpensive computing power o
method of choice in the toolbox is a technique developed
119721 and by Lucy [1974], working independently. The toolbox re gure 5.10(a) shows an image generated using the command EXAMPLE
method as the Lucy-Richardson (L-R) algorithm, but we also see it quote Using funcli
the literature as the Richardson-Lucy algorithm. deconvlucy
The L-R algorithm arises from a maximum-likelihood formulation ( f = checkerboard(8); restore a b l ~
Section 5.10) in which the image is modeled with Poisson statistics. Maxim noisy image.
ing the likelihood function of the model yields an equation that is satisfi are image of size 64 x 64 pixels. As before, the size of
when the following iteration converges: image was increased to size 512 x 512 for display purposes by using func-
j k + 1 ( ~ ,Y ) = jk(x, Y )
imshow(pixeldup(f, 8 ) ) ;
178 Chapter 5
FIGURE 5.1 0
(a) Original
image. (b) Image
blurred and
corrupted by
Gaussian noise.
(c) through (f)
Image (b)
restored using the
L-R algorithm
with 5, 10,20, and
100 iterations.
respectively.
Chopfer 5 a Registration 183
ship between pixels in an image. Thev are often called iubber>lzeet ~rnj?d
tion, a process that takes two images of the same scene and aligns them <#
they can be merged for visualization, or for quantitative comparison. In 1
following sections, we discuss (1) spatial transformations and how to defi See Sectior~s2.10.6
and visualize them in MATLAB; (2) how to apply spatial transformations and 11.1.1fora dis-
images; and (3) how to determine spatial transformations for use in ima cussion of stn~crures.
registration.
FIGURE 5.12 A simple spatial transformation. (Note that the .ry-axes in this figur
not correspond to the image axis coordinate system defined in Section 2.1.1.
mentioned in that section, IPT on occasion uses the so-called spatial coordin
system in which y designates rows and x designates columns. This is the system
throughout this section in order to be consistent with IPT documentation on the
of geometric transformations.)
Image Restoration 5.1 1 a Geometric Transformations and Image Registration 185
>> wz
= [ I 1 ; 3 21;
>> XY = tformfwd(WZ, tform)
XY =
186 Chapter 5 <@ Image Restoration 5.1 1 % Geometric Transformations and Image Registration 187
s e t ( g c a , 'XAxisLocation', ' t o p ' )
xlabel('wl),ylabel('zl)
% Create t h e x-y p l o t .
subplot(1, 2, 2) FIGURE 5.1 3
p l o t ( x , y, ' b ' ) , a x i s equal, a x i s i j Visualizing affine
hold on transformations
plot(xl, y ' , ' b ' )
hold o f f
xlim(wx1imi.t~)
ylim(zy1imits) tforml.
s e t ( g c a , 'XAxisLocation', ' t o p ' ) (c) Grid 2.
xlabel('xl), ylabel('yl)
EXAMPLE 5.12: ## In this example we use vistformfwd to visualize the effect of sever
Visualizing affine ferent affine transforms. We also explore a n alternate way to create an
transforms using
v i s t f ormfwd.
tf orm using maketf orm. We start with an affine transform that scales ho
tally by a factor of 3 and vertically by a factor of 2:
> > T I = [ 3 0 0; 0 2 0; 0 0 I ] ;
>> tforml = maketform('affinel, T I ) ;
>> v i s t f o r m f w d ( t f o r m 1 , [ 0 1001, [ 0 1 0 0 1 ) ;
such as
>> T2 = [I 0 0; .2 1 0; 0 0 I ] ;
>> tform2 = m a k e t f o r m ( ' a f f i n e ' , T 2 ) ;
>> v i s t f o r m f w d ( t f o r m 2 , [ 0 1 0 0 ] , [ 0 1 0 0 1 ) ;
Figures 5.13(c) and (d) show the effect of the shearing transform on a grid.
A n interesting property of affine transforms is that the compositio
era1 affine transforms is also an affine transform. Mathematically, affin
forms can be generated simply by using multiplication of the T matrices.
next block of code shows how to generate and visualize an affine transfor
that is a combination of scaling, rotation, and shear.
a b
c d
e
FIGURE 5.14
Affine
transformations
of the
checkerboard
image.
(a) Or~glnal
Image (b) Linear
conformal
transformation
using the default
,, lmf ransf orm interpolation
(bilinear).
(c) Using nearest
ne~ghbor
~nterpolatlon.
(d) Spec~fyingan
alternate fill
value.
(e) Controlling
the output space
EXAMPLE 5.13: location so that
Spatially translation 1s
transforming visible.
images.
190 Chapter 5 (h Image Restoration 5.1 1 w Geometric Transformationsand Image Registration 191
i m t r a n s f orm:
>> 92 = i m t r a n s f o r m ( f , t f o r m , 'nearest');
e images were taken at different times using the same instrument, such
lite images of a given location taken several days, months, or even years
n either case, combining the images or performing quantitative analysis
mparisons requires compensating for geometric aberrations caused by
nces in camera angle, distance, and orientation; sensor resolution; shift
Function i m t r a n s f orm has several additional optional parameters that ect position; and other factors.
useful at times. For example, passing it a F i l l V a l u e parameter controls
color i m t r a n s f orm uses for pixels outside the domain of the input image:
>> 93 = i m t r a n s f o r m ( f , t f o r m , ' F i l l v a l u e ' , 0.5); 1 points using a test pattern and a version of the test pattern that has un-
projective distortion. Once a sufficient number of control points have
In Fig. 5.14(d) the pixels outside the original image are mid-gray instead of bl sen, IPT function c p 2 t f orm can be used to fit a specified type of spatial
Other extra parameters can help resolve a common source of confusion
garding translating images using i m t r a n s f o r m . For example, the follow
commands perform a pure translation:
>> T2 = [ l 0 0 ; 0 1 0 ; 5 0 50 I ] ;
>> t f o r m 2 = m a k e t f o r m ( ' a f f i n e 1 , T 2 ) ;
>> 94 = i m t r a n s f o r m ( f , t f o r m 2 ) ; FIGURE 5.1 5
Image registration
The result, however, would be identical to the original image in Fig. 5.14 based on control
points.
This effect is caused by default behavior of i m t r a n s f o r m . Specific (a) Original image
i m t r a n s f orm determines the bounding box (see Section 11.4.1 for a defini with control
points (the small
circles
superimposed on
the image).
(b) Geometrically
pute the result. XData is a two-element vector that specifies the location o distorted image
left and right columns of the output image; YData is a two-element vector with control
specifies the location of the top and bottom rows of the output image.The points.
lowing command computes the output image in the region betwe (c) Corrected
image using a
( x , y ) = (1, I ) and ( x , y) = (400,400). projective
transformation
>> 95 = i m t r a n s f o r m ( f , tform2,'XData', [ I 4001, 'YData', [ I 4001, ... inferred from the
'FillValtie', 0.5); control points.
A collection of spatial
transformations that are applied
lications that enhance the capabilities of an already large set of existing tools.
>> basepoints = [ 8 3 81; 450 56; 43 293; 249 392; 436 4421;
>> i n p u t p o i n t s = [68 66; 375 47; 42 286; 275 434; 523 5321;
>> t f o r m = c p 2 t f o r m ( i n p u t p o i n t s , basepoints, ' p r o j e c t i v e ' ! ;
>> gp = imtransform(g, t f o r m , 'XData' , [ I 5021, 'YData' , [I5021)
Sometimes it is necessary to approximate an indexed image by one with er colors in addition to the ones shown in Table 6.1 involve fractional val-
fewer colors. For this we use function imapprox, whose syntax is c e , [ . 5 . 5 . 5 ] isgray, [ . 5 0 0 ] isdarkred,and [ . 4 9 1 .831
[Y, newmap] = i m a p p r o x ( X , map, n) provides several predefined color maps, accessed using the
This function returns an array Y with colom~apnewmap, which has at most n color
The input array X can be of class u i n t 8 , u i n t l 6 , or double.The output Y is of clas colormap(map-name)
u i n t 8 if n is less than or equal to 256. If n is greater than 256, Y is of class double.
When the number of rows in map is less than the number of distinct integer val- ich sets the colormap to the matrix map-name; an example is
ues in X, multiple values in X are displayed using the same color in map. For exam-
ple, suppose that X consists of four vertical bands (ofequal width, with values I,@, > colormap(copper)
128, and 256. If we specify the colormap map = [ 0 0 0; 1 1 1 1, then all the el-
ements in X with value 1 would point to the first row (black) of the map and all the re copper is one of the prespecified MATLAB colormaps. The colors in
other elements would point to the second row (white). Thus, the command map vary smoothly from black to bright copper. If the last image displayed
imshow(x, map) would display an image with a black band followed by thr as an indexed image, this command changes its colormap to copper. Alter-
white bands. In fact, this would be true until the length of the map became 65, atively,the image can be displayed directly with the desired colormap:
which time the display would be a black band, followed by a gray band, followe
by two white bands. Nonsensical image displays cam result if the length of the map imshow(X, copper)
exceeds the allowed range of values of the elements of X.
ble 6.2 lists some of the colormaps available in MATLAB.The length (number
There are several ways to specify a color ma~p.One approach is to use th colors) of these colormaps can be specified by enclosing the number in paren-
statement
eses. For example,gray ( I 6 ) generates a colormap with 16 shades of gray.
>> map(k, :) = I r ( k ) g(k) b ( k ) l
1.3 IPT Functions for Manipulating RGB and Indexed Images
where [ r ( k ) g ( k ) b ( k ) 1 are RGB values that specify one row of a col- ble 6.3 lists the IPT functions suitable for converting between RGB, in-
ormap. The map is filled out by varying k.
xed, and gray-scale images. For clarity of notation in this section, we use
Table 6.1 lists the RGB values for some basic colors. Any of the three for-
b-image to denote RGB images, gray-image to denote gray-scale images,
mats shown in the table can be used to specify colors. For example, the back-
to denote black and white images, and X, to denote the data matrix compo-
ground color of a figure can be changed to green by using any of the following nt of indexed images. Recall that an indexed image is composed of an inte-
three statements:
r data matrix and a colormap matrix.
>> w h i t e b g ( ' g l ) Function d i t h e r is applicable both to gray-scale and color images. Dither-
>> w h i t e b g ( ' g r e e n l ) a process used mostly in the printing and publishing industry to give the
>> w h i t e b g ( [ O 1 0 1 ) 1 impression of shade variations on a printed page that consists of dots. In
e case of gray-scale images, dithering attempts to capture shades of gray by
oducing a binary image of black dots on a white background (or vice versa).
sizes of the dots vary, from small dots in light areas to increasingly larger
TABLE 6.1 for dark areas. The key issue in implementing a dithering algorithm is a
RGB values of Long name Short name
eoff between "accuracy" of visual perception and computational complex-
some basic colors. Black k 10 0 01 ty.The dithering approach used in IPT is based on the Floyd-Steinberg algo-
The long or short Blue b [O 0 11 hm (see Floyd and Steinberg [1975], and Ulichney [19871).The syntax used
names (enclosed Green 9 [ o 1 01 function d i t h e r for gray-scale images is
by quotes) can be Cyan c
used instead of Red [ I 0 01 bw = d i t h e r ( g r a y - i m a g e )
the numerical Magenta [ 1 0 11
triplet to specify Yellow 11 1 0 1
an RGB color. White ere, as noted earlier, gray-image is a gray-scale image and bw is the
11 1 11
thered result (a binary image).
200 Chapter 6 H Color Image Processing 6.1 a Color Image Representation
TABLE 6.2
Some of the i n d to reduce the number of colors in an image. This
MATLAB
predefined Function g r a y s l i c e has the syntax
colormaps.
X = grayslice(gray-image, n)
colorcube Contains as many regularly spaced colors in RGB color space as
possible, while attempting to provide more steps of gray, pure red,
pure green, and pure blue.
Consists of colors that are shades of cyan and magenta. It varies
smoothly from cyan to magenta.
Varies smoothly from black to bright copper. 1 -,
--, 2 ..., -
n-1
Consists of the colors red, white, blue, and black.This colormap ,a n n
completely changes color with each index increment.
Returns a linear gray-scale colormap.
Varies smoothly from black, through shades of red, orange, and
yellow, to white.
Varies the hue component of the hue-saturation-value color
model. The colors begin with red, pass through yellow, green, cyan, X = grayslice(gray-image, v)
blue, magenta, and return to red. The colormap is particularly
appropriate for displaying periodic functions. r whose values are used to threshold gray-image. When
Ranges from blue to red, and passes through the colors cyan, in conjunction with a colormap, g r a y s l i c e is a basic tool for pseudocol-
yellow, and orange.
age processing, where specified gray intensity bands are assigned differ-
Produces a colormap of colors specified by the ColorOrder
property and a shade of gray. Consult online help regarding colors. The input image can be of class u i n t 8 , u i n t l 6 , o r double. The
function ColorOrder. shold values in v must between 0 and 1, even if the input image is of class
Contains pastel shades of pink. The pink colormap provides sepia 8 or u i n t 16. The function performs the necessary scaling.
tone colorization of grayscale photographs. unction gray2ind, with syntax
Repeats the six colors red, orange, yellow, green, blue, and violet.
Consists of colors that are shades of magenta and yellow. [X, map] = gray2ind(gray_image, n)
gray-image = i n d 2 g r a y ( X , map)
TABLE 6.3
IPT functions for
converting
between RGB,
indexed, and gray- st in this chapter for function rgb2ind has the form
scale intensity
images.
[X, map] = rgb2ind(rgb_image, n , d i t h e r - o p t i o n )
a
b c
d e
FIGURE 6.4
(a) RGB image.
(b) Number of
colors reduced
to 8 without
dithering.
(c) Number of
colors reduced to
8 with dithering.
(d) Gray-scale
version of (a)
obtained using
function
rgb2gray.
(e) Dithered gray-
scale image (this
is a binary image).
EXAMPLE 6.1:
Illustration of
some of the
functions in
Table 6.3.
204 Chapter 6 $M (Zolor Image Processing 6.2 a Converting to Other Zolor Spaces 205
The image in Fig. 6.4(e) is a binary image, which again represents a sign
degree of data reduction. By looking at Figs. 6.4(c) and (e), it is clear
dithering is such a staple in the printing and publishing industry, especial
situations (such as in newspapers) where paper quality and printing resolu
are low. unction ntsc2rgb implements this equation:
rgb-image = ntsc2rgb(yiq_image)
Converting to Other Color Spaces
As explained in the previous section, the toolbox represents colors as RGB the input and output images are of class double.
ues, directly in an RGB image, or indirectly in an indexed image, where the
ormap is stored in RGB format. However, there are other color spaces ( The YCbCr Color Space
called color models) whose use in some applications may be more conven CbCr color space is used widely in digital video. In this format, luminance
and/or appropriate. These include the NTSC, YCbCr, HSV, CMY, CMYK, ation is represented by a single component, Y, and color information is
HSI color spaces. The toolbox provides conversion functions from RGB to as two color-difference components, Cb and Cr. Component Cb is the dif-
NTSC, YCbCr, HSV and CMY color spaces, and back. Functions for conver between the blue component and a reference value, and component Cr is
to and from the HSI color space are developed later in this section.
conversion function is
ycbcr-image = rgb2ycbcr(rgb_image)
rgb-image = ycbcr2rgb(ycbcr_image)
input YCbCr image can be of class uint8, u i n t l 6 , or double.The output To see the tramforma-
ge is of the same class as the input. tion matrix used to
convertfrom YCbCr
to RGB, type the fol-
lowing command at
an image. Function rgb2ntsc performs the transformation: the prompt:
(hue, saturation, value) is one of several color systems used by people to >> e d i t ycbcr2rgb
t colors (e.g., of paints or inks) from a color wheel or palette. This color
yiq-image = rgb2ntsc(rgb_image) is considerably closer than the RGB system to the way in which hu-
ere the assumption is that all color values have been normalized to the range
n demonstrates that light reflected from a surface coated with
t contain red (that is, C = 1 - R in the equation). Similarly,
inant color in printing), a fourth color, black, is added, giving rise to the
color model. Thus, when publishers talk about "four-color printing,"
0" axis. Value is measured along the axis of the cone. The V = 0 end of the
is black. The V = 1 end of the axis is white, which lies in the center of the
color hexagon in Fig. 6.5(a). Thus, this axis represents all shades of gray. Sat m RGB to CMY
tion (purity of the color) is measured as the distance from the V axis.
cmy-image = imcomplernent(rgb-image)
hsv-image = rgb2hsv(rgb_image)
The input RGB image can be of class uint8, uintl6, or double; the out
image is of class d o u b l e . T h e function for converting from HSV back to R
is hsv2rgb:
orange, or red), whereas saturation gives a measure of the degree to
rgb-image = hsv2rgb(hsv_image) olor is diluted by white light. Brightness is a subjective descrip-
to measure. It embodies the achromatic no-
The input image must be of class double. The output also is of class d o u b l
ost useful descriptor of monochromatic im-
easurable and easily interpretable.
6.2.4 The CMY and CMYK Color Spaces
Cyan, magenta, and yellow are the secondary colors of light or, alternati
the primary colors of pigments. For example, when a surface coated with
pigment is illuminated with white light, no red light is reflected
face. That is, the cyan pigment subtracts red light from reflected
which itself is composed of equal amounts of red, green, and blue light.
208 Chapter 6 m Color Image Processing 6.2 1 Converting to Other Color Spaces 209
similar, but its focus is on presenting colors that are meaningful when interpre
ed in terms of a color artist's palette.
As discussed in Section 6.1.1, an RGB color image is composed FlGlJRE 6.7 Hue and
monochrome intensity images, so it should come as no surprise that saturat~onin the HSI
be able to extract intensity from an RGB image.This becomes quite c color model.The dot
is an arbitrary color
take the color cube from Fig. 6.2 and stand it on the black, (0,0, O), ver pomt.The angle from
with the white vertex, (1,1, I), directly above it, as Fig. 6.6(a) shows. the red axis gives the
in connection with Fig. 6.2, the intensity is along the line joining the hue, and the length of
tices. In the arrangement shown in Fig. 6.6, the line (intensity axis) j the vector is the
black and white vertices is vertical. Thus, if we wanted to determine saturation.The
intensity of all colors
sity component of any color point in Fig. 6.6, we would simply pass a in any of these planes
perpendicular to the intensity axis and containing the color point. The is given by the
section of the plane with the intensity axis would give us an intensity va position of the plane
the range [O,l]. We also note with a little thought that the saturation (p on the vertical
of a color increases as a function of distance from the intensity axis. In fact, Intensity axis.
saturation of points on the intensity axis is zero, as evidenced by the fact t
all points along this axis are gray. g discussion, we see that the HSI space consists of a
In order to see how hue can be determined from a given RGB point, co 1intensity axis and the locus of color points that lie on a plane perpen-
sider Fig. 6.6(b), which shows a plane defined by three points, (black, to this axis. As the plane moves up and down the intensity axis, the
and cyan). The fact that the black and white points are contained in the pl
tells us that the intensity axis also is contained in the plane. Furthermore,
see that all points contained in the plane segment defined by the intensi down its gray-scale axis, as shown in Fig. 6.7(a). In
and the boundaries of the cube have the same hue (cyan in this case). Thi rimary colors are separated by 120".The secondary
because the colors inside a color triangle are various combinations or mixture
of the three vertex colors. If two of those vertices are black and white, and t
third is a color point, all points on the triangle must have the same hue sin
the black and white components do not contribute to changes in h
course, the intensity and saturation of points in this triangle do change). By
tating the shaded plane about the vertical intensity axis, we would obtain
ferent hues. From these concepts we arrive at the conclusion that the ical axis) is the length of the vector from the origin
saturation, and intensity values required to form the HSI space can be origin is defined by the intersection of the color
tained from the RGB color cube. That is, we can convert any RGB point with the vertical intensity axis. The important components of the HSI
corresponding point is the HSI color model by working out the geometri space are the vertical intensity axis, the length of the vector to a color
formulas describing the reasoning just outlined in the preceding discussion. tor makes with the red axis. Therefore, it is not un-
efined is terms of the hexagon just discussed, a tri-
a b White gs. 6.7(c) and (d) show. The shape chosen is not
FIGURE 6.6
Relationship
between the RGB
and HSI color
models. verting Colors from RGB to HSI
Black
210 Chapter (i a Color Image Processing 6.2 . Converting to Other Color Spaces
e = COS-I
; [ ( R- G ) + ( R - B ) ]
[ ( R - G ) +~ ( R - B ) ( G - B ) ] ' / ~
saturation component is given by
I
FIGURE 6.8 The
HSI color model 3
S = l -
based on (a)
triangular and (b)
(R+G + B ) [min(R, G , B ) ]
circular color ally, the intensity component is given by
planes. The
triangles and 1
circles are I = -(R
3
+ G + B)
perpendicular to
the vertical assumed that the RGB values have been normalized to the range [0,11,
intensity axis.
that angle 0 is measured with respect to the red axis of the HSI space, as in-
ted in Fig. 6.7. Hue can be normalized to the range [O,1]by dividing by 360"
alues resulting from the equation for H. The other two HSI components al-
dy are in this range if the given RGB values are in the interval [O,l ] .
sector (0" 5 H < 120"): When H i s in this sector, the RGB components
given by the equations
B = Z(1 - S )
R=I l+
[
S cos H
cos(60 -H) 1
G = 31 - ( R + B )
and = sqrt((r -
~ 7 1 . ~+2 ( r - b ) . * ( g - b));
a = acos(num./(den + e p s ) ) ;
B = 31 - (R + G)
BR sector (240" I: H 5 360"): Finally, if H is in this range, we subtract 2 b > g) = 2*pi - H(b > g ) ;
from it:
H = H - 240" = min(min(r, g ) , b ) ;
and
S cos H
cos(60 - H ) 1 ombine a l l t h r e e r e s u l t s i n t o an h s i image.
= c a t ( 3 , H, S , I ) ;
R = 3 1 - (G + B)
Use of these equations for image processing is discussed later in this chapter. M-function for Converting from HSI to RGB
following function,
An M-function for Converting from RGB to HSI
The following function, rgb = h s i 2 r g b ( h s i )
h s i = rgb2hsi(rgb) lements the equations for converting from HSI to RGB. The documenta-
on in the code details the use of this function.
implements the equations just discussed for converting from RGB to HSI
simplify the notation, we use rgb and h s i to denote RGB and HSI images, ction rgb = h s i 2 r g b ( h s i )
spectively. The documentation in the code details the use of this function. I2RGB Converts an HSI image t o RGB.
RGB = HSI2RGB(HSI) converts an HSI image t o RGB, where HSI
function hsi = rgb2hsi(rgb) i s assumed t o be of c l a s s double w i t h :
%RGB2HSI Converts an RGB image to HSI. h s i ( : , : , 1 ) = hue image, assumed t o be i n t h e range
% HSI = RGB2HSI(RGB) converts an RGB image to HSI. The input image [O, 11 by having been divided by 2 * p i .
% i s assumed to be of size M-by-N-by-3, where the third dimension h s i ( : , : , 2 ) = s a t u r a t i o n image, i n t h e range [ O , I ] .
% accounts for three image planes: red, green, and blue, i n that h s i ( : , :, 3 ) = i n t e n s i t y image, i n t h e range [ 0 , 1 1 .
% order. If a l l RGB component images are equal, the HSI conversion
% i s undefined. The i n p u t image can be of class double ( w i t h values The components of t h e output image a r e :
% i n the range [ O , I ] ) , uint8, or uintl6. r g b ( : , :, 1 ) = r e d .
% r g b ( : , :, 2 ) = green.
% The output image, HSI, i s of class double, where: r g b ( : , :, 3 ) = blue.
% h s i ( : , : , 1 ) = hue image normalized to the range [0, 11 by Extract t h e i n d i v i d u a l HSI component images.
% dividing a l l angle values by 2*pi. = h s i ( : , :, 1 ) * 2 * p i ;
% h s i ( : , :, 2) = saturation image, i n the range [0, I ] .
% h s i ( : , :, 3) = intensity image, i n the range [0, I ] . = hsi(:, :, 3);
% E x t r a c t t h e i n d i v i d u a l component immages. Implement t h e conversion equations.
rgb = im2double(rgb); = zeros(size(hsi, I ) , size(hsi, 2 ) ) ;
r = rgb(: , :, I ) ; = zeros(size(hsi, I ) , size(hsi, 2 ) ) ;
g = r g b ( : , :, 2 ) ; = zeros(size(hsi, l ) , size(hsi, 2 ) ) ;
b = r g b ( : , :, 3 ) ;
RG sector (0 <= H < 2*pi/3).
% Implement t h e conversion equations. dx = find( (0 <= H ) & ( H < 2 * p i / 3 ) ) ;
num = 0 . 5 * ( ( r - g ) + ( r - b ) ) ; (idx) = I ( i d x ) .* ( 1 - S ( i d x ) ) ;
214 Chapter 6 sp Color Image Processing 6.3 The Basics of Color Image Processing 215
R(idx) = I(idx) . * ( 1 + S(idx) . * cos(H(idx)) . / . .. 0" to 360' (i.e., from the lowest to highest possible values of hue). This
cos(pi13 - H(idx))) ; cisely what Fig. 6.9(a) shows because the lowest value is represented as
G ( i d x ) = 3*I(idx) - (R(idx) + B ( i d x ) ) ; and the highest value as white in the figure.
% BG s e c t o r (2*pi/3 <= H < 4 * p i / 3 ) . e saturation image in Fig. 6.9(b) shows progressively darker values to-
idx = f i n d ( ( 2 * p i / 3 <= H ) & ( H < 4*pi/3) ) ; d the white vertex of the RGB cube, indicating that colors become less and
R(idx) = I ( i d x ) . * (1 - S ( i d x ) ) ; saturated as they approach white. Finally, every pixel in the intensity
G(idx) = I ( i d x ) .* (1 + S ( i d x ) . * cos(H(idx) - 2*pi/3) . I ... ge shown in Fig. 6.9(c) is the average of the RGB values at the corre-
cos ( p i - H ( i d x ) ) ) ; nding pixel in Fig. 6.2(b). Note that the background in this image is white
B(idx) = 3 * I ( i d x ) - (R(idx) + G ( i d x ) ) ; ause the intensity of the background in the color image is white. It is black
% BR sector. e other two images because the hue and saturation of white are zero.
idx = f i n d ( (4*pi/3 <= H ) & ( H <= 2 * p i ) ) ;
G ( i d x ) = I ( i d x ) .* ( 1 - S ( i d x ) ) ; The Basics of Color Image Processing
B(idx) = I ( i d x ) .* (1 + S(idx) . * cos(H(idx) - 4*pil3) . / . . .
cos(5*pi/3 - H(idx))); his section we begin the study of processing techniques applicable to color
R(idx) = 3*I(idx) - ( G ( i d x ) + B ( i d x ) ) ; ges. Although they are far from being exhaustive, the techniques devel-
d in the sections that follow are illustrative of how color images are han-
% Combine a l l t h r e e r e s u l t s i n t o an RGB image. Clip t o [ 0 , 1 1 t
led for a variety of image-processing tasks. For the purposes of the following
% compensate f o r f l o a t i n g - p o i n t arithmetic rounding e f f e c t s .
on we subdivide color image processing into three principal areas:
rgb = c a t ( 3 , A, G , B ) ;
rgb = max(min(rgb, I ) , 0 ) ; r transformations (also called color mappings); (2) spatial processing of
ual color planes; and (3) color vectorprocessing. The first category deals
'
EXAMYLE6.2: @ Figure 6.9 shows the hue, saturation, and intensity components of recessing the pixels of each color plane based strictly on their values and
Convertingfronl image of an RGB cube on a white background, similar to the image their spatial coordinates. This category is analogous to the material in
RGB HS1. Fig. 6.2(b). Figure 6.9(a) is the hue image. Its most distinguishing feature 3.2 dealing with intensity transformations. The second category deals
the discontinuity in value along a 45" line in the front (red) plane of the spatial (neighborhood) filtering of individual color planes and is analo-
cube. To understand the reason for this discontinuity, refer to Fig. 6.2( s to the discussion in Sections 3.4 and 3.5 on spatial filtering.
draw a line from the red to the white vertices of the cube, and select a po e third category deals with techniques based on processing all compo-
in the middle of this line. Starting at that point, draw a path to the right, fo of a color image simultaneously. Because full-color images have at least
lowing the cube around until you return to the starting point.The major components, color pixels really are vectors. For example, in the RGB sys-
ors encountered on this path are yellow, green, cyan, blue, magenta, and b ,each color point can be interpreted as a vector extending from the origin
to red. According to Fig. 6.7, the value of hue along this path should incr that point in the RGB coordinate system (see Fig. 6.2).
Let c represent an arbitrary vector in RGB color space:
This cquation indicates that thc components of c are simply the RGB compo-
' nents of a color image at a point. We take into account the fact that thc color
components are a function of coordinales ( x , y) by using the notation
R(s, y)
: For an imagt: of size .%I x N , there are .4lN such vzctors, c ( x , y ) , for
a b c = 0 , 1 , 2,..., M - 1 a n d y = 0 , 1 , 2 ,..., N - 1.
FIGURE 6.9 HSI component images of an image of an RGB color cube. (a) Hue, (b) saturation, and ( In some cases, equivalent results are obtained whether color images are
intensity images. ocessed one plane at a time or as vector quantities. However, as explained in
216 Color Image Processing
Chapter 6 !i# 6.4 Color Transformations 217
a b r1
FIGURE 6.10
Spatial masks for transformation functions { T I ,T2,.. . , K,} are
gray-scale and
RGB color
images. of the gray-scale transformations introduced in Chapter 3, like
Spatial mask _f Spatial mask ement, which computes the negative of an image, are independent of
ay-level content of the image being transformed. Others, like histeq,
ive, but the transformation
stimated. And still others,
Color Transformations
The techniques described in this section are based on processing
components of a color image or intensity component of a monochrome imag z = interplq([O 255]', [ 0 2 5 5 ] ' , [O: 2 5 5 1 ' )
within the context of a single color model. For color images, we rest a 256-element one-to-one mapping connecting control points (0,O)
tion to transformations of the form ,255)-thatis, z = [ 0 1 2 ..
. 2551 ' .
s i = T , ( r i ) , i = 1 , 2 ,..., n
where ri and si are the color components of the input and output images, n is
the dimension of (or number of color components in) the color space of ri, and
the Ti are referred to as fill-color transformation (or mapping) functions.
If the input images are monochrome, then we write an equation of the form
si = G ( r ) , i = 1,2,. . . , n
where r denotes gray-level values, si and T, are as above, and n is the number of
color components in si. This equation describes the mapping of gray levels into
arbitrary colors, a process frequently referred to as a pseudocolor transforma-
tion or pseudocolor mapping. Note that the first equation can be used to process mapping functions using control points: (a) and (c) linear interpolation, and
monochrome images in RGB space if we let rl = r2 = r3 = r. In either case, the
218 Chopter 6 Color Image Processing 6.4 api Color Transformations 219
In a similar manner, cubic spline interpolation is implemented using t
s p l i n e function,
,>>,
id:\
?,;:.>
;.. S P % ~
z = spline(x, y, x i )
,
of the syntax of function ice:
% Only the ice graphical
over, if y contains two more elements than x, its first and last entries are % interface i s displayed.
sumed to be the end slopes of the cubic spline. The function depicted % Shows and returns the mapped
% image g .
= ice('imagei, f , 'wait', 'off ' ) ; % Shows g and returns
% the handle.
= ice( 'image', f , 'space', ' hsi' ) ; % Maps RGB image f i n HSI space.
editing) function does precisely this. Its syntax is that when a color space other than RGB is specified, the input image
ther monochrome or RGB) is transformed to the specified space before
r--------
ice g = i c e ( ' P r o p e r t y Name', 'Property V a l u e ' , . . .)
The developmentof where ' Property Name ' and ' Property Value ' must appear in pairs,
firnction ice, given the dots indicate repetitions of the pattern consisting of corresponding in
in Appendix B, is a pairs. Table 6.4 lists the valid pairs for use in function ice. Some examples
comprehensive illus-
tration of how to de-
sign a graphical user
interface (GUz) in
M A T L AB.
result is image g. When ' off ' is selected, g is the handlet of the proce
image, and control is returned immediately to the command window; ther
fore, new commands can be typed with the i c e function still active. To obta
,--
the properties of an image with handle g we use the g e t function
,,&:,
.,..I>,
- :3:L;r,*?:,, a '
h = get(g)
.( ,. .
TABLE 6.4
Valid inputs for
function ice. An RGB or monochrome input image, f, to be transformed b
interactively specified mappings.
The color space of the components to be modified. Possible
valuesare ' r g b ' , 'cmy', ' h s i ' , 'hsv', ' n t s c ' (or 'yiql),an
'ycbcrl.The default is 'rgb'.
directly.
220 Chapter 6 Color Image Processing 6.4 pl Color Tran sformations 221
TABLE 6.5
Manipulating FIGURE 6.1 3
control points (a) A negative
with the mouse. mapping function,
and (b) its effect
on the
monochrome
image of Fig. 6.12.
EXAMPLE 6.4: W Consider next the use of function i c e for monochrome and color contrast e red, green, and blue components of the input images in Examples 6.3 and
Monochrome and manipulation. Figures 6.15(a) through (c) demonstrate the effectiveness of e mapped identically-that is, using the same transformation function. To
'Ontrast I c e in processing monochrome images. Figures 6.15(d) through (f) show specification of three identical functions, function i c e provides an "all
enhancement. nts" function (the RGB curve when operating in the RGB color space)
similar effectiveness for color inputs. As in the prevlous example, map
functions that are not shown remain in their default or 1 : 1state. In both used to map all input components. The remaining examples demonstrate
cessing sequences, the Show PDF checkbox is enabled. Thus, the histogra rmations in which the three components are processed differently.
the aerial photo in (a) is displayed under the gamma-shaped mapping func-*
tion (see Section 3.2.1) in (c); and three histograms are provided in (f) for As noted earlier, when a monochrome image is represented in the RGB EXAMPLE 6.5:
the color image in (d)-one for each of its three color components. Although color space and the resulting components are mapped independently, the Pseudocolor
the S-shaped mapping function in (f) increases the contrast of the image iq transformed result is a pseudocolor image in which input image gray levels mappings.
(d) [compare it to (e)], it also has a slight effect on hue. The small change of have been replaced by arbitrary colors.Transformations that do this are useful
color is virtually imperceptible in (e), but is an obvious result of the ma uman eye can distinguish between millions of colors-but rela-
ping, as can be seen in the mapped full-color reference bar in (f). Recall fr des of gray. Thus, pseudocolor mappings are used frequently to
the previous example that equal changes to the three components of e small changes in gray level visible to the human eye or to highlight im-
RGB image can have a dramatic effect on color (see the color complem nt gray-scale regions. In fact, the principal use of pseudocolor is human
mapping in Fig. 6.14). tion-the interpretation of gray-scale events in an image or sequence
s via gray-to-color assignments.
(a) is an X-ray image of a weld (the horizontal dark region) con-
FIGURE 6.1 6
(a) X-ray of a
defective weld;
(b) a pseudo-
color version of
the weld; (c) and
(dl mapping
functions for the
green and blue
components.
(Original image
courtesy of X-
TEK Systems,
Ltd.)
224 Chapter 6 81 Color Image Processing 6.4 9 Color Transformat~ons 225
the input in a variety of color spaces, as detailed in Table 6.4.To inter-
ents of RGB image f 1, for example, the ap-
Histogram equalization is a gray-level mapping process that seeks to pro- EXAMPLE 6.7:
EXAMPLE 6.6: BB Figure 6.17 shows an application involving a full-color image, in which it tensity histograms. As discussed in Hlstogram based
Color balancing advantageous to map an image's color components independently. Common ion is the cumulative distribution mappings.
called color balancing or color correction, this type of mapping has been he gray levels in the input image. Because color images
nents, the gray-scale technique must be modified to han-
more than one component and associated histogram. As might be expect-
it is unwise to histogram equalize the components of a color image
sult usually is erroneous color. A more logical approach
are possible when white areas, where the RGB or CMY components should nsities uniformly, leaving the colors themselves (i.e., the
equal, are present. As can be seen in Fig. 6.17, skin tones also are excellen
samples for visual assessments because humans are highly perceptive of prop e of a caster stand containing cruets and
er skin color. ers. The transformed image in Fig. 6.18(b), which was produced using the
Figure 6.17(a) shows a CMY scan of a mother and her child with an exces Figs. 6.18(c) and (d), is significantly brighter. Several of
ble on which the caster is resting are
t was mapped using the function in
es the CDF of that component (also dis-
e hue mapping function in Fig. 6.18(d) was selected to
or perception of the intensity-equalized result. Note
e input and output image's hue, saturation, and inten-
ponents are shown in Figs. 6.18(e) and (f), respectively. The hue com-
are virtually identical (which is desirable), while the intensity and
ere altered. Finally note that, to process an RGB
ce, we included the input property nametvalue pair
El
FIGURE 6.17 Uslng function i c e for color balancing: (a) an image heavy in magenta; (b) the correcte ere f 2 is an RGB image generated by I c e and f 3 1s a standard MATLAB
Image; and (c) the mapplng function used to correct the ~mbalance.
6.5 ira Spatial Filtering of
226 Chapter b Color Image Processing
s i a n ' (see Table 3.4). Once a filter has been generated, fil-
by using function imf i l t e r , introduced in Section 3.4.1.
228 Chnpfer 6 EI
EXAMPLE 6.8:
Color image
smoothing.
230 Chapter Li &r Color Image Processing 6.6 r Working Directly in RGB Vector Space 231
FIGURE 6.22
(a) Blurred image.
(b) Image
enhanced using
the Laplaclan,
followed by
contrast
enhancement
using function
Ice.
Clearly, the two filtered results are quite different. For example, in additio
EXAMPLE 6.9:
Color image
the Laplacian filter mask sharpening.
V 2 [ c ( x y, ) ] =
[
V 2 R ( x ,Y )
V2G(x, Y )
V2BIX,*I 1
which, as in the previous section, tells us that we can compute the Laplacian
.AS
Working Directly in RGB Vector Space
. mentioned in Section 6.3, there are cases in which processes based on indi-
vldual color planes are not equivalent to working directly in RGB vector
space. This is demonstrated in this section, where we illustrate vector process-
a full-color image by computing the Laplacian of each component ima "ing by considering two important applications in color image processing: color
separately.
-
Chapter 3010r Image Processing 6.6 m Working Directly in RGB Vector Space 235
and that the value of the rate of change (i.e., the magnitude of the gradient)
the directions given by the elements of 0(x, y) is given by
1
F d x , Y) = {?[(& + gyy) + (gxx - gyy) cos 20 + 2gxy sin 201
Note that 0(x, y) and FO(x,y) are images of the same size as the input i
The elements of B(x, y) are simply the angles at each point that the grad
calculated, and Fo(x, y) is the gradient image.
these results is rather lengthy, and we would gain little in terms of the
mental objective of our current discussion by detailing it here. The interes
FIGURE 6.25
(a) RGB image.
(b) Gradient Following conven-
computed in R G tion, we use a super-
script, 7; to indicate
vector space. vector or matrix
(c) Gradient transposition and a
computed as in normal, inline, T to
Fig. 6.24(f). denote a threshold
(d) Absolute value. Care should
difference be exercised not to
between (b) and confuse these unre-
(c), scaled to the lared w e s of the
same variable.
range [O, 11.
a b
FIGURE 6.26 Two
approaches for
enclosing data in
RGB vector space
for the purpose of
segmentation.
238 Chapter 6 a Color Image Processing 6.6 8 Working Directly in RGB Vector Space
k = roipoly(f); % S e l e c t region i n t e r a c t i v e l y .
See Section 12.2 for where C is the covariance matrixt of the samples representative of the c = immultiply(mask, f ( : , :, 1 ) ) ;
a detailed discursion
on efficient imple-
we wish to segment. This distance is commonly referred to as the Mahalan en = immultiply(mask, f ( : , :, 2 ) ) ;
mentations for com- distance.The locus of points such that D ( z , m) IT describes a solid 3-D e e = immultiply(mask, f ( : , :, 3 ) ) ;
puting the Euclidean tical body [see Fig. 6.26(b)] with the important property that its principal a c a t ( 3 , r e d , green, b l u e ) ;
and Mahalanobis
distances.
are oriented in the direction of maximum data spread. When C = I, the id
tity matrix, the Mahalanobis distance reduces to the Euclidean distance. S
mentation is as described in the preceding paragraph, except that the data ask is a binary image (the same size as f) with 0s in the background
now enclosed by an ellipsoid instead of a sphere. the region selected interactively.
Segmentation in the manner just described is implemented by funct we compute the mean vector and covariance matrix of the points in
colorseg (see Appendix C for the code), which has the syntax ,but first the coordinates of the points in the ROI must be extracted.
where method is either ' euclidean ' or ' mahalanobis ' ,f is the RGB i
eshape(g, M * N, 3 ) ; % reshape i s discussed i n Sec. 8.2.2.
a b
-
now vroceed to segment the image " using values of T equal to multiples
hich is an approximation to the largest standard deviation: T = 25,50,
FIGURE 6.27 .For the ' euclidean ' option with T = 25, we use
(a) Pseudocolor
of the surface of = c o l o r s e g ( ' e u c l i d e a n ' , f , 25, m);
Jupiter's Moon 10.
(b) Region of
interest extracted ure 6.28(a) shows the result, and Figs. 6.28(b) through (d) show the seg-
interactively using ntation results with T = 50, 75, 100. Similarly, Figs. 6.29(a) through (d)
function roipoly.
(Original image
courtesy of
NASA.)
a b easing values of T. The reason is that the 3-D color data spread in the
c d fitted much better in this case with an ellipsoid than with a sphere.
FIGURE 6.28 at in both methods increasing T allowed weaker shades of red to be
(a) through d in the segmented regions, as expected.
(d) Segmentation
of Fig. 6.27(a)
using option
'euclidean ' in
funct~on
colorseg with
T = 25,50,75,
and 100,
respectively.
FIGURE 6.29
(a) through
(d) Segmentation
of Fig. 6.27(a)
using option
'mahalanobls'
in function
colorseg with
T = 25,50,75,
and 100,
respectively.
Compare with
Fig. 6.28.
7.1 3 Background 243
,"~2
I
and h,, , in these equations are called forward and Inverse trans-
on kernels, respectively. They determine the nature, computational
=
tions f f t2 and l f f t2 in Chapter 4.
Background
Conslder an image f ( x ,y ) of size M x N whose forward, discrete transfor
transformat~onsthat differ not only in the transformation kernels em-
T ( u ,v,.. .) = Zf( x ,y ) g u , ,
x, Y
( x ,Y )
FIGURE 7.1
(a) The familiar
Fourier expansion
A-.
+w-
orthogonal to its integer translates.
set of functions that can be represented as a series expansion of pj, k at
scales or resolutions (i.e., small j ) is contained within those that can be
I "
esented at higher scales.
functions are only function that can be represented at every scale is f (x) = 0.
sinusoids of
varying frequency y function can be represented with arbitrary precision as j -+m.
and infinite
duration.
(b) DWT
expansion -w-.-A- hese conditions are met, there is a companion wavelet + j , that, together
teger translates and binary scalings, spans-that is, can represent-the
ce between any two sets of q,-representable functions at adjacent scales.
?1
functions are
"small waves" of ty 3: Orthogonality. The expansion functions [i.e.,{~,k(x))] form an
finite duration
and varying + 4 m a 1 or biorthogonal basis for the set of 1-D measurable, square-
frequency. nctions. To be called a basis, there must be a unique set of expan-
ents for every representable function. As was noted in the
remarks on Fourier kernels, g,,,v,,,. = h,,, ,,,., for real, orthonor-
completely describes them all. Instead, we characterize each DWT by a
form kernel pair o r set of parameters that defines the pair. The v 1 r = s
transforms are related by the fact that their expansion functions are
waves" (hence the name wavelets) of varying frequency and limited dur
(h,, g ~ =
)
{
4, = 0 otherwise
[see Fig. 7.1(b)]. In the remainder of the chapter, we introduce a numb called the dual of h. For a biorthogonal wavelet transform with scaling
these "small wave" kernels. Each possesses the following general prope avelet functions qj,k ( ~and , duals are denoted Fj,k(x) and
) i,hj, k ( ~ )the
Property 1: Separability, Scalability, and Translatability. The kernels ca
represented as three separable 2-D wavelets
The Fast Wavelet Transform
t/JH(.? Y) = t/J(x)cp(y)
ortant consequence of the above properties is that both ~ ( xand
) t/J(x)
t/JV(.> Y ) = cp(x)t/J(y)
pressed as linear combinations of double-resolution copies of them-
$D(x%Y) = t/J(x)t/J(~) at is, via the series expansions
where +H(x, y), t/JV(x,y), and ~ ~ (y)x are , called horizontal, vertical,
diagonal wavelets, respectively, and one separable 2-D scalingfunction
4 ~ = )C h , ( n ) 6 ( 2 x - n )
n
Y) = q(x)cp(y)
*(x) = Ch*(n)ficp(2x - n )
Each of these 2-D functions is the product of two 1-D real, square-integra n
scaling and wavelet functions
h, and h$-the expansion coefficients-are called scaling and wavelet
pj, k(x) = 2 ~ ~ ~ ~-( k2) j x , respectively. They are the filter coefficients of the fast wavelet trans-
+,,k(x) = 2j1'+(2jx - k) (FWT), an iterative computational approach to the DWT shown in
2. The W,(j, m, n) and { ~ i ( jm, , n) for i = H, V, D) outputs in this
Translation k determines the position of these 1-D functions along the X-a are the DWT coefficients at scale j. Blocks containing time-reversed
scale j determines their width-how broad or narrow they are along x-
g and wavelet vectors-the h,(-n) and h$(-m)-are lowpass and
2jI2 controls their height or amplitude. Note that the associated expan
ass decompositionfilters, respectively. Finally, blocks containing a 2 and a
functions are binary scalings and integer translates of mother wav
arrow represent downsampling-extracting every other point from a se-
t/J(x) = t/Jo, o(x) and scaling function q ( x ) = po,,,(x). e of points. Mathematically, the series of filtering and downsampling
ions used to compute ~ f ( jm, , n) in Fig. 7.2 is, for example,
Property 2: Multiresolution Compatibility. The 1-D scaling function just int
duced satisfies the following requirements of multiresolution analysis: (1, m, n) = hg(-m) * [h,(--n) * W,(j + 1, m, n)ln=2k,kzO]lrn=2k,k20
246 Chapter 7 % Wavelets 7.2 B The Fast Wavelet Transform 247
FIGURE 7.2 The TABLE 7.1
2-D fast wavelet
transform (FWT) wavelet Toolbox
filter bank. Each FWT filters and
pass generates one d b 2 ' , 'db3', ..., 'db45' filter family
DWT scale. In the c o i f 1 ' , ' c o i f 2 ' , ..., ' c o i f 5 ' names.
first iteration, Wv(j+ 1.1n+n)o-- sym2', 'sym3'. ..., 'sym45'
W,(j + 1, m. tz) = Discrete Meyer ' dmey '
f ( ~Y ),. ' b i o r l . l ' , 'biorl . 3 ' , 'bior1.5', 'bior2.2',
' b i o r 2 . 4 ' , ' b i o r 2 . 6 ' , 'bior2.B1,'bior3.1 ' ,
'bior3.3', 'bior3.5', 'bior3.7', 'bior3.9',
'bior4.4','bior5.5','bior6.8'
' r b i o l . I 1 , ' r b i o l .3',' r b i o 1 . 5 ' , ' r b i o 2 . 2 ' ,
'rbio2.4','rbio2.6','rbio2.8','rbio3.1',
'rbio3.3','rbio3.5','rbio3.7','rbio3.9',
indices is equivalent to filtering and downsampling by 2.
Each pass through the filter bank in Fig. 7.2 decomposes the input into f
h t y p e set to ' d ' , ' r ' , ' 1' , or ' h ' to obtain a pair of decomposition, re-
waveinfo(wfami1y) 2 .:c j
to do this without the Wavelet Toolbox (i.e., with IPT alone).The material here @k? waveinfo
lays the groundwork for their development.
The Wavelet Toolbox provides decomposition filters for a wide variety MATLAB prompt.To obtain a digital approximation of an orthonormal
fast wavelet transforms.The filters associated with a specific transform are a orm's scaling andlor wavelet functions, type
cessed via the function wf i l t e r s , which has the following general syntax:
[ p h i , p s i , x v a l ] = wavefun(wname, i t e r )
[Lo-0, Hi-D, Lo-R, Hi-R] = wfilters(wname)
hich returns approximation vectors, p h i and p s i , and evaluation vector
T l ~ r on the icon Here, input parameter wname determines the returned filter coefficients in ac- a l . Positive integer i t e r determines the accuracy of the approximations by
is r~sedto rlenote a cordance with Table 7.1: outputs Lo-D, Hi-D, Lo-R, and Hi-R are row vecto trolling the number of iterations used in their computation. For biorthogo-
MATLAB Wavelrr transforms, the appropriate syntax is
Toolbox ,fiinclion, 11)
that return the lowpass decomposition, highpass decomposition, lowpass
o111>osetlto rr construction, and highpass reconstruction filters, respectively. (Reconstruct
MATLAB o r Itnnge filters are discussed in Section 7.4.) Frequently coupled filter pairs can alte [ p h i l , p s i l , p h i 2 , p s i 2 , x v a l ] = wavefun(wname, i t e r )
Processing fi)olbox
wavef u n
fi~nction.
nately be retrieved using
ere p h i l and p s i l are decomposition functions and phi2 and p s i 2 are
[ F l , F2] = w f i l t e r s ( w n a m e , t y p e ) onstruction functions.
Wavelets 7.2 The Fast Wavelc Transform 249
EXAMPLE 7.1: B The oldest and simplest wavelet transform is based on the Haar scali Haar wavelet function FIGURE 7.3 The
Haar filters,
scaling, and wavelet functions. The decomposition and reconstruction filters for a 1.5 - I Haar scaling and
wavelet functions.
wavelet functions. based transform are of length 2 and can be obtained as follows:
1 -
21 [Lo-D, Hi-D, Lo-R, Hi-R] = wfilters('haar')
LO-D = 0.5- -
0.7071 0.7071
Hi-D =
-0.7071 0.7071
,,------------ -------- ----
LO-R = -0.5 - -
0.7071 0.7071
Hi-R = -1 -
0.7071 -0.7071
I
-1.5
Their key properties (as reported by the waveinf o function) and plots of o 0.5 1
associated scaling and wavelet functions can be obtained using
>> waveinfo('haarl); bplot(122); plot(xva1, psi, ' k ' ,xval, xaxis, ' - - k t ) ;
HAARINFO Information on Haar wavelet. is([O 1 -1.5 1.51); axis square;
tle('Haar Wavelet Function');
Haar Wavelet
General characteristics: Compactly supported 7.3 shows the display generated by the final six commands. Functions
wavelet, the oldest and the simplest wavelet. , axis,and plot were described in Chapters 2 and 3; function subplot
to subdivide the figure window into an array of axes or subplots. It has
scaling function phi = 1 on [0 1 1 and 0 otherwise.
wavelet function psi = 1 on [0 0.51, = -1 on [0.5 1 1 an lowing generic syntax:
otherwise.
H = subplot(m, n, p) or H = subplot(mnp)
Family Haar
Short name haar
Examples haar is the same as dbl re m and n are the number of rows and columns in the subplot array, re-
Orthogonal Yes ively. Both m and n must be greater than 1. Optional output variable H is
Biorthogonal Yes andle of the subplot (i.e., axes) selected by p,with incremental values of p
Compact support Yes ginning at 1) selecting axes along the top row of the figure window, then the
DWT possible nd row, and so on. With or without H,the pth axes is made the current plot.
CWT possible s the subplot (122)function in the commands given previously selects
Support width 1 ot in row 1 and column 2 of a 1 X 2 subplot array as the current plot; the
Filters length 2 quent axis and title functions then apply only to it.
Regularity haar is not continuous e Haar scaling and wavelet functions shown in Figure 7.3 are discontinu-
Symmetry Yes and compactly supported, which means they are 0 outside a finite interval
Number of vanishing the support. Note that the support is 1.In addition, the waveinf o data
moments for psi 1 s that the Haar expansion functions are orthogonal, so that the forward
Reference: I. Daubechies, inverse transformation kernels are identical. 1
Ten lectures on wavelets,
CBMS, SIAM, 61, 1994, 194-202. en a set of decomposition filters, whether user provided or generated by
>> [phi, psi, xval] = wavefun('haarl, 10); ilters function, the simplest way of computing the associated wavelet
>> xaxis = zeros(size(xva1)); orm is through the Wavelet Toolbox's wavedec2 function. It is invoked
>> subplot(l21); plot(xva1, phi, ' k ' , xval, xaxis, '--k');
>> axis([O 1 -1.5 1.51); axis square;
>> title('Haar Scaling Function'); [C, S] = wavedec2(X, N, Lo-D, Hi-D)
, ,
250 Chapter 7 a Wavelets 7.2 r The Fast Wavelet Transform 253
where X is a 2-D image or matrix, N is the number of scales to be com on. It we were to extract the horizontal detail coefficient matrix from
(i.e., the number of passes through the FWT filter bank in Fig. 7.2). and cl ,for example, we would get
and Hi-D are decomposition filters. The slightly more efficient syntax
in which wname assumes a value from Table 7.1, can also be used. Output
structure [ C , S ] is composed of row vector C (class double), which con
the computed wavelet transform coefficients, and bookkeeping matrix S
class double), which defines the arrangement of the coefficients in C.The
tionship between C and S is introduced in the next example and describ ed detail or approximation matrix; the second element is the number
detail in Section 7.3.
EXAMPLE 7.2: '@ Consider the following single-scale wavelet transform with respe
A simple FWT wavelets:
using Haar filters.
c2, s 2 ] = wavedec2(f, 2 , ' h a a r ' )
>> f = magic(4)
f = Columns 1 through 9
16 2 3 13
5 11 10 8
9 7 6 12 Columns 10 through 16
4 14 15 1 4.0000 10.0000 6.0000
>> [ c l , s l ] = wavedec2(f, 1 , ' h a a r ' ) -6.0000 -10.0000
cl =
Columns 1 through 9
17.0000 17.0000 17.0000 17.0000
-1 .OOOO -1.0000 1 .OOOO 4.0000
Columns 10 through 16
-4.0000 -4.0000 4.0000 10.0000
-6.0000 -1 0.0000
sl =
2 2
2 2
4 4
ding single-scale transform and substituted for the approximation coeffi-
Here, a 4 X 4 magic square f is transformed into a 1 X 16 wa from which they were derived. Bookkeeping matrix s 2 is then updated
sition vector c l and 3 X 2 bookkeeping matrix s 1. The entire ect the fact that the single 2 X 2 approximation matrix in c l has been
is performed with a single execution (with f used as the input) of ed by four 1 x 1 detail and approximation matrices in c2. Thus,
tions depicted in Fig. 7.2. Four 2 x 2 outputs-a downsampled appro
and three directional (horizontal, vertical, and diagonal) detail matrices
generated. Function wavedec2 concatenates these 2 X 2 matrices column rices at scale 0, and s 2 ( 1 , : ) is the size of the final
in row vector c l beginning with the approximation coefficients B
:'XI
case 'sym4'
Id = [-7.576571478927333e-002 -2.963552764599851e-002 ...
4.976186676320155e-001 8.037387518059161e-001 ...
2.978577956052774e-001 -9.921954357684722e-002 ... varargout = { l r , h r l ;
-1.260396726203783e-002 3.222310060404270e-002];
t = (0:7);
hd=ld; hd(end:-1:l) = c o s ( p i * t ) . * I d ;
l r = ld; lr(end:-1:l) = l d ; -- *..wMs3
hr = c o s ( p i * t ) . * I d ;
normal filter in wavef l l t e r (i.e., ' h a a r ' , ' d b 4 ' , a n d
case ' b i o r 6 . 8 '
rsed versions of t h e decomposi-
Id = [O 1.908831736481291e-003 -1.914286129088767e-003 ...
-1.699063986760234e-002 1.193456527972926e-002 ... s decompositlon filter is a modulated verslon of its
4.973290349094079e-002 -7.726317316720414e-002 . . . he lowpass decomposition filter coefficients need t o
-9.405920349573646e-002 4.207962846098268e-001 ... ining filter coefftcients can b e
8.259229974584023e-001 4.207962846098268e-001 ... vef l l t e r , time reversal is carried out by reordering
-9.405920349573646e-002 -7.726317316720414e-002 ...
4.973290349094079e-002 1.193456527972926e-002 . . .
-1.699063986760234e-002 -1.914286129088767e-003 ... s between 1and -1 as t increases from 0 in integer
1 ,908831 736481291 e-0031; nal filter in wavef i l t e r (i.e., ' b 1 o r 6 . 8 ' a n d
hd = [O 0 0 1.442628250562444e-002 -1.446750489679015e-002 ositlon filters a r e specified;
-7.872200106262882e-002 4.036797903033992e-002 ... ns of them. F~nally,we note
4.178491091502746e-001 -7.589077294536542e-001 ...
4.178491091502746e-001 4.036797903033992e-002 ...
-7.872200106262882e-002 -1.446750489679015e-002 ...
1.442628250562444e-002 0 0 0 01;
t = (0:17); t e r generated decompositlon filters, it is easy t o
l r = c o s ( p i * ( t + 1 ) ) . * hd; utine for the computation of t h e related fast
hr = c o s ( p i * t ) . * I d ; s t o devise a n efficient algorrthm based o n t h e fil-
aintaln compatibility with
case ' j p e g 9 . 7 ' decomposition structure
Id = [0 0.02674875741080976 -0.01686411844287495 ... m a bookkeeping matrix).
-0.07822326652898765 0.2668641184428723 ...
0.6029490182363579 0.2668641184428723 ... we call wavef a s t , uses symmetric image exten-
-0.07822326652898785 -0.01686411844287495 ... n t o reduce t h e border d i s t o r t ~ o nassociated with t h e computed FWT.
0.026748757410809761; wavef a s t
hd = [O -0.09127176311424948 0.05754352622849957 ... \rams - --
0.5912717631142470 -1.115087052456994 ...
0.5912717631142470 0.05754352622849957 ... respect t o decomposltlon f l l t e r s LP and
-0.09127176311424948 0 01;
HP.
t = (0:9);
l r = c o s ( p i * ( t + 1 ) ) .* hd;
[ C , L] = WAVEFAST(X, N , WNAME) performs t h e same operation but
hr = c o s ( p i * t ) . * Id;
f e t c h e s f i l t e r s LP and HP f o r wavelet WNAME using WAVEFILTER.
otherwise
e r r o r ( 'Unrecognizable wavelet name (WNAME) . ' ) ; Scale parameter N must be l e s s than o r equal t o log2 of t h e
end
256 Chapter 7 a Wavelets 7.2 a The Fast Iave'let Transform
M a t r i x S i s an ( n t 2 ) x 2 bookkeeping m a t r i x :
volve rows w i t h LP and downsample. Then convolve columns
h HP and LP t o get t h e h o r i z o n t a l and next approximation
As can be seen in the main routine, only one f o r loop, which cycles thr FIGURE 7.4
the decomposition levels (or scales) that are generated, is used to orch A 512 X 512
image of a vase.
the entire forward transform computation. For each execution of the 10
current approximation image, app, which is initially set to x, is symme
extended by internal function symextend.This function calls padarray,
was introduced in Section 3.4.2, to extend app in two dimensions by mirro
flecting f 1 - 1 of its elements (the length of the decomposition filter minu;
across its border.
Function symextend returns an extended matrix of approximation toe_
cients and the number of pixels that should be extracted from the cente?'
any subsequently convolved and downsampled results. The rows of the & 4
tended approximation are next convolved with highpass decomposition fii{
h p and downsampled via symconv. This function is described in the follo '
paragraph. ~ o n v o l v e doutput, rows, is then submitted to symconv to convc
and downsample its columns with filters hp and lp-generating the dia
and vertical detail coefficients of the top two branches of Fig. 7.2.These r
are inserted into decomposition vector c (working from the last element Get transform and computation time f o r wavefast.
ward the first) and the process is repeated in accordance with Fig. 7.2 t o g .6 ;
erate the horizontal detail and approximation coefficients (the bottom t d :2, s2] = w a v e f a s t ( f , n, wname) ;
branches of the figure). ?"= t o c ;
I
Function symconv uses the conv2 function to d o the bulk of the transfc 'Compare t h e r e s u l t s .
computation work. It convolves filter h with the rows o r columns of x (dt tio = t 2 / (reftime + eps);
pending on type), discards the even indexed rows or columns (i.e., do xdiff = abs (max(c1 - c2) ) ;
"i
ples by 2), and extracts the center keep elements of each row or
Invoking conv2 with matrix x and row filter vector h initiates a row-b ;1 the 512 X 512 image of Fig. 7.4 and a five-scale wavelet transform with
convolution; using column filter vector h ' results in a columnwise convolutio~ jpect to 4th order Daubechies' wavelets, fwtcompare yields
EXAMPLIE 7.3: I The following test routine uses functions t i c and t o c to compare th f = imread('Vasei, ' t i f ' ) ;
Comparing the cution times of the Wavelet Toolbox function wavedec2 and custom func [ r a t i o , maxdlfference] = f w t c o m p a r e ( f , 5 , ' d b 4 ' )
execution times of 3
wavefast and
wavef a s t : 4*
wavedec2.
function [ r a t i o , maxdiff ] = fwtcompare(f , n, wname) ice
%FWTCOMPARE Compare wavedec2 and wavefast.
% [RATIO, MAXDIFF] = FWTCOMPARE(F, N , WNAME) compares the operat
% of toolbox function WAVEDEC2 and custom function WAVEFAST.
0
, pote that custom function wavef a s t was almost twice as fast as its Wavelet
% INPUTS: F l b o x counterpart while producing virtually identical results.
% F Image t o be transformed.
% N Number of scales t o compute. 1 Working- with Wavelet Decomposition Structures
%
%
WNAME Wavelet t o use.
E$ewavelet transformation functions of the previous two sectlons produce
% OUTPUTS: n~ndlspla~nble data structures of the form {c, S ) , where c 1s a transform coef-
% RATIO Execution tune r a t l o (custom/toolbox) cient vector and S is a bookkeeping matrix that defines the arrangement of
% MAXDIFF Maxlmum coefflclent difference. fficients In c. To process images, we must be able to examine and/or modify
% Get transform and c o m ~ u t a t i o ntlme f o r wavedec2. this sect~on,we formally define { c , S}, examine some of the Wavelet Tool-
LA",
funct~onsfor manipulating rt, and develop a set of custom functions that
[ c l , s l ] = wavedec2(f, n , wname); be used without the Wavelet Toolbox. These functions are then used to
reftlme = t o c ; Julld a general purpose rout~nefor displaying c.
260 Chapter 7 a Wavelets 7.3 Working with Wavelet Decomposition Structures 261
then
0 -0.2842
cl = w t h c o e f 2 ( ' h 1 , c l , s l , 2 ) ;
horizdet2 = d e t c o e f 2 ( ' h 1 , newcl, s l , 2 )
e e s l ( 1 , : ) a n d s l ( 2 , : ) ] , ( b ) three2 X 2detail
dimensions of Nth-level approximation A N ,ith-level details (Hi, V,,
for i = 1,2,. . . N), and original image F, respectively.The information i
)I,
s1 ( 3 , : and (c) three 4 X 4 detail matrices for level
horizdet2 is returned.The coefficients corresponding to horizdet2 in is a decomposition level (Ignored if TYPE = 'a').
then zeroed using wthcoef 2,a wavelet thresholding function of the for is a two-dimensional coefficient matrix for pasting.
nc = wthc0ef2(typej c , S, n, t , sorh) also WAVECUT, WAVECOPY, and WAVEPASTE.
where type is set to ' a ' to threshold approximation coefficients and '
or ' d ' to threshold horizontal, vertical, or dia S(~) -= 2) ( (size(c, 1) -= 1)
n is a vector of decomposition leve r( 'C must be a row vector. ' ) ;
sponding thresholds in vector t,whil
thresholding, respectively. If t is omitted, all coefficients meeting the S) [ is numeric(^) 1 (size(s, 2 ) -= 2)
n specifications are zeroed. Output nc is the modified (i.e., thresh0 umeric ~ W O - C O array.');
~ U ~ ~
other syntaxes that can be examined using the MATLAB help com % Coefficient matrix elements.
7,3.1 Editing Wavelet Decomposition Coefficients ts(2:end - 1 ) ) >= eleme*ts(end)
without the Wavelet Toolbox ,.(['[C SI must form a standard wavelet decomposition '
Without the Wavelet Toolbox, bookkeeping matrix S is the key to ac 'structure.' I) ;
the individual approximation and detail coefficients of multiscale vect
this section, we use S to build a set of mp(lower(opcode(l:3)), 'pas')& napgin '6
ulation of c.Function wavework is t r('Not enough input arguments.');
which are based on the familiar cut-copy-paste metaphor of modern w
cessing applications.
% Default level is 1 .
wavework function [varargoutl = wavework(opcode, type, c, s, n, x )
eavz - - ---- ""
%WAVEWORK is used to edit wavelet decomposition structures. % Maximum levels in [C, Sl.
% [VARARGOUT] = WAVEWORK(OPCODE, TYPE, C, S, N, X) gets the
% coefficients specified by TYPE and N for access or modif icat
% based on OPCODE.
%
% INPUTS:
% OPCODE Operation to perform % Make pointers into C.
/o ...........................
% ' copy' [varargoutl = Y = requested (via TYPE and N)
% coefficient matrix
% 'cut' lvarargoutl = [NC, Y] = New decomposition vector
% (with requested Coefficient matrix zeroed) AND
a,
requested coefficient matrix
% 'paste' [varargoutl = [NC] = new decomposition vector wit
% coefficient matrix replaced by x
%
% TYPE Coefficient category % Index to detail info.
0/ .............................................................
% 'a' Approximation coefficients
% 'h' Horizontal details
% Iv' Vertical details
% 'd' Diagonal details
%
% [ C , Sl is a wavelet toolbox decomposition structure.
264 Chapter 7 a Wavelets 7.3 @ Working with Wavelet Decornposition Structures
c, y ] = wavework('cutl, t y p e , c, s ) ;
on y = wavecopy(type, c, s, n)
-- wavecopy
s w i t c h statement then begins the computation of a pair of point -"
efficients associated with input parameters t y p e and n. For the approxi WAVECOPY(TYPE, C, S, N) r e t u r n s a c o e f f i c i e n t a r r a y based on
case (i.e., c a s e ' a ' ), the computation is trivial since the coefficients are
at the start of c (so pointer s t a r t is 1);the ending index, pointer s t o p
C o e f f i c i e n t category
...............................................................
Approximation c o e f f i c i e n t s
Horizontal details
zontal, vertical, or diagonal coefficients, respectively, and n i n d e x is a
Vertical details
to the row of s that corresponds to input parameter n. Diagonal d e t a i l s
The second s w i t c h statement in function wavework performs the
tion requested by opcode. For the ' c u t ' and ' c o p y ' cases, the coe S] i s a wavelet d a t a s t r u c t u r e .
c between s t a r t and s t o p are copied into y, which has be p e c i f i e s a decomposition l e v e l ( i g n o r e d i f TYPE = ' a ' ) .
two-dimensional matrix whose size is determin
f u n c t i o n [ n c , y ] = w a v e c u t ( t y p e , c, s , n ) n c = w a v e p a s t e ( t y p e , c , s, n, x )
-wavecut- %WAVECUT Zeroes c o e f f i c i e n t s i n a wavelet decomposition s t r u c t u r
wavepaste
mm---------.
EXAMPLE 7.5: a Functions wavecopy and wavecut can be used to reproduce the
Manipulating c Toolbox based results of Example 7.4:
with wavecut and
wavecopy. >>f = magic(8); Detail coefficient scaling
>>[ c l , s l ] = w a v e d e c 2 ( f , 3, ' h a a r ' ) ; ..............................................................
>>a p p r o ~= w a v e c o p y ( ' a ' , c l , s l ) Maximum range ( d e f a u l t )
approx = Magnify default by the scale f a c t o r
260.0000 1, -2... Magnify absolute values by abs(sca1e)
>> h o r i z d e t 2 = w a v e c o p y ( ' h l , c l , s l , 2 )
horizdet2 = Border between wavelet decompositions
- - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1.0e-013 *
Border replaces image ( d e f a u l t )
0 -0.2842 Border increases width of image
0 0
>> [ n e w c l , h o r i z d e t 2 1 = w a v e c u t ( ' h l , c l , s l , 2 ) ;
>> newhorizdet2 = w a v e c o p y ( ' h ' , newcl, s l , 2 )
newhorizdet2 =
0 0
0 b
velets employed in the forward transform. Recall that they can be ob-
from the wf i l t e r s and wavef i l t e r functions of Section 7.2 with input
y = z e r o s ( [ 2 11 . * s i z e ( x ) ) ; y(l:2:end, : ) = x;
y = conv2(y, f l ' ) ;
z = z e r o s ( [ l 21 .* s i z e ( y ) ) ; z ( : , 1:2:end) = y;
z = conv2(z, f 2 ) ; verec2(cl, sl , wname) ;
z = z(f1n - 1 : f l n + keep(1) - 2, f l n - 1 : f l n t keep(2) - 2)
>> f = imread('A.tifl);
>> imshow(f);
>> [ c , S ] = w a v e f a s t ( f , 1, ' s y m 4 ' ) ;
>> f i g u r e ; wave2gray(c, s , -6);
>> [nc, y] = w a v e c u t ( ' a l , c , s ) ;
>> f i g u r e ; wave2gray(nc, s , - 6 ) ;
>> edges = abs(waveback(nc, s , ' s y m 4 ' ) ) ;
>> f i g u r e ; imshow(mat2gray(edges));
mlets is shown in Fig. 7.8(b), where it is clear that a four-scale decom-
The horizontal, vertical, and diagonal directionality of the sing1 has been performed. To streamline the smoothing process, we employ
wavelet transform of Fig. 7.7(a) with respect to ' sym4 ' wavelets is clea wing utiiity function:
ible in Fig. 7.7(b). Note, for example, that the horizontal edges of the o
image are present in the horizontal detail coefficients of the upper-r n [nc, g8] = wavezero(c, s, 1, wnarne) wavezero
rant of Fig. 7.7(b).The vertical edges of the image can be similarly id RO Zeroes wavelet transform d e t a i l c o e f f i c i e n t s . .!mmr------
the vertical detail coefficients of the lower-left quadrant. To combi , G8] = WAVEZERO(C, S, L, WNAME) zeroes the l e v e l L d e t a i l
formation into a single edge image, we simply zero the approximatl e f f i c i e n t s i n wavelet decomposition s t r u c t u r e [ C , S] and
cients of the generated transform, compute its inverse, and take th omputes the resulting inverse transform w i t h respect t o WNAME
value. The modified transform and resulting edge image are sho
Figs. 7.7(c) and (d), respectively. A similar procedure can be used to is01 001 = Wavecut('hl, C , S, 1 ) ;
vertical or horizontal edges alone. 001 = wavecut('vl, nc, s , 1 ) ;
001 = w a v e c u t ( ' d ' ,
nc, s , 1 ) ;
EXAMPLE 7.9: %Bs Wavelets, like their Fourier counterparts, are effective instrume aveback(nc, s , wname);
Wavelet-based smoothing or blurring images. Consider again the test image of Fig. irn2uint8(rnat2gray( i )) ;
image smoothing which is repeated in Fig. 7.8(a). Its wavelet transform with respect to f
or blurring.
Processing 279
278 Chapter 7 %
a b
c d
e f
FIGURE 7.81
Wavelet-based
image smoothing:
(a) A test Image;
(b) ~ t wavelet
s
transform; (c) the
inverse transform
after zerolng the
tlrst-level detall
coefflclents; and
(d) through
(f) s ~ m ~ lresults
ar
after zerolng the
second-, thlrd-,
and fourth-level
details
EXAMPLE 7.10:
Progressive
reconstruction.
Chapter IWavelets
% Approximation 1
= wavecopy('al, c, s ) ;
gure; imshow(mat2gray ( f ) ) ;
, S ] = waveback(c, s, ' j p e g 9 . 7 ' , 1); % F i n a l image
= wavecopy('al, c, s ) ;
gure; imshow(mat2gray(f));
that the final four approximations use waveback t o perform single level
i$
Toolbox. In the next chapter, wavelets will be used for image compression,an
a. 'ch they have received considerable attention in the literature.
brd: d e. f
n1
CR = -
n2
10 (or 10: 1) indicates that the original image has
Preview of two image files andlor variables can be computed with the follow-
FIGURE 8.1
A general image
Quantizer + ! compression
because variable-length coding is a mainstay of imag I
coder 1 system block
MATLAB is best at processing matrices of uniform (i.e., fixe diagram.
During the development of the function, we assume that the reader has Encoder image
working knowledge of the C language and focus our discussion on how
r------------------
I I
r =
35.1612
[-
ems = MN .=o Y=O 2
C, ricx, Y) - f ( ~y)12]
.
lowing M-function computes e,, and displays (if ems + 0) both e(x, Y)
Note that in function i m r a t i o , internal function b = b y t e s ( f ) is desig histogram. Since e(x, y) can contain both positive and negative values,
to return the number of bytes in (1) a file, (2) a structure variable, andlor (3 rather than i m h i s t (which handles only image data) is used to generate
nonstructure variable. If f is a nonstructure variable, function whos
,,.<+!&zh
. , $'.,
'15 ,. '4.
duced in Section 2.2, is used to get its size in bytes. Iff is a file name
d i r performs a similar service. In the syntax employed, d i r returns a
(see Section 2.10.6 for more on structures) with fields name, d a t e , bytes,
i s d i r . They contain the file's name, modification date, size in bytes a
i o n rmse = compare(f1, f 2 , scale)
ARE Computes and displays the e r r o r between two matrices.
MSE = COMPARE(F1, F2, SCALE) returns t h e root-mean-square e r r o r
- compare
---
whether or not it is a directory ( i s d i r is 1 if it is and is 0 otherwise), re etween i n p u t s F1 and F2, displays a histogram o f the difference,
tively. Finally, if f is a structure, b y t e s calls itself recursively to sum the nd displays a scaled difference image. When SCALE i s omitted, a
cale f a c t o r of 1 i s used.
ber of bytes allocated to each field of the structure. This eliminates
overhead associated with the structure variable itself (124 bytes per field), ck i n p u t arguments and set defaults.
,,;&-, turning only the number of bytes needed for the data in the fields. Functi (nargchk(2, 3, n a r g i n ) ) ;
...-:.
...::
A?,;:,?tii&,1dnames
.-.> f ieldnarnes is used to retrieve a list of the fields in f, and the statements
,~<* \
names = for k = l:length(fields)
fieldnames(s) re- b = b + bytes(f.(fields{k})); Compute the root-mean-square e r r o r .
turns a cell array o f double(f 1) - double(f 2 ) ;
strings containing
the structure field perform the recursions. Note the use of dynamic structure fieldnames in the
namesassociated se = sqrt(sum(e(:) .^ 2) 1 (m * n));
with structure s. cursive calls to b y t e s . If S is a structure and F is a string variable containin
field name, the statements Output e r r o r image & histogram i f an e r r o r ( i . e . , rmse -= 0 ) .
% Form e r r o r histogram.
S.(F) = f o o ;
f i e l d = S.(F); emax = max(abs(e(:)));
[h, x ] = h i s t ( e ( : ) , emax);
Chapter Image Compression 8.2 r Coding
i f length(h) >= 1
figure; bar(x, h , ' k ' ) ; TABLE 8.1
Illustration of
% Scale the error image symmetrically and display coding redunda
emax = emax I scale; La,, = 2 for
e = matZgray(e, [-emax, emax]); Code 1;Lavg=
figure; imshow(e); for Code 2.
end
end
Finally, we note that the encoder of Fig. 8.1 is responsible for redu
coding, interpixel, and/or psychovisual redundancies of the input image
first stage of the encoding process, the mapper transforms the input ima
a (usually nonvisual) format designed to reduce interpixel redundanci
second stage, or quantizer block, reduces the accuracy of the mapp
in accordance with a predefined fidelity criterion-attempting to e
only psychovisually redundant data.This operation is irreversible a
omitted when error-free compression is desired. In the third and fi
the process, a symbol coder creates a code (that reduces coding red
for the quantizer output and maps the output in accordance with the co
The decoder in Fig. 8.1 contains only two components: a symbo
and an inverse mapper. These blocks perform, in reverse order, the inve + l(0.5) + 3(0.125) + 2(0.1875) = 1.8125
erations of the encoder's symbol coder and mapper blocks. Because qu
tion is irreversible, an inverse quantization block is not included.
= 3(0.1875)
-
resulting compression ratio is Cr = 211.8125 1.103. The underlying
r the compression achieved by Code 2 is that its code words are of
Coding Redundancy
Let the discrete random variable rk for k = 1 , 2 , . . . , L with associated
bilities pr(rk) represent the gray levels of an L-gray-level image.
Chapter 3, rl corresponds to gray level 0 (since MATLAB array indices c at is sufficient to describe completely an image without loss of informa-
be 0 ) and
nk
pr(rk) = - k = 1 , 2 , . .. , L
n
where nk is the number of times that the kth gray level appears in the ima
and n is the total number of pixels in the image. If the number of bits used 1
represent each value of rk is I(rk),then the average number of bits required I ( E ) = log ---- = -log P ( E )
represent each pixel is P(E)
L of information. If P ( E ) = 1 (that is, the event always occurs), I ( E ) = 0
Lt?~g = l(~k)~r(~k)
k=l
That is, the average length of the code words assigned to the various gray-leve the event has occurred. Given a source of random events from the dis-
values is found by summing the product of the number of bits used to re e set of possible events { a l ,a z , . . . , a ] ) with associated roba abilities
sent each gray level and the probability that the gray level occurs. Thus a ] ) ,P ( a 2 ) ,. . . , P ( a , / ) ) ,the average information per source output, called
total number of bits required to code an M X N image is MNLaVg. entropy of the source, is
When the gray levels of an image are represented using a natural m-bit J
nary code, the right-hand side of the preceding equation reduces t H =- P ( a j )log P ( a j )
j=1
288 Chapter 8 Image Compression 8.2 hP Coding Redundancy 289
If an image is interpreted as a sample of a "gray-level source" that emi 3 119 168 168
we can model that source's symbol probabilities using the gray-lev g 119 107 119
togram of_the observed image and generate an estimate, called the first 7 107 119 119
estimate, H, of the source's entropy:
L
fi = -x
k=l
pr(rk) 1% pr(rk)
1875 0.5 0.125 0 0 0 0 0.1875
Such an estimate is computed by the following M-function and, under th
sumption that each gray level is coded independently, is a lower bou
the compression that can be achieved through the removal of coding r
dancy alone.
entropy function h = entropy(x, n ) Table 8.1, with LavgC= 1.81, approaches this first-order entropy esti-
wi84m----------.
%ENTROPY Computes a f i r s t - o r d e r estimate of the entro is a minimal length binary code for image f . Note that gray level 107
% H = ENTROPY(X, N ) returns the f i r s t - o r d e r estimate o f matrix ds to rl and corresponding binary codeword 0112 in Table 8.1, 119
% w i t h N symbols ( N = 256 i f omitted) i n bitslsymbol. The estim ode I,, and 123 and 168 correspond to 0102 and 002,
% assumes a s t a t i s t i c a l l y independent source characterized by t lsl
% relative frequency of occurrence of the elements i n X .
error(nargchk(1, 2, nargin)); % Check input arguments Huffman Codes
i f nargin < 2
n = 256; % Default for n . an image or the output of a gray-level mapping
end nces, run-lengths, and so on), H u f i a n codes contain
x = double(x); of code symbols (e.g., bits) per source symbol
Make input double
%
xh = h i s t ( x ( : ) , n ) ; Compute N-bin histogram
%
ubject to the constraint that the source symbols are
xh = xh 1 sum(xh(:)); Compute probabilities
%
% Make mask t o eliminate 0 ' s since log2(0) = -inf.
i = find(xh);
ining the lowest probability symbols into a single symbol that replaces
h = -sum(xh(i) .* l o g 2 ( x h ( i ) ) ) ; % Compute entropy in the next source reduction. Figure 8.2(a) illustrates the process for the
-level distribution in Table 8.1.At the far left, the initial set of source sym-
Note the use of the MATLAB f i n d function, which is employed to d s are ordered from top to bottom in terms of de-
the indices of the nonzero elements of histogram xh.The statement f i rm the first source reduction, the bottom two
equivalent to f i n d (x -= 0 ) . Function entropy uses f i n d to create a
of indices, i , into histogram xh, which is subsequently employed t is compound symbol and its associated probability
all zero-valued elements from the entropy computation in th e reduction column so that the probabilities of the
If this were not done, the log2 function would force output h
is not n number) when any symbol probability was 0.
EXAMPLE 8.1: &l Consider a simple 4 X 4 image whose histogram (see p in the an's procedure is to code each reduced source,
Computing first- code) models the symbol probabilities in Table 8.1. The following c
order entropy est source and working back to the original source. The
estimates. line sequence generates one such image and computes a first-order estimate a1 length binary code for a two-symbol source, of course, consists of the
its entropy. ) shows, these symbols are assigned to the two
;reversing the order of the 0
>>f = [ I 1 9 123 168 119; 123 119 168 1681;
>> f = [ f ; 119 119 107 119; 107 107 119 1191 was generated by combining two symbols in the reduced source to its left,
f = 0 used to code it is now assigned to both of these symbols, and a 0 and 1
119 123 168 119 arbitrarily appended to each to distinguish them from each other. This
290 Chapter 8 M Image Compression 8.2 @ Coding Redundancy 291
a1 v a r l a b l e surviving a l l r e c u r s i o n s o f f u n c t l o n 'makecode'
................................................................. %
% S o r t t h e symbol p r o b a b l l l t l e s
2 lowest p r o b a b l l l t l e s
% and prune t h e l o w e s t one
% Reorder t r e e f o r new p r o b a b l l l t l e s
s{2) = { s { l ) , ~ ( 2 ) ) ; % and merge & prune ~ t nodes s
% t o match t h e p r o b a b l l l t l e s
The source reduction and code assignment procedures just described
implemented by the following M-function, which we call h u f f man. - - - . - . . . . - . . . . . . . - - - - - - - - - - - - - - - - - - - - - . -9. - - - - - - - - - - - - - - -
0
n c t l o n makecode(sc, codeword)
huffman f ~ n c t l o nCODE = huffman(p)
---- Scan t h e nodes of a Huffman source reduction t r e e recursively t o
%HUFFMAN B u l l d s a v a r l a b l e - l e n g t h Huffman code f o r a symbol source. generate t h e indicated v a r l a b l e l e n g t h code words.
% CODE = HUFFMAN(P) r e t u r n s a Huffman code as b i n a r y s t r l n g s l n
% c e l l a r r a y CODE f o r l n p u t symbol p r o b a b i l i t y v e c t o r P. Each wo
% l n CODE corresponds t o a symbol whose probability 1s a t t h e
292 Chapter Image Compression 8.2 i~Coding
i f isa(sc, ' c e l l ' ) % For c e l l array nodes, atrix. That is, X{1) refers to the contents of the first element (an
makecode(sc{l), [codeword 01 ) ;
makecode(sc{2), [codeword 11 ) ;
else
CODE(sc1 = c h a r ( ' 0 ' + codeword);
end
CODE is initialized and the input probability vector is normalized
e Huffman code for normalized proba-
The following command line sequence uses huff man t .The first step, which is initiated by the
Fig. 8.2: ce ( p ) statement of the main routine, is to call internal function
whose job is to perform the source reductions illustrated in
>> p = [0.1875 0 . 5 0.125 0.18751; itially empty source reduction
>> c = huffman(p) CODE, are initialized to their indices.
C =
'011 '
'1 '
'010'
'00'
[ y , i] = s o r t ( x )
Note that the output is a variable-length character array in which ea
string of 0s and 1s-the binary code of the correspondingly indexed output y is the sorted elements of x and index vector i is such that
p. For example, ' 01 0 ' (at array index 3) is the code for the gray
).When p has been sorted, the lowest two probabilities are merged by
probability 0.125.
their composite probability in p ( 2 ),and p ( 1 ) is pruned.The source re-
In the opening lines of huff man, input argument p (the i cell array is then reordered to match p based on index vector i using s
ability vector of the symbols to be encoded) is checked for r
. Finally, s ( 2 ) is replaced with a two-element cell array containing the
global variable CODE is initialized as a MATLAB cell
probability indices via s ( 2 ) = { s { l ) , ~ ( 2 ) )(an example of content
Section 2.10.6) with l e n g t h ( p ) rows and a single colum
a1 variables must be declared in the functions that refer g), and cell indexing is employed to prune the first of the two merged
statement of the form
global X Y Z
between the last two statements of the huff man main routine. MATLA TABLE 8.2
Origin sc codeword C o d e assignment
tion c e l l d i s p prints a cell array's contents recursively; function ce
main routine (1x2 cell) [1 process for the
[21 source reduction
2 makecode [ 4 ] (1x2 cell} 0 cell array in
source reduction tree nodes in Fig. 8.3(a): (1) each two-way bra
3 makecode 4 0 0 Fig. 8.3.
(which represents a source reduction) corresponds to a two-element
4 makecode [31 [ I ] 0 1
in s; and (2) each two-element cell array contains the indices of the sy
5 makecode 3 0 1 0
that were merged in the corresponding source reduction. For exa 6 makecode 1 0 1 1
merging of symbols a3 and a1 at the bottom of the tree produces th 7 makecode 2 1
element cell array s t 1 ){2), where s{l){2){1} = 3 and s{1}{2){2) =
indices of symbol a3 and a l , respectively). The root of the tree is the top
two-element cell array s . e that s c is not a cell array, as in rows 3,5,6, and 7 of the table, addi-
The final step of the code generation process (i.e., the assignment of recursions are unnecessary; a code string is created from codeword and
based on source reduction cell array s) is triggered by the final statem ed to the source symbol whose index was passed as sc.
huff man-the makecode ( s , [ ] ) call. This call initiates a recursive co
signment process based on the procedure in Fig. 8.2(b). Although rec Huffman Encoding
generally provides no savings in storage (since a stack of values an code generation is not (in and of itself) compression. To realize the
processed must be maintained somewhere) or increase in speed. it has pression that is built into a Huffman code, the symbols for which the code
vantage that the code is more compact and often easier to understand. created, whether they are gray levels, run lengths, or the output of some
ularly when dealing with recursively defined data structures like t mapping operation, must be transformed or mapped (i.e., en-
MATLAB function can be used recursively; that is, it can call itself ance with the generated code.
rectly or indirectly. When recursion is used, each function call generates a
set of local variables, independent of all previous sets. Consider the simple 16-byte 4 X 4 image: EXAMPLE 8.2:
Internal function makecode acce Variable-length
code mappings in
2 = u i n t 8 ( [ 2 3 4 2 ; 3 2 4 4 ; 2 2 1 2 ; 1 1 2 21) MATLAB.
array, it contains the two source symbols (or composite symbols) that w
joined during the source reduction process. Since they must be individu 2 3 4 2
coded, a pair of recursive calls (to makecode) is issued for the elements-a1 3 2 4 4
with two appropriately updated code words (a 0 and 1 are appende 2 2 1 2
1 1 2 2
using CODE{sc} = c h a r ( ' 0 ' + codeword). As was noted in Section 2. Size Bytes Class
MATLAB function char converts an array containing positive integers
16 uint8 array
nd t o t a l i s 16 elements using 16 bytes
string ' 01 0 ' , since adding a 0 to the ASCII code for a 0 yields an ASCII '
while adding a 1 to an ASCII ' 0 ' yields the ASCII code for a 1, namely ' an 8-bit byte; 16 bytes are used to represent the entire
Table 8.2 details the sequence of makecode calls that results for the sou gray levels of f 2 are not equiprobable, a variable-length
reduction cell array in Fig. 8.3. Seven calls are required to encode the f (as was indicated in the last section) will reduce the amount of memory
symbols of the source.The first call (row 1 of Table 8.2) is made from them ired to represent the image. Function huff man computes one such code:
routine of huff man and launches the encoding process with inputs codewo
and s c set to the empty matrix and cell array s, respectively. In accordan c = huffman(hist(double(f2(:)), 4 ) )
with standard MATLAB notation, {I x2 c e l l ) denotes a cell arr
296 Chapter 8 a! Image Compression 8.2 Coding Redundancy
Since Huffman codes are based on the relative frequency of occurrence rmed into a 3 x 16 character array, h2f 2. Each
source symbols being coded (not the symbols themselves), c is i of h 2 f 2 corresponds to a pixel of f 2 in a top-to-bottom left-to-right
code that was constructed for the image in Example 8.1. In fact, image at blanks are inserted to size the array proper-
be obtained from f in Example 8.1 by mapping gray levels 107, 119,l since two bytes are required for each ' 0 ' or ' 1 ' of a code word, the
168 to 1, 2, 3, and 4, respectively. For either image, p = [ o . 1875 0.5 emow used by h2f 2 is 96 bytes-still six times greater than the original
0.18751. s needed for f 2. We can eliminate the inserted blanks using
A simple way to encode f 2 based on code c is to perform a straightfo
lookup operation:
>> h l f 2 = c ( f 2 ( : ) ) '
hlf2 =
char a r r a y
Columns 1 through 9
'1' '010' '1' 1 1 '010' '1' '1' ' 1 '0
Columns 10 through 16 required memory is still greater than f 2's original 16 bytes.
'00' 1 '1' '1' '00' '1' '1' e c must be applied at the bit level, with several encod-
>> w h o s ( ' h l f 2 ' ) 1s packed into a single byte:
Name Size Bytes Class
hlf2 1x16 1530 c e l l array
Grand t o t a l i s 45 elements using 1530 bytes
>> d e c 2 b i n ( d o u b l e ( h c o d e ) ) ore t h e s i z e of i n p u t X.
ans = ze = u i n t 3 2 ( s i z e ( x ) ) ;
1010101101011011 nd the range o f x values and s t o r e i t s minimum value biased
Colrvercs rt deci~~rnl 0000011110011000 +32768 as a UINT16.
b~rcgerto ( I boinrj>
slri~rgFor 11to1.e111- round(double(x) ;
t!pr
fofor-iirario~l, Note that d e c 2 b i n has been employed to display the individual
>> h e l p dec2bin. h 3 f 2 . code. Neglecting the terminating modulo-16 pad bits (i.e., the fina
Os), the 32-bit encoding is equivalent to the previously generated = double(intl6(xmin));
Section 8.2.1) 29-bit instantaneous uniquely decodable block c = u i n t l 6 ( p m i n + 32768) ; y .min = pmin;
10~0~01~010110110000011110011. mpute t h e i n p u t histogram between xmin and xmax w i t h u n i t
dth bins, scale t o UINT16, and s t o r e .
As was noted in the preceding example, function m a t 2 h u f f embeds t
formation needed to decode an encoded input array (e.g., its original histC(X, xmin:xmax) ;
sions and symbol probabilities) in a single MATLAB structure varia
information in this structure is documented in the help text sect1 = 65535 * h / max(h); This ,f~lnc[ion is sinti-
ltrr to h i s t . For
m a t 2 h u f f itself: uintl6(h); y . h i s t = h; no re rletails, type
>> h e l p h i s t c .
f u n c t i o n y = mat2huff ( x ) de t h e i n p u t m a t r i x and s t o r e t h e r e s u l t .
%MAT2HUFF Huffman encodes a m a t r i x . = huffman(double(h)); % Make Huffman code map
% Y = MAT2HUFF(X) Huffman encodes m a t r i x X u s i n g symbol = map(x(:) - xmin + 1 ) ; % Map image
% p r o b a b i l i t i e s i n u n i t - w i d t h histogram b i n s between X ' s minimum % Convert t o char a r r a y
% and maximum values. The encoded data i s returned as a s t r u c t u r e
% Y: % Remove blanks
, Y.code The Huffman-encoded values o f X, stored i n = c e i l ( l e n g t h ( h x ) 1 16); % Compute encoded s i z e
% a u i n t l 6 vector. The o t h e r f i e l d s o f Y c o n t a i n = r e p m a t ( ' O 8 , 1, ysize * 16); % P r e - a l l o c a t e modulo-16 vector
% a d d i t i o n a l decoding i n f o r m a t i o n , i n c l u d i n g : l : l e n g t h ( h x ) ) = hx; % Make hx modulo-16 i n l e n g t h
, Y.min The minimum value o f X p l u s 32768 = reshape(hxl6, 16, y s i z e ) ; % Reshape t o 16-character words
%
0
%
%
%
D
Y.size
Y.hist
The s i z e o f X
The histogram o f X
I f X i s l o g i c a l , u i n t 8 , u i n t l 6 , u i n t 3 2 , i n t 8 , i n t l 6 , o r double,
= p0~2(15:-1:O);
e = uintl6(sum(hxl6 .*
% Convert binary s t r i n g t o decimal
- n = link(n)
i= 15, j = 0, k = 0, n = 0;
i l e (xsz - k ) {
i f ( * ( l i n k + n) > 0)
if((*(hx+j)>>i)&OxOOOl)
n = *(link + n);
{
e l s e n = * ( l i n k + n) - 1;
i f ( i ) i - - ; e l s e { j + + ; i= 15;)
I*S t a r t a t r o o t node, 1 s t * /
I * hx b i t and x element * /
I * Do u n t i l x i s f i l l e d * /
/ * I s there a l i n k ? * /
/*Isbital?*/
/ * Yes, get new node * I
I* I t ' s 0 so g e t new node * /
I * Set i, j t o next b i t * /
I * B i t s l e f t t o decode? * /
i= 1. For encoded string 101010110101... of Example 8.2, the resulting mexErrMsgTxt("0ut o f code b i t s ? ? ? " ) ;
.
transition sequence is i = 1 , 3 , 1 , 2 , 5, 6, 1 , . . ; the corresponding out
.
sequence is -, 1-2 ( , -, -, -, 1-3 1 , - . .,where - is used to denote the / * I t must be a l e a f node * /
sence of an output. Decoded output values 2 and 3 are the first two pixel * ( x + kt+) = - * ( l i n k t n); I* Output value * I
the first column of test image f 2 in Example 8.2. I * S t a r t over a t r o o t * /
C function u n r a v e l accepts the link structure just described and uses i
drive the binary searches required to decode input h x . Figure 8.5 diagrams I * I s one l e f t over? * I
basic operation, which follows the decision-making process that was describe * ( x + kt+) = -*(link + n) ;
in conjunction with Table 8.3. Note, however, that modifications are needed t
compensate for the fact that C arrays are indexed from 0 rather than 1. d mexFunction(int nlhs, mxArray * p l h s [ ] ,
Both C and Fortran functions can be incorporated into MATLAB an i n t nrhs, const mxArray * p r h s [ ] )
serve two main purposes: (1)They allow large preexisting C and Fortran
grams to be called from MATLAB without having to be rewritten as M- double * l i n k , *x, xsz;
Chapter :mage Compression 8.2 % Coding Redundancy 307
unsigned short *hx; unravel, contains the C code that implements the link-based decod-
i n t hxsz; cess of Fig. 8.5. The gateway routine, which must always be named
I* Check inputs f o r reasonableness * / c t i o n , interfaces C computational routine u n r a v e l to M-file calling
i f ( n r h s != 3) n, huf f2mat. It uses MATLAB's standard MEX-file interface, which is
mexErrMsgTxt("Three inputs r e q u i r e d . " ) ;
else i f ( n l h s > 1 )
mexErrMsgTxt ( "Too many output arguments. " ) ; ur standardized input/output parameters-nlhs, plhs, nrhs, and prhs.
ese parameters are the number of left-hand-side output arguments (an
I* I s l a s t input argument a scalar? * /
i f (!mxIsDouble(prhs[2] ) I I mxIsComplex(prhs[2]) II eger), an array of pointers to the left-hand-side output arguments (all
mxGetN(prhs[2]) * mxGetM(prhs[2]) != 1 ) ATLAB arrays), the number of right-hand-side input arguments (an-
mexErrMsgTxt("1nput XSIZE must be a s c a l a r . " ) ; integer), and an array of pointers to the right-hand-side input argu-
(also MATLAB arrays), respectively.
/ * Create input matrix pointers and get scalar *I TLAB provided set of Application Program Interface (API) func-
hx = mxGetPr(prhs[O]) ; / * UINT16 *I
s. API functions that are prefixed with mx are used to create, access,
l i n k = mxGetPr(prhs[l]); / * DOUBLE *I
xsz = mxGetScalar(prhs[2]); I* DOUBLE *I manipulate, and/or destroy structures of class mxArray. For example,
m x C a l l o c dynamically allocates memory like a standard C calloc
I* Get the number of elements in hx * I function. Related functions include mxMalloc and mxRealloc that are
hxsz = mxGetM(prhs[O]); used in place of the C nzalloc and realloc functions.
I* Create ' x s z ' x 1 output matrix * / mxGetScalar extracts a scalar from input array prhs. Other mxGet . . .
plhs[O] = mxCreateDoubleMatrix(xsz, 1 , mxREAL); functions, like mxGetM, mxGetN, and mxGetString, extract other types
I* Get C pointer t o a copy of the output matrix * I
x = mxGetPr(plhs[O]); mxCreateDoubleMatrix creates a MATLAB output array for p l h s .
I * Call the C subroutine * / Other m x c r e a t e . . . functions, like m x c r e a t e s t r i n g and mxcreate-
unravel(hx, link, x, xsz, hxsz); NumericArray, facilitate the creation of other data types.
PI functions prefixed by mex perform operations in the MATLAB
}
nvironment. For example, mexErrMsgTxt outputs a message to the
ATLAB workspace.
The companion help text is provided in M-file u n r a v e l . m:
nction prototypes for the API mex and mx routines noted in item 2 of the
%UNRAVEL Decodes a variable-length b i t stream. ding list are maintained in MATLAB header files mex. h and m a t r i x . h,
% X = UNRAVEL(Y, LINK, XLEN) decodes UINT16 input vector Y based 0 ectively. Both are located in the < m a t l a b > / e x t e r n / i n c l u d e directory,
% transition and output table LINK. The elements of Y are re <matlab> denotes the top-level directory where MATLAB is installed
% considered t o be a contiguous stream of encoded b i t s - - i . e . , the
% MSB of one element follows the LSB of the previous element. InpU ur system. Header mex. h, which must be included at the beginning of all
% XLEN i s the number code words i n Y , and thus the size of output inclusion statement # i n c l u d e "mex. h " at the start
% vector X ( c l a s s DOUBLE). Input LINK i s a transition and output -file unravel), includes header file m a t r i x . h. The prototypes of the
% table ( t h a t drives a s e r i e s of binary searches): nes that are contained in these files define the para-
0
o ers that they use and provide valuable clues about their general operation.
% 1 . LINK(0) i s the entry point f o r decoding, i . e . , s t a t e n = 0. itional information is available in MATLAB's External Interfaces refer-
% 2 . If LINK(n) < 0 , the decoded output i s /LINK(n)( ; s e t n = 0.
% 3. If LINK(n) > 0 , get the next encoded b i t and transition t o gure 8.6 summarizes the preceding discussion, details the overall struc-
% s t a t e [LINK(n) - 11 i f the b i t i s 0, e l s e LINK(n). of C MEX-file unravel, and describes the flow of information between it
M-file huff 2mat. Though constructed in the context of Huffman decod-
Like all C MEX-files, C MEX-file u n r a v e l . c consists of two distinct par the concepts illustrated are easily extended to other C- and/or Fortran-
a computational routine and a gateway routine.The computational routine, a1 d MATLAB functions.
8.3 @
. Interpixel
Chapter 8 #! Image Compression
a
Because the gray levels of the images are not equally probable, variable-1 b
coding can be used to reduce the coding redundancy that would result FIGURE 8.8 A
natural binary coding of their pixels: lossless predlctive
coding model:
>> fl = imread('Randorn Matches.tifl); (a) encoder and
>7 cl = mat2huff (fl); (b) decoder.
>> entropy (f1 )
ans =
7.4253
>> imratio(f I , cl )
ans =
. 1 .0704
>> f2 = imread('A1igned Matches.tifl);
>> c2 = mat2huff(f2);
>> entropy ( f 2 )
ans = der and decoder, each contining an identical predictor. As each successive
7.3505 of the input image, denoted fa, is introduced to the encoder, the predic-
>> imratio(f2, c 2 ) enerates the anticipated value of that pixel based on some number of past
ans = t s . p e output of the predictor is then rounded to the nearest integer, de-
1.0821 d f,, and used to form the difference or prediction error
en = fN - ftl
Note that the first-order entropy estimates of the two images are about
same (7.4253 and 7.3505 bitslpixel); they are compressed similarly
mat2huff (with compression ratios of 1.0704 versus 1.0821). These obse
tions highlight the fact that variable-length coding is not designed to take
vantage of the obvious structural relationships between the aligned matc
Fig. 8.7(c). Although the pixel-to-pixel correlations are more evident in
image, they are present also in Fig. 8.7(a). Because the values of the pi frr = en + f n
either image can be reasonably predicted from the values of their nei us local, global, and adaptive methods can be used to generate f,. In
the information carried by individual pixels is relatively small. Much o f t cases, however, the prediction is formed by a linear combination of rn
sual contribution of a single pixel to an image is redundant; it could have be
guessed on the basis of the values of its neighbors. These correlations are t
underlying basis of interpixel redundancy.
In order to reduce interpixel redundancies, the 2-D
used for human viewing and interpretation must be
efficient (but normally "nonvisual") format. For exa
tween adjacent pixels can be used to represent an im
this type (that is, those that remove interpixel redundancy) are referred
mappings. They are called reversible mnppings if the original image ele
can be reconstructed from the transformed data set.
A simple mapping procedure is illustrated in Fig. 8.8. The approac
lossless predictive coding, eliminates the interpixel redundancies o
spaced pixels by extracting and coding only the new information in each
The new informution of a pixel is defined as the difference between the actu atial coordinates x and y. Note that prediction f ( x ,y) is a function of the
and predicted value of that pixel. As can be seen, the system consists of a evious pixels on the current scan line alone.
312 Chapter 8 a Image Compression 8.3 ;aYi Interpixel
M-functions mat2lpc and lpc2mat implement the predictive encodin ediction coefficients i n F and the assumption of 1 - D lossless
decoding processes just described (minus the symbol coding and dec edictive coding. If F i s omitted, f i l t e r F = 1 (for previous
steps). Encoding function mat2lpc employs a f o r loop to build simultan xel coding) i s assumed.
ly the prediction of every pixel in input x. During each iteration, xs, whit
gins as a copy of x, is shifted one column to the right (with zero paddin e also MAT2LPC.
on the left), multiplied by an appropriate prediction coefficient, and ad nargchk(1, 2, nargin) ) ; % Check input arguments
prediction sum p. Since the number of linear prediction coefficients is n % Set default f i l t e r i f omitted
ly small, the overall process is fast. Note in the following listing that if p
tion filter f is not specified, a single element filter with a coefficient o
used. Reverse the f i l t e r coefficients
%
Get dimensions of output matrix
%
function y = mat2lpc(x, f ) Get order of linear predictor
%
%MAT2LPC Compresses a matrix using 1 -D lossles predictive coding. Duplicate f i l t e r for vectorizing
%
% Y = MAT2LPC(X, F ) encodes matrix X using 1 - D lossless predictiv eros(m, n + order); Pad for 1st 'order' column decodes
%
% coding. A linear prediction of X i s made based on the
% coefficients i n F. If F i s omitted, F = 1 (for previous pixel ode the output one column at a time. Compute a prediction based
% coding) i s assumed. The prediction error i s then computed and the 'order' previous elements and add it to the prediction
% o u t p u t as encoded matrix Y . or. The result i s appended to the output matrix being b u i l t .
%
% See also LPC2MAT.
, j j ) = y(:, j ) + round(sum(f(:, order:-1:l) . * ...
error(nargchk(1, 2 , nargin)); % Check i n p u t arguments x ( : , ( j j - 1):-I:(]] - order)), 2 ) ) ;
if nargin < 2 % Set default f i l t e r if omitted
f = I;
end :, order t 1:end); % Remove l e f t padding
x = double(x); % Ensure double for computations er encoding the image of Fig. 8.7(c) using the simple first-order lin- EXAMPLE 8.5:
[m, n ] = s i z e ( x ) ; % Get dimensions of i n p u t matrix Lossless
p = zeros(m, n ) ; % Init linear prediction t o 0 predictive coding.
xs = x; zc = zeros(m, 1 ) ; % Prepare for input shift and pad f'(x, y) = round[af(x, Y - I ) ]
for j = l:length(f) % For each f i l t e r coefficient ... edictor of this form commonly is called aprevious pixel predictor, and the
xs = [zc x s ( : , 1:end - 1 ) ) ; % Shift and zero pad x onding predictive coding procedure is referred to as differential coding
p = p + f ( j ) * xs; % Form partial prediction sums ious pixel coding. Figure 8.9(a) shows the prediction error image that
end with (Y = 1. Here, gray level 128 corresponds to a prediction error of 0,
y = x - round(p); % Compute the prediction error
Decoding function lpc2mat performs the inverse operations of enc 14000 I I I I I FIGURE 8.9
counterpart mat2lpc. As can be seen in the following listing, it employs a (a) The predict~on
12000 - -
error image for
eration f o r loop, where n is the number of columns in encoded input mat -
Each iteration computes only one column of decoded output x, since 10000 - Fig. 8.7(c) with
- f = [I].
coded column is required for the computation of all subsequent col 8000 -
(b) Histogram of
decrease the time spent in the f o r loop, x is preallocated to its maxi the pred~ct~on
padded size before starting the loop. Note also that the computations e error.
ployed to generate predictions are done in the same order as they were
lpc2mat to avoid floating point round-off error.
Note that it is highly peaked around 0 and has a relatively small varianc
comparison to the input images gray-level distribution [see Fig. 8.7(d)].
reflects, as did the entropy values computed earlier, the removal of a great
of interpixel redundancy by the prediction and differencing process. We
clude the example by demonstrating the lossless nature of the predictive
ing scheme-that is, by decoding c and comparing it to starting image f :
>> g = l p c 2 m a t ( h u f f 2 m a t ( c ) ) ;
>> compare(f, g )
ans =
0
316 Chapter 8 Image Compression 8.5 a JPEG
EXAMPLE 8.7:
Combining IGS
low-order bit truncation. Note that the IGS implementation is quantization with
lossless predictive
that input x is processed one column at a tnme. To generate a and Huffman
4-bit result in Fig. 8.10(c), a column sum s-initially set to all ze coding.
as the sum of one column of x and the four least significant bits
(previously generated) sums. If the four most significant bits of any x val ss predictive coding, and Huffman coding to compress the image
11112,however, 00002is added instead.The four most significant bit to less than a quarter of its original size:
sulting sums are then used as the coded pxxel values for the col
processed. f = ~mread('Brushes.tlf');
-- quantize f u n c t l o n y = q u a n t l z e ( x , b, t y p e )
%QUANTIZE Quantizes t h e elements o f a UINT8 m a t r l x .
% Y = QUANTIZE(X, 8, TYPE) quantizes X t o B b l t s . Truncation i s
q = q u a n t l z e ( f , 4, ' l g s ' ) ;
qs = d o u b l e ( q ) / 16;
e = mat2lpc(qs) ;
c = mat2huff (e);
~ m r a t l o ( f ,c )
% used u n l e s s TYPE 1 s ' l g s ' f o r Improved Gray Scale q u a n t l z a t l o
e r r o r ( n a r g c h k ( 2 , 3, n a r g l n ) ) ; % Check i n p u t arguments
lf ndlms(x) -= 2 1 - ~ s r e a l ( x ) I ...
-isnumerlc(x) I - ~ s a ( x , ' u l n t 8 ' )
e r r o r ( ' T h e l n p u t must be a UINT8 numerlc m a t r l x . ' ) ; oded result c can be decompressed by the inverse sequence of operations
end hout 'inverse quantization'):
% Create b l t masks f o r t h e q u a n t l z a t l o n
10 = U l n t 8 ( 2 * ( 8 - b) - 1 ) ;
h l = ulnt8(2 8 - double(10) - 1 ) ;
A
8.5.1 JPEG
One of the most popular and comprehensive continuous tone, still frame co
pression standards is the JPEG (for Joint Photographic Experts Group) sta
dard. In the JPEG baseline codingsystern, which is based on the discrete co
a
b
FIGURE 8.14
JPEG 2000 block
diagram:
(a) encoder and
(b) decoder.
326 Chapter 8 :A Image Compression 8.5 JPEG Compression 327
ed to represent the original image and the analysis gain bits for subband
computed. For error-free compression, the transform used is biorthog band analysis gain bits follow the simple pattern shown in Fig. 8.15. For
with a 5-3 coefficient scaling and wavelet vector. In lossy applications, a 9 le, there are two analysis gain bits for subband b = 1HH.
efficient scaling-wavelet vector (see the wavef i l t e r function of Chapte error-free compression, p b = 0 and Rb = c6 so that Ah = 1. For irre-
employed. In either case. the initial decomposition results in four subban e compression, no particular quantization step size is specified. Instead,
low-resolution approximation of the image and the image's horizontal, umber of exponent and mantissa bits must be provided to the decoder on
cal, and diagonal frequency characteristics. band basis, called e,rplicit quantization, or for the NLLL subband only,
Repeating the decomposition process Nt times, with subsequent itera inzplicit quantization. In the latter case, the remaining subbands are
restricted to the previous decomposition's approximation coefficients, ized using extrapolated NLLL subband parameters. Letting co and pobe
duces an NL-scale wavelet transform. Adjacent scales are related spatiall umber of bits allocated to the NLLL subband, the extrapolated parame-
powers of 2, and the lowest scale contains the only explicitly defined a for subband b are
mation of the original image. A s can be surmised from Fig. 8.15, where
tation of the standard is summarized for the case of NL = 2, a ge Pb = Po
NL-scale transform contains 3NL + 1 subbands whose coefficients are d ~b = EU + nsdb - nsdo
ed ah for b = NLLL, NLHL, ... , l H L , l L H , 1HH. The standard doe nsdb denotes the number of subband decomposition levels from the
specify the number of scales t o be computed. 1 image to subband b. The final step of the encoding process is to code
After the NL-scale wavelet transform has been computed, the total n ntized coefficients arithmetically on a bit-plane basis. Although not
of transform coefficients is equal to the number of samples in the o in the chapter, arithrneric coding is a variable-length coding proce-
image-but the important visual information is concentrated in a few co ,like Huffman coding, is designed to reduce coding redundancy.
cients. To reduce the number of bits needed to represent them, coeffi ustom function im2 j peg2k approximates the JPEG 2000 coding process
al,(u, v) of subband b is quantized to value qh(ll, v) using .8.14(a) with the exception of the arithmetic symbol coding. As can be
[lflbt;v)l] the following listing, Huffman encoding augmented by zero run-length
-
qb(u, v) = sign[ah(u, v)] floor --- is substituted for simplicity.
where the "sign" and "floor" operators behave like MATLAB functions ion y = im2jpeg2k(x, n, q) im2j peg2k
PEG2K Compresses an image using a JPEG 2000 approximation. ".
same name (i.e.. functions s i g n and f l o o r ) . Quantization step size Ai,is
= IM2JPEG2K(X, N, Q ) compresses image X using an N-scale JPEG
For roc11eIen7r1ttof
x. sign(x) retrlms 1
A, = 2R,J-i.,(1 + $) K wavelet transform, implicit or explicit coefficient
uantization, and Huffman symbol coding augmented by zero
if the elrtttet~ris un-length coding. If quantization vector Q contains two
,sreorcrrllflt~zero, 0 if where Rh is the nominal dynamic range of subband 0, and E(, and pl, are lements, they are assumed t o be implicit quantization
-,
it ecl~~nls
i f it is
zero.
zero, rrr~rl
,hen
number of bits allotted to the exponent and mantissa of the subband's co
cients. The nominal dynamic range of subband b is the sum of the numb
ameters; e l s e , i t i s assumed t o contain explicit subband step
es. Y i s an encoding structure containing Huffman-encoded
ata and additional parameters needed by JPEG2K2IM f o r decoding.
FIGURE 8.1 5 See also JPEG2K2IM.
JPEG 2000 two-
scale wavelet
transform r(nargchk(3, 3, nargin)) ;
coefficient % Check input arguments
notation and dims(x) -= 2 1 - i s r e a l ( x ) I -isnumeric(x) ( - i s a ( x , ' u i n t 8 ' )
analysis gain rror( 'The input must be a UINT8 image. ' ) ;
(in the circles).
error (nargchk(1, 1, nargin) ) ; % Check i n p u t arguments cal (i.e., their basis functions are limited
% Get decodlng parameters: scale, quantlzatlon vector, run-leng
% table s l z e , zero run code, end-of-data code, wavelet bookkeep
% array, and run-length table.
n = double(y.n);
q = double(y.q) / 100;
runs = double(y.runs); EXAMPLE 8.9:
rlen = length(runs); cted from an encoding that compressed JPEG 2000
zrc = -double(y.zrc); as generated from an 88 : 1; encoding. compression.
eoc = zrc - 1;
s = double(y.s);
s = reshape(s, n + 2, 2 ) ; the JPEG 2000's bit-plane-oriented arithmetic coding, the
% Compute the slze of the wavelet transform. n rates just noted differ from those that would be obtained by a true
c l = prod(s(1, : ) ) ; encoder. In fact, the actual rates would increase by a factor of 2.
f o r 1 = 2:n + 1 ression of the results in the left column of Fig 8.16 is
c l = c l + 3 * prod(s(1, : ) ) ;
end
% Perform Huffman decodlng followed by zero run decoding. -based JPEG results of Figs.
r = huff2mat(y.huffman); veals a noticeable decrease of error
c=[]; zl=flnd(r==zrc); 1 = 1 ;
f o r J = 1 :length(zl)
c = [ C r ( l : z l ( ] ) - 1 ) zeros(1, r u n s ( r ( z l ( 1 ) + I ) ) ) ] ;
1 = z ~ ( J )+ 2; 0-based coding dramatically in-
end sense) image quality. This is particularly evident in
z l = f l n d ( r == eoc); % Undo terminating zero run e blocking artifact that dominated the corresponding
~f length(z1) == 1 % or l a s t non-zero run.
c = [c r ( l : z l - I ) ] ;
c = [ c zeros(1, c l - l e n g t h ( c ) ) l ;
else
c = [C r(l:end)]; els.The results of Fig. 8.16 were generated with the
end
% Denormallze the coefflclents.
c = c + ( c > 0) - ( c < 0 ) ; f = imread('Tracy.tlfl);
f o r k = 1:n cl = 1m2]peg2k(f, 5, [8 8 . 5 1 ) ;
q1=3*k-2;
c = wavepaste('h', c , s , k, wavecopy('h', c, s , k ) * q ( q 1 ) ) ;
c = wavepaste('v', c, s , k , wavecopy('vl, c , s , k ) * q ( q l + 1 )
c = wavepaste('d', c, s , k , wavecopy('d', c, s , k ) * q ( q l + 2 )
end
c = wavepaste('al, c , s , k, wavecopy('a', c , s , k ) * q ( q l + 3 ) ) ; ' crl = i m r a t l o ( f , cl )
332 Chapter 8 u
Summary 333
a b
c d
e f
FIGURE 8.1 6 Left
column: JPEG
2000
approxlmatlons of
Fig. 8.4 using five
scales and lmpliclt
quantization with
= 8 and
EO = 8.5. Rlght
column: Slmllar
results wlth
E" = 7.
9.1 r'relim
erlying the development of
s that extract "meaning" from an image. Other approaches are de-
nd applied In the remaining chapters of the book
Preliminaries
I j.
> $ti>a:
f
'it
<$
"!
Some Basic Concepts from Set Theory
Preview whose coordinates and amplitude (i e., mtensity) values are integers.
be a set in z2,the elements of which are pixel coordinates ( x , y). If
zu E A
arly, if w is not an element of A, we write
we A
t B of pixel coordinates that satisfy a particular cond~tionis written as
Images, and dlscuss binary sets and logical operators. In Section 9.2 we B = condition)
two fundamental morphological operations, dihrlon and eroscon, in te
have applications that are unique to gray-scale images, such as peak fi C=AnB
The material in this chapter begins a transition from image-pro
methods whose Inputs and outputs are images, to image analysis m
whose outputs in some way descrlbe the contents of the Image. Morpholo
336 Chapter 9 B Morphological Image Processing 9.2 #8 Dilation and Erosion
A -B ={~U~WEA,W~B}
simple illustration, Fig. 9.3 shows the results of applying several logical
Figure 9.1 illustrates these basic set operations.The result of each operati rs to two binary images containing text. (We follow the IPT conven-
shown In gray t foreground (1-valued) pixels are displayed as white.) The image in
In addition to the preceding basic operations, morpholog~calopera
often require two operators that are specificto sets whose elements are
coordinates. The reflectron of set B, denoted B, is defined as he letters in "UTK" and "GT" over-
[Fig. 9.3(f)] shows the letters in "UTK"
a b
FIGURE 9.2 TABLE 9.1
(d) Translat~onof MATLAB Expression
Using logical
Abyz Set Operation for Binary Images Name
(b) Retlect~onot expressions in
B The sets A and AnB A&B AND MATLAB to
B are from AUB A I B OR perform set
hg. 9 1. A" -A NOT operations on
A-B A&-B DIFFERENCE binary images
338 Chapter 9 a Morphological Image Processing 9.2 % Dilation and Erosion 339
FIGURE 9.4
Illustration of
dilation.
(a) Original image
with rectangular
object.
(b) Structuring
element with five
pixels arrang,edin
a diagonal line.
The structuring element translated to The origin of the
these locations does not overlap any structuring
1-valued pixels in-the original image. element is shown
with a dark
border.
(c) Structuring
element
translated to
several locations
on the image.
(d) Output irnage.
, --. "4" locations. the
*-
, j -A - - - -- --'
,- -"- structurlne element
I
a b c
d e f
FIGURE 9.3 (a) B~naryImage A. (b) Binary image B. (c) Complement -A. (d) Union A I B. (e) Intersectio
(f) Set d~fferenceA & -6.
A & B.
cL> {) c) i; t% +i1 1 1 1 1 1 1 1 ti u q
9.,:2,,7 Dilation it ;j (1 ( 8 81 1 1 1 1 1 1 1 1 1 i; ; I 1 ;
3 1
P
$fh~ ~
structuring element can be decomposed into a five-element row of 1s and
~afive-elementcolumn of Is:
The following commands read the image from a file, form the structuring e
ment matrix, perform the dilation, and display the result.
>> A = imread('broken-text.tifl);
>> B = [ O 1 0 ; 1 1 1; 0 1 0 1 ;
>> A 2 = imdilate(A, 8);
>> imshow(A2)
a b
FIGURE 9.6
A slmple example
of d~lation. erhead associated with each dilation operation, and at least
(a) Input Image tions are required when using the decomposed form. How-
conta~~n~ngbroken eed with the decomposed implementation is still significant.
text. ~(b)
Dllated
Image
The s t r e l Function
function strel constructs structuring elements with a variety of shapes
sizes. Its basic syntax is
F se = strel(shape, parameters)
342 Chapter 9 ! Morphological Image Processing 9.2 % Dilation and Erosion 3843
where shape is a string specifying the desired shape, and parameters TABLE 9.2
of parameters that specify information about the shape, such as its size. Description
The various
ample, s t r e 1 ( ' diamond ' , 5 ) returns a diamond-shaped structuring e Creates a flat, diamond-shaped syntax forms of
that extends f5 pixels along the horizontal and vertical axes. Table 9. structuring element. where R specifies the function s t r e l .
marizes the various shapes that s t r e l can create. distance from the structuring element (The word flnt
In addition to simplifying the generation of common structuring e origin to the extreme points of the means that the
shapes, function s t r e l also has the important property of producing st diamond. structuring
ing elements in decomposed form. Function i m d i l a t e automatically t r e l ( ' d i s k l ,R ) Creates a flat. disk-shaped structuring element has zero
decomposition information to speed up the dilation process. The follo element with radius R. (Additional height. This is
parameters may be specified for the disk; mt:aningful only
ample illustrates how s t r e l returns information related to the decompo for gray-scale
of a structuring element. see the s t r e l help page for details.)
s t r e l ( ' l i n e ' , LEN, DEG) Creates a flat, linear structuring element, dilation and
where LEN specifies the length, and DEG erosion. See
EXAMPLE 9.2: Consider again the creation of a diamond-shaped structuring element Section 9.6.1.)
A n illustration of specifies the angle (in degrees) of the
strel: line, as measured in a counterclockwise
structuring
element direction from the horizontal axis.
decomposition >> s e = s t r e l ( ' d i a m o n d l , 5 )
strel('octagon', R) Creates a flat, octagonal structuring
using s t r e l . se = element, where R specifies the distance
Flat STREL object containing 61 neighbors. from the structuring element origin to the
sides of the octagon, as measured along
Decomposition: 4 STREL objects containing a t o t a l of 17 neigh the horizontal and vertical axes. R must
Neighborhood: be a nonnegative multiple of 3.
0 0 0 0 0 1 0 0 0 0 0 = s t r e l ( ' p a i r ' , OFFSET) Creates a flat structuring element
0 0 0 0 1 1 1 0 0 0 0 containing two members. One member is
0 0 0 1 1 1 1 1 0 0 0 located at the origin.The second
0 0 1 1 1 1 1 1 1 0 0 member's location is specified by the
0 1 1 1 1 1 1 1 1 1 0 vector OFFSET, which must be a two-
1 1 1 1 1 1 1 1 1 1 1 element vector of integers.
0 1 1 1 1 1 1 1 1 1 0 = s t r e l ( ' p e r i o d i c l i n e ' , P, V Creates a flat structuring element
0 0 1 1 1 1 1 1 1 0 0 containing 2*P + i members. v is a two-
0 0 0 1 1 1 1 1 0 0 0 element vector containing integer-valued
0 0 0 0 1 1 1 0 0 0 0 row and column offsets. One structuring
0 0 0 0 0 1 0 0 0 0 0 element member is located at the origin.
The other members are located at I *V,
We see that s t r e l does not display as a normal MATLAB matrix; it retu -1*V,2*V,-2*V, . . . , P*V,and-P*V.
instead a special quantity called an strel object. The command-window disp = s t r e l ( ' r e c t a n g l e ' ,M N ) Creates a flat, rectangle-shaped
of an strel object includes the neighborhood (a matrix of Is in a diam structuring element, where MN specifies
shaped pattern in this case); the number of I-valued pixels in the struct the size. MN must be a two-element vector
element (61); the number of structuring elements in the decomposition of nonnegative integers.The first element
and the total number of 1-valued pixels in the decomposed structuring of MN is the number rows in the
structuring element; the second element
ments (17). Function getsequence can be used to extract and examine se
is the number of columns.
rately the individual structuring elements in the decomposition.
e = s t r e l ( ' s q u a r e l ,W) Creates a square structuring element
whose width is W pixels. w must be a
getsequence >> decomp = g e t s e q u e n c e ( s e ) ;
nonnegative integer scalar.
>> whos
Name Size e = s t r e l ( ' a r b i t r a r y ' , NHOOD) Creates a structuring element of
ClassBytes e = strel(NH0OD)
arbitrary shape. NHOOD is a matrix of
decomp 4x1 1716 s t r e l object 0s and Is that specifies the shape.The
se 1 xl 3309 s t r e l object second, simpler syntax form shown
Grand t o t a l i s 495 elements using 5025 bytes performs the same operation.
YIorphological Image Processing 9.2 #?k Dilation and Erosion
The output of whos shows that s e and decomp are both strel objec
further, that decomp is a four-element vector of strel objects.The four st "shrinks" or "thins" objects in a binary image. A s in dilation, the man-
ing elements in the decomposition can be examined individually by ind extent of shrinking is controlled by a structuring element. Figure 9.7 il-
into decomp: the erosion process. Figure 9.7(a) is the same as Fig. 9.4(a).
7(b) is the structuring element, a short vertical line. Figure 9.7(c)
22 decomp ( 1 ) ly depicts erosion as a process of translating the structuring element
ans = ut the domain of the image and checking to see where it fits entirely
F l a t STREL o b j e c t c o n t a i n i n g 5 n e i g h b o r s .
0 [I 0 (1 (1 9 0 0 (I li 0 0 0 0 I! 1 1 0
Neighborhood: II !i ii 0 o (I I! 11 o o o ii o I! !I o (I
0 1 0 11 I1 (1 0 0 0 (I 11 0 0 0 [I 0 11 f j 0 (1 d,
1 1 1 !) 0 f! 0 0 0 0 0 !J 0 0 0 0 (1 0 i! i!
0 0 0 0 0 O 0 0 111 11 0 0 0 0 11 !.I 0
FIGURE 9.7
0 1 0 Illustration of
>> decomp(2)
0 ii !:
11 (! !)
0 0 1 1
(1 o 1 1
1 1 1 1 1 0 ii 0 0 11
1 1 1 1 1 II ii (! 0 (I Ik1'i.
-.
erosion.
0 ii (1 il 0 1 1 1 1 1 1 1 0 !j i~t i i! 1 (a) Original Image
ans = 0 0 0 I1 11 0 0 0 (j li 0 0 0 ('1 0 i t 0 with rectangular
0 ti 0 0 0 0 ij 0 0 1) (1 I) 0 ii I! 0 0 object.
F l a t STREL o b j e c t c o n t a i n i n g 4 n e i g h b o r s . 11 11 o o 0 II 11 I) {I (1 o i t ti o 0 !I (b) Structuring
i t 11 (1 (1 o [I i! !I o o i) 11 (I r] o (1 element with
Neighborhood: !j 0 0 11 I! 0 0 il il t i t i 0 (1 i,!11 !I !) three pixels
0 1 0 arranged In a
1 0 1 Output is zero in these locations because vertlcal Ime. The
0 1 0 the structuring element overlaps the origin of the
structuring
>> decomp(3) element 1s shown
ans = with a dark
border.
F l a t STREL o b j e c t c o n t a i n i n g 4 n e i g h b o r s . (c) Structuring
element
Neighborhood: translated to
0 0 1 0 0 several locations
0 0 0 0 0 on the image.
1 0 0 0 1 (d) Output image.
0 0 0 0 0
0 0 1 0 0
>> decomp(4)
ans = (> 0 i! II o (1 (I o
II !I 0
\! o 0 11 11 o
[I 11 0 11 i! !J 11 0 (1 0 fl ! I
!I 0 I1 !I 0
F l a t STREL o b j e c t c o n t a i n i n g 4 n e i g h b o r s . II 1 1 I! (j (I [I 1 1 11 ;I i j 0 ( 1 II II o o 11
0 0 !I 0 0 i) il 0 0 i! i) 0 !! 0 0 11 !)
Neighborhood: (1 i) ( 1 11 !I !I (1 (I I ) II 11 i) (1 o !I I I 11
0 1 0 0 ii ii 11 0 0 t l 0 !j 0 li 0 11 !I i! 1: 0
1 0 1 ii $ 1 ( 1 !J 0 1 1 1 1 1 1 1 0 O 0 11
0 1 0 ( 1 (1 I; () a ~ i !I (1 ( 1 0 i j 61 ii 11 0 11 0 !i
0 i) 11 1 1 11 0 0 0 !I i l !I 0 0 0 I 1 I1 i l
1; ( j ii I 1 1) ;) 1) 0 [I i i il f) i j (I 11 I] ( 1
Function i m d i l a t e uses the decomposed form of a structuring element 1) ,I t i i j (J ij 11 i i t l 11 (I 11 11 il !I o (1
tomatically, performing dilation approximately three times faster ( s 611 I, 11 (; [ j i! (1 11 0 i! (1 (I [I !I II I) o !,t
0 (j 0 I 1 ( 1 (1 0 1.1 (1 i t li It (I 'I 11 I1 f !
than with the non-decomposed form.
346 Chapter 9 s* P\~lorphological
Image Processing 9.3 a Combining Dila ti01 and Erosion
= imread('wirebond-maskstif');
= strel('diskl,10);
= imerode(A, se) ; imerode
A 8B = { z I ( B ) , n A Cf 0 )
In other words, erosion of A by B is the set of all structuring element origi = strel('diskl, 5);
cations where the translated B has no overlap with the background of A. = imerode(A, se) ;
FIGURE 9.8 An
illustration of Combining Dilation and Erosion
erosion. tical image-processing applications, dilation and erosion are used most
(a) Original
image. in various combinations. An image will undergo a series of dilations
(b) Erosion with a
disk of radius 10.
(c) Erosion with a
disk of radius 5.
(d) Erosion with a
disk of radius 20.
d region in Fig. 9.9(c); this region is the complete opening.The white re-
in this figure are areas where the structuring element could not fit
348 Chapter 9 llai Morphological Image Processing 9.3 Combining Dilation and Erosion 349
a b:
c d-.
FIGURE 9.10
Illustration of
opening and
closing.
(a) Original
image.
(b) Opening.
(c) Closing.
is example illustrates the use of functions imopen and imclose. The EXAMPLE 9.4:
e shapes. tif shown in Fig. 9.10(a) has several features designed to illus- Working with
the characteristic effects of opening and closing, such as thin protrusions, ~~~,","~~smeOpen
,gulfs, an isolated hole, a small isolated object, and a jagged boundary.The
wing commands open the image with a 20 x 20 structuring element:
e = strel('squarel,20);
A.B = (A$B)GB o = imopen(f, se);
Geometrically, A . B is the complement of the union of all translation
that do not overlap A . Figure 9.9(d) illustrates several translations of B t
fc = imclose(f, se);
C = imopen(A, 0 )
and lier opening has the net effect of smoothing the object quite significantly.
close the opened image as follows:
C = imclose(A, 0)
f oc = imclose(f o, se) ;
where A is a binary image and B is a matrix of 0s and 1s that specifies the stru
turing element. A strel obiect. SE. can be used instead of 0.
ure 9.10(d) shows the resulting smoothed objects.
350 Chapter 9 B Morphological Image Processing 9.3 r Combining Dilation and Erosion 3.51
,! 0 i! ;) 1 1 il (1 !I 0 11 '1 ti
ir ii il ,: a b
0 11 1 ! J ; I (1 it (1 i j t l i) ;! 1 1 (1 Il 81 C
el (1 1 I,! (1 1 1 1 1 1 1 , I { J i j 11 (1 ,: BI d e
0 1 1 1 I ' ii I ] !I (: ! I !i i l 1 1 11 I! f
(> i j 1 !I !! 81 .:J :I i~{ ) !> 1 1 1 !: 1 g
11 i j I I (1 1 (> i! I t (>
t; (1 i! g! I (J l a 1
(j !I 0 ;; 1 1 1 !; i j I! (1 ( I I! ij I I i; 1 FIGLIRE 9.1 2
: I ( j ( 1 ,I $! 1 !! t, <!g ) !! <J
G, i! ij s 8
(a) Original image
i! !x, t i i; [I 1 1 {i (1 !\ l~i0 (1
$ > I ? {I i l
A. (b) Structuring
element BI.
(c) Erosion of A
0 {) 11 \! :j l > () f ! !! i) < I (1 r j I 1 1; ,,
a b c (1 ( 1 c, 1; {i I! (; () I ! (1 ;! f) ( 1 by Bl.
(d) Complement of
I.', !! (1 :! (1 (1 !I <; { ) i l l! (; ij f > ii i,)
ening followed by closing. (0 the original image,
i! 1) I(! i) 11 (I 0 1, 0 i! (1 0 i l (1
A'. (e) Structuring
f !
[I 11 (> I > I! (j [ I ;i !I 11 (1 ii ! I 1 $ 1 :;
(1
~elernentB2.
!I ll (: !I (i ( j I! !i I1 il !! ! I 0
(f) Erosion of A'
1) 1 ) (1 ij !\ 1 \>
'j \I !; 11 0 $1 I!
Figure 9.11 further illustrates the usefulness of closing and opening 11 ll 0 ti : I (1 11 (1 :i {I (1 (1 (1 li gj ;;
by B2. (9) Output
plying these operations to a noisy fingerprint [Fig. 9.ll(a)J.The comman ll I? k t ?: [ I 0 (.8 {I t! 11 :I t~ (1 t:
image.
>> f = imread('fingerprint.tifl); 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>> se = strel('squarel, 3); 1 1 ~ ! 1 1 1 1 1 1 1 1 1 1 1 1 1
>> f o = imopen(f, s e ) ; I I ! ~ ~ I I ~ ~ ! ! ~ ) ~
B?
I I ~ ~ ~ ~ I
>> imshow(fo) 111il!'!111 1 l l l l ' i t i 1 1
1 1 1 1 1 1 1 1 I 1 1 1 1 0 ~ ~ 0 1
1 1
1 1 1 1 1 ~ ! 1 1 1 1 1 1 1 0 1 1
produced the image in Fig. 9.11(b). Note that noisy spots were remove I I I I ! ; ! ! ~ I I I I ~ I I ~ I ~
opening the image, but this process introduced numerous gaps in the ridg l l l l ~ ~ r l l ~ ~ l l ~1 l 1l t
the fingerprint. Many of the gaps can be filled in by following the opening ~ l ~ l l l ~ l l l l l ~ l t l
a closing:
l ~ ~ l i ~ l l l l l l l I l1 l l
1 i j 1 !: 1 ! I I: i ) :1 i ) 11 1 1 1 1 1
>> f o c = i r n c l o s e ( f o , s e ) ;
i! 0 11 ;! L! 1 1 1 1 1 1 11 1: i l 11 1
>> ~ . ~ s ~ o w ( ~ o c ) 1 1 1 1 !) 1 I ! i ; :>I! I \ i! \i $ 1 :>
1 1 i ] 0 .'! i j 1 1 1 1 0 :! ! I 8,) 1
Figure 9.11(c) shows the final result. 1 1 1 1 :; I, # I I I :I 1 1 1 ( I t! i,! t i < i
1 1 1 1 ~ ~ 1 ~ i 1 1 1 1 I i ! I ~ i 1
3,3,.2 The Hit-or-Miss Transformation 1 1 I i 1 : 1 ! ' i 1 ' l 1 1 11 1 1 1 1
I l l l ! ! l i ~ l l l l l l l l l
Often, it is useful to be able to identify specified configurations of pixels, s
i j i j i) 0 I?11 0 i, !j
(I 'j i ) 11 I) :J
as isolated foreground pixels, o r pixels that are end points of line segme [I 11 !I (1 t ) ( 1 I! 0 11 !J (1 (I i! i t I I 0
The hit-or-miss transformation is useful for applications such as these. The (1 !I (1 (': <! 0 < \ ( j 0 { I i \ :) ii (1 II (1
or-miss transformation of A by B is denoted A @ B. Here, B is a structuri o (1 1 ,I I > : i ;I i ) 1 1 it I I ,,I i ) 11 (1 I )
element pair, B = ( B ,, B2),rather than a single element, as before.The hit- :! i l (j i! i l il i l ii I) I t (1 !) [ I i! ( i !l
I 1 i] i l 11 i) I ! \ I i] I 1 i) i l !! (i 1 1 (! 1)
Figure 9.12 shows how the hit-or-miss transformation can be used to ide
fy the locations of the following cross-shaped pixel configuration:
re 9.12(a) contains this configuration of pixels in two different locations.
0 1 0 ion with structuring element BI determines the locations of foreground
1 1 1 s that have north, east, south, and west foreground neighbors. Erosion of
0 1 0 omplement with structuring element B2 determines the locations of all
ixels whose northeast, southeast, southwest, and northwest neighbors
352 Chapter 9 3 Morphological Image Processing 9.3 %k Combining Dilation and Erosion 353
belong to the background. Figure 9.12(g) shows the intersection (1 single-pixel dot in Fig. 9.13(b) is an upper-left-corner pixel of the objects
AND) of these two operations. Each foreground pixel of Fig. 9.12(g) is t .9,13(a).The pixels in Fig. 9.13(b) were enlarged for clarity. @a
cation of a set of pixels having the desired configuration.
The name "hit-or-miss transformation" is based on how the result is Using Lookup Tables
the hit-or-miss structuring elements are small, a faster way to compute
sformation is to use a lookup table (LUT).The technique is
e neighborhood config-
use. For instance, there
= 512 different 3 X 3 configurations of pixel values in a binary image.
bwhitmiss, which has the syntax of lookup tables practical, we must assign a unique index
sible configuration. A simple way to do this for, say, the 3 X 3 case,
C = bwhitmiss(A, 81, 82) ly each 3 X 3 configuration element-wise by the matrix
where C is the result,A is the input image, and 81 and 82 are the structuri 1 8 64
ements just discussed. 2 16 128
4 32 256
EXAMPLE 9.5:
Csing IPT
n sum all the products. This procedure assigns a unique value in the
function h different 3 X 3 neighborhood configuration. For exam-
bwhitmiss. value assigned to the neighborhood
west, west, or southwest neighbors (these are "misses"). These requir 1 1 0
lead to the two structuring elements: 1 0 1
1 0 1
>> 81 =
>> 82 =
strel([O 0 0; 0 1 1 ; 0 1 01);
strel([l 1 1; 1 0 0 ; 1 0 01);
+ 2(1) + 4(1) + 8(1) + 16(0) + 32(0) + 64(0) + 128(1) +
= 399, where the first number in these products is a coefficient from
Note that neither structuring element contains the southeast neighbor, w receding matrix and the numbers in parentheses are the pixel values,
is called a don't care pixel. We use function bwhitmiss to compute the tr
s two functions, makelut and a p p l y lut (illustrated
formation, where f is the input image shown in Fig. 9.13(a):
t can be used to implement this technique. Function ?...
>> g = bwhitmiss(f, B1 , 8 2 ) ;
a lookup table based on a user-supplied function, and vi ,&gakelut
,.::,,",y.
binary images using this lookup table. Continuing with ~".d~~@'plylut
>> imshow(g)
makelut requires writing a function that accepts a 3 X 3
a b matrix and returns a single value, typically either a 0 or 1. Function
FIGURE 9.13 ut calls the user-supplied function 512 times, passing it each possible
(a) Or~glnal neighborhood. It records and returns all the results in the form of a 512-
midge (b) Result
of applying the rite a function, endpoints. m, that uses makelut and
hlt-or-miss oints in a binary image. We define an end point as a
tr%nsforn~at~on
(the dots shown
at has exactly one foreground neighbor. Function
were enlarged to oints computes and then applies a lookup table for detecting end points
faclhtate vlewlng) input image. The line of code
persistent lut stent
EXAMPLE 9.6: if## A n interesting application of lookup tables is Conway's "Game of ction out = conwaylaws(nhood) conwaylaws
Playing Conway's which involves "organisms" arranged on a rectangular grid. We include it ONWAYLAWS Applies Conway's genetic laws to a single pixel. '"UUi
Game of Life as another illustration of the power and simplicity of lookup tables. There a OUT = CONWAYLAWS(NHO0D) applies Conway's genetic laws to a single
using binary
images and simple rules for how the organisms in Conway's game are born, survive, pixel and its 3-by-3neighborhood, NHOOD.
lookup-table- die from one "generation" to the next. A binary image is a convenient re m-neighbors = sum(nhood( : ) ) - nhood(2, 2);
based sentation for the game, where each foreground pixel represents a living or nhood(2, 2) == 1
computation. ism in that location. if num-neighbors <= 1
Conway's genetic rules describe how to compute the next generatio out = 0; % Pixel dies from isolation.
next binary image) from the current one: elseif num-neighbors > = 4
out = 0; % Pixel dies from overpopulation.
1. Every foreground pixel with two or three neighboring foreground p
survives to the next generation. out = 1 ; % Pixel survives.
2. Every foreground pixel with zero, one, or at least four foreground nei
bors "dies" (becomes a background pixel) because of "isolation'
"overpopulation." if num-neighbors == 3
3. Every background pixel adjacent to exactly three foreground neighbo out = 1; % Birth pixel.
a "birth" pixel and becomes a foreground pixel.
out = 0; % Pixel remains empty.
All births and deaths occur simultaneously in the process of computing
next binary image depicting the next generation.
Morphological Image Processing 9.3 !% Combining Dilation and Erosion 357
The lookup table is constructed next by calling makelut with a fu TABLE 9.3
handle to conwaylaws: Operations
supported by
>> lut = makelut(@conwaylaws, 3); function bwmorph.
Various starting images have been devised to demonstrate the effect Remove isolated foreground pixels.
way's laws on successive generations (see Gardner, [1970,1971]). Cons Closing using a 3 x 3 structuring element; use imclose for other
example, an initial image called the "Cheshire cat configuration," structuring elements.
Fill in around diagonally connected foreground pixels.
>>bwl ~ [ O O O O O O O O O O Dilation using a 3 X 3 structuring element; use imdilate for
0 0 0 0 0 0 0 0 0 0 other structuring elements.
0 0 0 1 0 0 1 0 0 0
0 0 0 1 1 1 1 0 0 0 Erosion using
structuring a 3 X 3 structuring element; use imerode for other
elements.
0 0 1 0 0 0 0 1 0 0
0 0 1 0 1 1 0 1 0 0 Fill in single-pixel"holes" (background pixels surrounded by
0 0 1 0 0 0 0 1 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 Remove H-connected foreground pixels.
O O O O O O O O O O ] ; Make pixel p a foreground pixel if at least five pixels in N s ( p )
(see Section 9.4) are foreground pixels; otherwise make p a
The following commands perform the computation and display u p to the background pixel.
generation: Opening using a 3 X 3 structuring element; use function imopen
for other structuring elements.
>> imshow(bw1, ' n ' ) , title('Generati0n 1 ' ) Remove "interior" pixels (foreground pixels that have no
>> bw2 = applylut(bw1, lut); background neighbors).
>> figure, imshow(bw2, 'n'); title('Generati0n 2 ' ) Shrink objects with no holes to points; shrink objects with holes
>> bw3 = applylut(bw2, lut);
>> figure, imshow(bw3, 'n'); title('Generati0n 3 ' ) Skeletonize an image.
Remove spur pixels.
We leave it as a n exercise to show that after a few generations the cat fad Thicken objects without joining disconnected Is.
a "grin" before finally leaving a "paw print."
Thin objects without holes to minimally connected strokes;
thin objects with holes to rings.
9J.4 Function bwmcnrph "Top-hat" operation using a 3 x 3 structuring element;
IPT function bwmorph implements a variety of useful operations based use imtophat (see Section 9.6.2) for other structuring
binations of dilations, erosions, and lookup table operations. Its calling synt
g = bwmorph(f, operation, n )
a b c
FIGURE 9.15 (a) Fingerprint image from Fig. 9.11(c) thinned once. (b) Image thinned twice. (c) I
thinned until stability.
>> for k = 1 : 5
fs = fs & -endpoints(fs);
end
FIGURE 9.1 7
FIGURE 9.1 9
containing ten Connected
objects (b) A components
subset of pixels (a) Four
4-connected
components.
(b) Two
8-connected
components.
(c) Label matrix
obtained using
4-connectivity
(d) Label matrix
obtained using
8-connectivity.
a: b G,:
&"e-
k g,
FIGURE 9.18 (a) Pixelp and its
4-neighbors,N4(p) (b) Pixel p
and its diagonal neighbors,
ND(p).(c) Pixelp and its
8-neighbors,Ns(p). (d) Pixels p
and q are 4-adjacent and m connected component was just defined in terms of a path, and the
8-adjacent (e) IPixelsp and q are of a path in turn depends on adjacency. 111s implies that the nature
8-adjacent but not 4-adjacent
( f ) The shaded pixels are both connected component depends on which form of adjacency we choose,
4-connected and 8-connected. 4- and 8-adjacency being the most common. Figure 9.19 illustrates the ef-
(g) The shaded foreground
pixels are 8-connected but not
ected components. Figure 9.19(b) shows that choosing 8-adjacency re-
he number of connected components to two.
I_I - function bwlabel computes all the connected components in a binary
e. The calling syntax IS
these concepts. A path between pixels pl and p, is a sequence o [ L , num] = bwlabel(f, conn)
p,,p2,. . . , p n P 1 ,pn such that pk is adjacent to p k + ~ for
, 1 5 k < n.
can be 4-connected or 8-connected, depending on the definition of adjac f is an input binary image and conn specifies the deslred connectiv~ty
used. r 4 o r 8). Output L is called a label matnx, and num (optional) gives the
Two foreground pixels p and q are said to be Cconnected if there e number of connected components found. If parameter conn is omitted,
lue defaults to 8. Figure 9.19(c) shows the label matrix corresponding to
.19(a), computed using bwlabel (f , 4).The pixels In each different con-
d component are assigned a unique integer, from 1 to the total number
onnected components. In other words, the p ~ x e l slabeled 1 belong to the
362 Chapter 9 Morphological Image Processing 9.5 icn Morphological Re(
first connected component; the pixels labeled 2 belong to the second FIGURE 9.20 Centers
ed component; and so on. Background pixels are labeled 0. Figure of mass (white
shows the label matrix corresponding to Fig. 9.19(a), compute astensks) shown
superimposed on
bwlabel(f, 8). then corresponding
connected
EXAMPLE 9.7: % This example shows how to compute and display the center of mass components.
Computing and connected component in Fig. 9.17(a). First, we use bwlabel to compute
displaying the connected components:
center of mass of
connected
components. >> f = imread('objects.tif');
>> [ L , n] = b w l a b e l ( f ) ;
>> [ r , C ] = f i n d ( L == 3 ) ;
n this section we use 8-connectivity (the default), which
Function mean with r and c as inputs then computes the center of mass o hat B in the following discussion is a 3 x 3 matrix of Is, with the cen-
object. ed at coordinates (2,2).
'," , the mask and f is the marker, the reconstruction of g from f , denoted
-:mean >> rbar = mean(r);
>> cbar = mean(c); e following iterative procedure:
If A is (1 vector.
mean ( A ) cuniputes A loop can be used to compute and display the centers of mass of a1
the nvernge vnl~reof jects in the image. To make the centers of mass visible when superimpos
its rlemetlrs. If A is n
nintrix, mean(A) the image, we display them using a white " * " marker on top of a black
(rents the coliimns of circle marker, as follows: hk+l = (hk @ B ) ng
A as vectors, retltrri-
big n row vector qf
nlerln vulires. The >> imshow(f)
.sytztflx mean ( A , >> hold on % So l a t e r plotting commands plot on top of the rker f must be a subset of g; that is,
dim) returns the >> for k = 1:n
r77eciri vrr1ue.s of the [ r , C] = find(L == k ) ; fCg
elenients along the
dinicnsion of A .spec- rbar = mean(r) ; re 9.21 illustrates the preceding iterative procedure. Note that, al-
ifi'ed by scillnr dim. cbar = mean(c); this iterative formulation is useful conceptually, much faster computa-
plot(cbar, rbar, 'Marker', ' o ' , 'MarkerEdgeColor', ' k t , . . . algorithms exist. IPT fucction imreconst r u c t uses the "fast hybrid
'MarkerFaceColor', ' k ' , 'Markersize', 1 0 ) m described in Vincent [1993]. The calling syntax for
plot(cbar, rbar, 'Marker', 'MarkerEdgeColor', ' w ' )
I * ' ,
end econstruct is
w&Morphological Reconstruction ere marker and mask are as defined at the beginning of this section.
that the vertical strokes were restored, but not the rest of the characters
the shapes and the structuring element. The method discussed in this section
openrng by reconstructron, restores exactly the shapes of the objects that re ining the strokes. Finally, we obtain the reconstruction:
main after erosion. The opening by reconstruction of f , using structuring eie
obr = i m r e c o n s t r u c t ( f e , f ) ;
ment B, is defined as R f ( f 8B). I
EXAMPLE9.8: result in Fig. 9.22(d) shows that characters containing long vertical strokes
1 A comparison between opening and opening by reconstructio restored exactly; all other characters were removed.The remaining parts
Opening by image containing text is shown in Fig. 9.22. In this example, we are in
g. 9.22 are explained in the following two sections. !#
in extracting from Fig. 9.22(a) the characters that contain long vertical
Since opening by reconstruction requires an eroded image, we perform
step first, using a thin, vertical structuring element of length proportional
the height of the characters: uction has a broad spectrum of ~racticalapplications,
he selection of the marker and mask images. For exam-
>> f = imread('book-text-bw.tifl); hoose the marker image, f,,, to be 0 everywhere except
>> f e = i m e r o d e ( f , ones(51, 1 ) ) ; here it is set to 1 - f :
.
Figure 9.22(b) shows the result. The opening, shown in Fig. 9.22(c), is cornput- Y E e " f (Y v\=
(1 - f (x,y ) if (x, y ) is on the border off
Lo
Jint-7 J /
ed using imopen: otherwise
366 Chapter 9 s Morphological Image Processing 9.6 aY Gray-Scale
Then g = [ R fc ( f , , , ) l C has the effect of filling the holes in f , as illustr the structuring element about its origin and translating it to all loca-
Fig. 9.22(e). IPT function imf ill performs this computation autom lution kernel is rotated and then translated
when the optional argument ' holes ' is used: nslated location, the rotated structuring element
g = imfill(f, 'holes')
latter, Do, a binary matrix, defines which locations in the
This function is discussed. in more detail in Section 11.1.2. d are included in the max operation. In other words, for an
pair of coordinates (xo,yo) in the domain of Db, the sum
9.5.3 Clearing Border Objects
Another useful application of reconstruction is removing objects th
11 coordinates (x', y ' ) E Db each time that
the border of an image. Again, the key task is to select the appropriate
tting b(xl, y ' ) as a function of coordinates x'
and mask images to achieve the desired effect. In this case, we use the
image as the mask, and the marker image, f,, is defined as
f (x, y ) if (x, y ) is on the border off
fm(x7 Y ) = otherwise
Figure 9.22(f) shows that the reconstruction, Rf(f,,), contains only the
touching the border.The set difference f - Rf(fm),shown in Fig. 9.22( b(xl, y ' ) = 0 for (x', y') E Ub
tains only the objects from the original image that do not touch the se, the max operation is specified completely by the pattern of 0s and
IPT function imclearborder performs this entire procedure automa ary matrix Db, and the gray-scale dilation equation simplifies to
Its syntax is
(f @ b)(x, y ) = max{f (x - x', Y - y ' ) 1 (x', Y ' ) E Db)
g = i m c l e a r b o r d e r ( f , conn) at gray-scale dilation is a local-maximum operator, where the rnaxi-
taken over a set of pixel neighbors determined by the shape of Db.
where f is the input image and g is the result. The value of conn can be
4 or 8 (the default). This function suppresses structures that are ligh
their surroundings and that are connected to the image border. Input f a second matrix specifying height values, b(xl, y ' ) . For example,
a gray-scale or binary image. The output image is a gray-scale or binary
respectively. = strel([l 1 I], [I 2 I])
9.23(c) shows the result of using imerode with the same structuring el-
used for Fig. 9.23(b):
FlGlURE 9.23
Dilation and = imerode(f, se);
ero!sion.
fob = (fGb)$b
by b, followed by the dilation of the
.
,denoted f b, is dilation followed by
f.b= (f@b)8b
e geometric interpretations. Suppose that an image
Figure 9.23(b) shows the result. As expected, the image is slightly blurre x, y) is viewed as a 3-D surface; that is, its intensity values are in-
rest of this figure is explained in the following discussion. s height values over the xy-plane. Then the opening o f f by b can
The gray-scale erosion o f f by structuring element b, denoted f 8 b, is structuring element b up against the
fined as across the entire domain o f f . The
(f9b)(x, Y ) = min{f ( x + x', y + y') - b(xf,y') I ( x ' , y') E Db) ing the highest points reached by any part of the
against the undersurface o f f .
where Db is the domain of b and f ( x , y) is assumed to be +GO outside the ne dimension. Consider the curve in
le row of an image. Figure 9.24(b)
nt in several positions, pushed up against the
element values are subtracted from the image pixel values and the mini lete opening is shown as the curve along the
is taken. . 9.24(c). Since the structuring element is too
As with dilation, gray-scale erosion is most often performed using flat s ak on the middle of the curve, that peak is re-
by the opening. In general, openings are used to remove small bright
s while leaving the overall gray levels and larger bright features rela-
(fe b ) ( x , Y ) = min{f ( x + x', y + Y') I (x', y') E Dh)
Thus, flat gray-scale erosion is a local-minimum operator, in which the aphical illustration of closing. Note that the
wn on top of the curve while being translated
370 Chapter 9 n Morphological Image Processing 9.6 Gray-Scale Morphology 371
a
C
e
FIGURE 9.24
Opening and (a) Original image
closing in one of wood dowel
dimension.
(a) Original 1-D
signal. (b) Flat
structuring
element pushed
up underneath
the signal.
(c) Opening.
structuring
element pushed
down along the
EXAMPLE 9.9: 1 Because opening suppresses bright details smaller than the structuring fasf = imclose(imopen(fasf, s e ) , s e ) ;
Mor~hological ment, and closing suppresses dark details smaller than the structuring elem
using they are used often in combination for image smoothing and noise remova
openings and
closings. this example we use irnopen and i m c l o s e to smooth the image of wood do e result, shown in Fig. 9.25(d), yielded slightly smoother results than using a
plugs shown in Fig. 0.25(a): gle open-close filter, at the expense of additional processing. iig
372 Chapter 9 iilt Morphological Image Processing 9.6 @ Gray-Scale Morphology 373
n imtophat. These two functions can be used together for contrast en- ..,$$;:.;imbot h a t
""9 .' ;-.\
ent using commands such as 5,
= strel('diskl, 3);
irnsubtract(imadd(f, irntophat(f, se)), imbothat(f , se)); fill
hniques for determining the size distribution of particles in an image EXAMPLE 9.11:
important part of the field of granulometry. Morphological techniques Granulometry.
used to measure particle size distribution indirectly; that is, without
ing explicitly and measuring every particle. For particles with regular
CI e that are lighter than the background, the basic approach is to apply
ological openings of increasing size. For each opening, the sum of all the
alues in the opening is computed; this sum sometimes is called the
area of the image. The following commands apply disk-shaped open-
h radii 0 to 35 to the image in Fig. 9.25(a):
EXAMPLE 9.10: k# Openings can be used to compensate for nonuniform background illum
Using the tophat tion. Figure 9.26(a) shows an image, f, of rice grains in which the backgro f = imread('p1ugs.jpg');
is darker towards the bottom than in the upper portion of the image. The sumpixels = zeros (1 , 3 6 ) ;
even illumination makes image thresholding (Section 10.3) d o r k = 0:35
If v is a vector, lhen
se = s t r e l ( ' d i s k l , k);
Figure 9.26(b), for example, is a thresholded version in which grains at d i f f ( v ) retzrrns a
f o = kmopen(f, se); vector, one element
of the image are well separated from the background, but grains at the sumpixels(k + 1) = sum(fo(:)); shorter than v , of dif-
are improperly extracted from the background. Opening the image can ferences between adja-
duce a reasonable estimate of the background across the image, as long as cent elements. If X is a
matrix, then d i f f ( X )
structuring element is large enough so that it does not fit entirely within lot(0:35, sumpixels), xlabel('kl), ylabel('Surface area') ret~rrnsn matrix of
rice grains. For example, the commands row differences:
ure 9.27(a) shows the resulting plot of sumpixels versus k. More interest- X ( 2 : end, : ) -
>> se = s t r e l ( ' d i s k l , 10); the reduction in surface area between successive openings: X(1: end-I, :).
>> f o = irnopen(f, se);
lot(-diff(sumpixe1s)) ;d l f f
resulted in the opened image in Fig. 9.26(c). By subtracting this image from
original image, we can produce an image of the grains with a reasonably e ylabel('Surface area reduction')
background:
374 Chapter 9 a dorphology 375
FIGURE 9.27 C
Granulometry. FIGURE 9.28 Gray-
(a) Surface area scale morpholog~cal
versus structuring reconstruction 1n
element radius. one dimens~on.
(b) Reduction in (a) Mask (top) and
surface area marker curves
versus radius. (b) Iterative
(c) Reduction in computation of- the
surface area reconstruction.
versus radius for a (c) Reconstruction
smoothed image. result (black curve).
:376 Chapter 9 &! Morphological Image Processing tlrr Summary 377
which case the previous command string is broken down into three
(1) Compute the filtered image, a b s ( i m filter(double(f) , w ) ), ily verified that choosing T = max(g ( : ) ) yields the same result as in
e flexible than using the mask in
ute the difference between the
and the next highest pixel value in a neighborhood, we would replace
EXA.MPLE 10.1: k?4 Figure 10.2(a) shows an image with a nearly invisible black point n the far right of the preceding expression by m*n - 1. Other variations
Point detection. basic theme are formulated in a similar manner.
dark gray area of the northeast quadrant. Letting f denote this image, w
the location of the point as follows:
2 Line Detection
>> w = [-1 -1 -1 ; -1 8 -1 ; -1 -1 -1 ] ;
>> g = abs(imfilter(double(f), w));
>> T = max(g(:));
hick) oriented horizontally. With a constant background,
ximum response would result when the line passed through the middle
the mask. Similarly, the second mask in Fig. 10.3 responds best to lines
third mask to vertical lines; and the fourth mask to lines
a b . Note that the preferred direction of each mask is
FlGUKli 10.2 other possible directions. The
(a) Gray-scale ing a zero response from the
image with a
nearly invisible
isolated black
point in the dark
gray area of the FIGURE 10.3 Line
northeast detector masks.
quadrant.
(b) Image
showing the
detected point.
(The point was
enlarged to make
it easier to see.)
Vertical -45"
1
Let R,, R2,R3, and R4 denote the responses of the masks in Fig. 10.3,frd a b
c d
left to right,
- where the R's are -given by the equation in the previous sectin e t
Su.ppose that the four m:~ s k are
s run divici hroi nagt:. If, at a
FIGURE 10.4
tai,n point in the image, /~ i >l JR;J, r all j .hat said to ben (a) Image of a
likelv associated with a line in the'direction of mask i. For example, if at a oai w~re-bondmask
in thk image, [ R , ( > 1 ~ ~ 1
for j = 2,3 , that oint is saici t o b e n (b) Result of
lik ely associated with a 1-iorizontal li :. Alte e may be intereste processing w ~ t h
the -45" detect01
detecting lines in a specified direction. In this case, we would use the mask a In Fig 10.3.
sociated with that direction and threshold its output, as in the equation in tf (c) Zoomed vlew
previous section. In other words, if we are interested in detecting all the lint of the top, left
in an image in the direction defined by a given mask, we simply run the ma! reglon of (b).
through the image and threshold the absolute value of the result. The pain (d) Zoomed view
that are left are the strongest responses, which, for lines one pixel thick, cori of the bottom,
r ~ g h sectlon
t of
spond closest to the direction defined by the mask. The following example (b). (e) Absolute
lustrates this procedure. value of (b).
(f) All polnts (In
EXALMPLE10.2: Figure 10.4(a) shows a digitized inary)I porti a wire -bolad mask whlte) whose
Dete:ction of lines electronic circuit. The: image size 486 :X 486 j. SUPPlose that we values sat~shed
in a 5;pecified the c o n d ~ t ~ o n
interested in finding all the lines that are one pixel thick, oriented at -45". l? g >= T, where g IS
direc:tion.
this purpose, we use the last mask in Fig. 10.3. Figures 10.4(b) through (f) we1 the image In (e)
generated using the following commands, where f is the image in Fig. 10.4(a (The polnts In (f)
were enlarged
>> w = [ 2 -1 -1 ; -1 2 -1; -1 -1 21; slightly to make
>> q = imfilter(double(f), w); them easler to
i r n ~ h o w ( ~ ,[ I ) Si Fig. 10. b) see.)
g t o p = g(1:120, 1 :120);
g t o p = pixeldup(g t o p , 4 ) ;
f i g u r e , irnshow(gt OP, [ I ) Fig.
g b o t = g(end-119: e n d , end- 9:e n
>> g b o t = pixeldup(gbot, 4);
>> f i g u r e , imshow(gbot, [ I) % Fig. 10.4(d)
g = abs(g);
f i g u r e , imshow(g,
T = max(g(:));
g = g >= T ;
>> f i g u r e , i m s h o w ( g ) % F i g . 10.4(f) a;
s
4
4
The shades darker than the gray background in Fig. 10.4(b) correspond to neg% 3
tive values. There are two main segments oriented in the -45" direction, one a&
the top, left and one at the bottom, right [Figs. 10.4(c) and (d) show zoomed seer;
tions of these two areas]. Note how much brighter the straight line segment $j
Fig .10A(d) is than the seg:ment in Fig D.4(c).The r easc is that the compor
in t he bottom, right of Fig .10.4(a) is o pixel thick, whi the on~eat the top,
is not.The mask response is stronger for the one-pixel-thick component. it,
Figure 10.4(e) shows the absolute value of Fig. 10.4(b). Since we are i n t e ~ j
ested in the strongest response, we let T equal the maximum value in this:
image. Figure 10.4(f) shows in white the points whose values satisfied
384 Chapter 10 r Image Segmentation 10.1 Point, Line, and Edge Detection
condition g >= T, where g is the image in Fig. 10.4(e).The isolated points in t econd-order derivatives in image processing are generally computed using
such a way that the mask produced a maximum response at those iso
cations. These isolated points can be detected using the mask in Fig.
then deleted, or they could be deleted using morphological operators, a
cussed in the last chapter. lacian is seldom used by itself for edge detection because, as a second-
10,1.3 Edge Detection Using Function edge
Although point and line detection certainly are important in any this section, the Laplacian can be a powerful complement when used in
ation with other edge-detection techniques. For example, although its
A fundamental property of the gradient vector is that it points in the di obel edge detector uses the masks in Fig. 10.5(b) to approximate digital-
tion of the maximum rate of change o f f at coordinates ( x , y). The angl e first derivatives G, and G., In other words, the gradient at the center
which this maximum rate of change occurs is t i n a neighborhood is computed as follows by the Sobel detector:
One of the key issues is how to estimate the derivatives G, and G, digitally.
various approaches used by function edge are discussed later in this section. + [ ( ~ 3f 226 f z g ) - (21 + 224 +~7)]~}~"
386 Chapter 10 s Image Segmentation 10.1 r Point, Line, and Edge Detection 387
TABLE 10.1 a
Edge detectors b
available in c'
function edge. d
FIGURE 10.5
detector
Some edge
masks
Image neighborhood
Zero crossings
Canny
Roberts
Prewitt edge detector uses the masks in Fig. 10.5(c) to approximate digi-
the first derivatives G, and G,. Its general calling syntax is
v2h(r) = - [r2
- Iu']e-$ ntax for the Canny edge detector is
a b
c d
e f
FIGURE 10.6
(a) Original
image. (b) Result
of function edge
using a vertical
Sobel mask with
the threshold
determined
automatically.
(c) Result using a
specified
threshold.
(d) Result of
determining both
vertlcal and
hor~zontaledges The value of T was
wlth a specifled chosen experinten-
threshold. tally to show res~rlts
(e) Result of conl~arablewith
computing edges ~ i ~ sIO(C)
. ' and
lO(d).
at 45' with
imf l l t e r using a
speclfied mask
and a speclfied
threshold. (f)
Result of
computing edges
at -45" with
lrnf l l t e r using a
specdied mask EXAMPLE 10.4:
and a specified Cornparison of
threshold. the Sobel, LOG,
and Canny edge
detectors.
. . .
space associated with it, and this line interskcti the line associated with
) at (a', b ' ) , where a' is the slope and b' the intercept of the line con-
g both ( x i ,y;) and ( x i , yj) in the xy-plane. In fact, all points contained on
line have lines in parameter space that intersect at (a', b ' ) . Figure 10.8 il-
trates these concepts.
394 Chapter 10 1 Image Segmentation 10.2 k% Line Detection Using the HouglITransform 3'95
= [ O 0 0 5
0 2 0 0
1 3 0 0
0 0 4 01;
S = s p a r s e ( r , c , s , rn, n )
ter TO rs:I Image Segmentation 10.2 % Line Detection Using the Hough Transform 397
Here,r and c are vectors of row and column indices,respectively,of the ((M - 1)A2 + (N - llA2);
ro elements of the matrix w e wish to convert to sparse format.Param
a vector containing the values that correspond to the index pairs (r, c
and n are the row and column dimensions for the resulting matrix. linspace(-q*drho, q*drho, nrho);
stance,the matrix S in the previous example can be generated directl = find (f) ;
the command -val]
I; y = y - I;
hough
matrix = x-matrix.*cos(theta-matrix) + . . .
$fa!#
function [h, theta, rho] = hough(f, dtheta, drho) y-matrix.*sin(theta_matrix);
%HOUGH Hough transform. = (nrho - l)/(rho(end) - rho(1));
% [H, THETA, RHO1 = HOUGH(F, DTHETA, DRHO) computes the Hough in-index = round(slope*(rho-matrix - rho(1)) + 1);
% transform of the image F. DTHETA specifies the spacing (in
% degrees) of the Hough transform bins along the theta axis. DRH bin-index = repmat(l:ntheta, size(x-~natrix, I), 1);
% specifies the spacing of the Hough transform bins along the rh ke advantage of the fact that the SPARSE function, which
% axis. H is the Hough transform matrix. It is NRHO-by-NTHETA, nstructs a sparse matrix, accumulates values when input
% where NRHO = 2*ceil(norm(size(F))/DRHO) - 1, and NTHETA = dices are repeated. That's the behavior we want for the
% 2*ceil(SO/DTHETA). Note that if SOIDTHETA is not an integer, ugh transform. We want the output to be a full (nonsparse)
% actual angle spacing will be 90 1 ceil(SO/DTHETA). trix, however, so we call function FULL on the output of
0
,
. , . ..
398 Chapter 10 Tran sform
-I
FIGURE 10.10
(a) Binary image
with five dots
(four of the dots
are in the
corners).
(b) Hough
transform
displayed using
imshow.
(c) Alternative
Hough transform
display with axis
labeling. (The
dots in (a) were
enlarged to make
them easier to
see.)
ghpeak
two-element v e c t o r s p e c i f y i n g t h e s i z e o f t h e suppression
neighborhood. T h i s i s t h e neighborhood around each peak t h a t i s
Chapter Image Segmentation 10.2 Line Detection Using the Hough Transform 401
% set to zero after the peak is identified. The elements of NHOo done = length(r) == numpeaks;
% must be positive, odd integers. R and C are the row and column
% coordinates of the identified peaks. HNEW is the Hough transfo
% with peak neighborhood suppressed.
%
% If NHOOD is omitted, it defaults to the smallest odd values >=
% size(H)/50. If THRESHOLD is omitted, it defaults to on houghpeaks is illustrated in Example 10.6.
% 0.5*max(H( : ) ) . If NUMPEAKS is omitted, it defaults to 1 .
if nargin < 4 Hough Transform Line Detection and Linking
nhood = size(h)/50; a set of candidate peaks has been identified in the Hough transform,it
% Make sure the neighborhood size is odd. s to be determined if there are line segments associated with those
nhood = max(2*ceil(nhood/2) + 1, 1); as well as where they start and end. For each peak, the first step is to
end
if nargin < 3 location of all nonzero pixels in the image that contributed to that
threshold = 0.5 * max(h(:)); r this purpose,w e write function houghpixels.
end
tion [r, c] = houghpixels(f, theta, rho, rbin, cbin) houghpixels
if nargin < 2 ~-------"--
numpeaks = 1 ; GHPIXELS Compute image pixels belonging to Hough transform bin.
end [R, C] = HOUGHPIXELS(F, THETA, RHO, REIN, CEIN) computes the
row-column indices (R, C) for nonzero pixels in image F that map
done = false; to a particular Hough transform bin, (RBIN, CBIN). REIN and CBIN
hnew = h; r = [I; c = [I; are scalars indicating the row-column bin location in the Hough
while -done transform matrix returned by function HOUGH. THETA and RHO are
[p, q] = find(hnew == max(hnew(:))); the second and third output arguments from the HOUGH function.
P = P(l); q = q(l); y, val] = find(f);
if hnew(p, q) >= threshold x-1; y = y - 1 ;
r(end + 1) = p; c(end + 1) = q;
ta-c = theta(cbin) * pi / 180;
% Suppress this maximum and its close neighbors. xy = x*cos(theta-c) + y*sin(theta-c);
pl = p - (nhood(1) - 1)/2; p2 = p + (nhood(1) - 1)/2; o = length(rh0);
ql = q - (nhood(2) - 1)/2; q2 = q + (nhood(2) - 1)/2; pe = (nrho - l)l(rho(end) - rho(1));
[PP, qql = ndgrid(pl:p2, ql:q2); bin-index = round(slope*(rho-xy - rho(1)) + 1) ;
PP = pp(:); qq = qq(:);
= find(rho-bin-index == rbin);
% Throw away neighbor coordinates that are out of bounds in
% the rho direction.
x(idx) + 1 ; c = y(idx) + 1 ;
badrho = find((pp < 1) I (pp > size(h, 1))); pixels associated with the locations found using houghpixels must be
pp(badrho) = [I; qq(badrho) = [ I ; uped into line segments.Function houghlines uses the following strategy:
% For coordinates that are out of bounds in the theta Rotate the pixel locations by 90" - 0 so that they lie approximately along
% direction, we want to consider that H is antisymmetric
% along the rho axis for theta = +/- 90 degrees.
theta-too-low = find(qq < 1); Sort the pixel locations by their rotated x-values.
qq(theta-too-low) = size(h, 2) + qq(theta-too-low); Use function diff to locate gaps.Ignore small gaps;this has the effect of
pp(theta-too-low) = size(h, 1) - pp(theta-too-low) + 1 ; merging adjacent line segments that are separated by a small space.
theta-too-high = find(qq > size(h, 2)); Return information about line segments that are longer than some mini-
qq(theta-too-high) = qq(theta-too-high) - size(h, 2); m u m length threshold.
pp(theta-too-high) = size(h, 1) - pp(theta-too-high) + 1 ;
Ction lines = houghlines(f,theta,rho,rr,cc,fillgap,minlength) houghlines
.- ,
% Convert to linear indices to zero out all the values. UGHLINES Extract line segments based on the Hough transform.
hnew(sub2ind(size(hnew), pp, qq)) = 0; LINES = HOUGHLINES(F, THETA, RHO, RR, CC, FILLGAP, MINLENGTH)
402 Chapter Image Segmentation 10.2 @Line
i Detection Using the HouglI Transform 403
% extracts line segments in the image F associated with particul % Rotate the end-point locations back to the original
% bins in a Hough transform. THETA and RHO are vectors returned
% function HOUGH. Vectors RR and CC specify the rows and columns Tinv = inv(T) ;
% of the Hough transform bins to use in searching for line point1 = point1 * Tinv; point2 = point2 * Tinv;
% segments. If HOUGHLINES finds two line segments associated wit numlines = numlines + 1 ; B = inv(A) con!-
% the same Hough transform bin that are separated by less than lines(numlines).pointl = pointl + 1 ; plites ihe inverse of
% FILLGAP pixels, HOUGHLINES merges them into a single line lines(numlines).point2 = point2 + 1; square 111flrri.~
A.
% segment. FILLGAP defaults to 20 if omitted. Merged line lines(numlines).length = linelength;
% segments less than MINLENGTH pixels long are discarded. lines(numlines).theta = theta(cbin);
% MINLENGTH defaults to 40 if omitted. lines(numlines).rho = rho(rbin);
-
%
% LINES is a structure array whose length equals the number of
% merged line segments found. Each element of the structure arra -
% has these fields:
%
% point1 End-point of the line segment; two-element vector n this example w e use functions hough, houghpeaks,and houghlines to EXAMPLE 10.6:
a set of line segments in the binary image,f,in Fig. 10.7(f). First,w e com- Using the Hough
% point2 End-point of the line segment; two-element vector transform for line
% length Distance between point1 and point2 e and display the Hough transform,using a finer angular spacing than the
detection and
% theta Angle (in degrees) of the Hough transform bin ault (be = 0.5 instead of 1.0). linking.
% rho Rho-axis position of the Hough transform bin
if nargin < 6 [H, theta, rho] = hough(f, 0.5);
fillgap = 20; imshow(theta, rho, H, [ I, 'notruesize'), axis on, axis normal
end xlabel( ' \theta') , ylabel( ' \rho1)
if nargin < 7
minlength = 40; t w e use function houghpeaks to find five Hough transform peaks that are
end ely to be significant.
numlines = 0; lines = struct;
for k = l:length(rr) [r, c] = houghpeaks(H, 5);
rbin = rr(k); cbin = cc(k);
> plot(theta(c), rho(r), 'linestyle', 'none', ...
% Get all pixels associated with Hough transform cell. 'marker', ' s ' , 'color', ' w ' )
[r, c] = houghpixels(f, theta, rho, rbin, cbin);
if isempty (r) re 10.11(a) shows the Hough transform with the peak locations superim-
continue
end d.Finally,w e use function houghlines to find and link line segments,and
% Rotate the pixel locations about (1,l) so that they lie a b
% approximately along a vertical line.
omega = (90 - theta(cbin)) * pi / 180; FIGURE 10.1 1
(a) Hough
T = [cos(omega) sin(omega); -sin(omega) cos(omega)];
transform with
xy= [r-1 c-11 *T; five peak
x = sort(xy(:,l)); locations selected.
% Find the gaps larger than the threshold. (b) Line segments
diff-x = [diff(x); Inf]; corresponding to
idx = [O; find(diff_x > fillgap)]; the Hough
for p = l:length(idx) - 1 transform peaks.
xl = x(idx(p) + 1); x2 = x(idx(p + 1));
linelength = x2 - X I ;
if linelength >= minlength
pointl = 1x1 rho(rbin)]; point2 = [x2 rho(rbin)];
404. Chapter Image Segmentation 10.3 % 411
we superimpose the line segments on the original binary image using i m s for choosing a global threshold are discussed in Section 10.3.1. In
hold on, and p l o t : 10.3.2 we discuss allowing the threshold to vary, which is called local
a
28 = ~o(/-'o - w T ) ~+ u l ( ~ / PT)~
-'
406 Chapter 10 :# Image Segmentation 10.4 B Region-Based SE
where
xt we compute a threshold using function g r a y t h r e s h :
wo = x
k-I
q=o
pq(rq) = graythresh(f)
w, = x
L- l
q=k
p,(rq)
k-1
Po = c. 4pq(rq)/w0
q=O
L-l
P1 = 2 qPq(rJ/w~
q=k
sholding using these two values produces images that are almost indistinguish-
L-1 from each other. Figure 10.13(b) shows the image thresholded using T2.
PT = 2q ~ q ( ~ q )
9=0 .2 Local Thresholding
bal thresholding methods can fail when the background illumination is un-
Function g r a y t h r e s h takes an image, computes its histogram, and then fi
was illustrated in Figs. 9.26(a) and (b). A common practice in such sit-
the threshold value that maximizes a;. The threshold is returned as a nor
is to preprocess the image to compensate for the illumination
ized value between 0.0 and 1.0.The calling syntax for g r a y t h r e s h is
ems and then apply a global threshold to the preprocessed image. The
T = graythresh(f) roved thresholding result shown in Fig. 9.26(e) was computed by applying
rphological top-hat operator and then using g r a y t h r e s h o n the result.
where f is the input image and T is the resulting threshold. To segmen an show that this process is equivalent to thresholding f ( x , y ) with a lo-
image we use T in function im2bw introduced in Section 2.7.2. Becaus varying threshold function T ( x ,y):
threshold is normalized to the range [0, 11, it must be scaled to the p 1 i f f ( x ,Y ) 2 T ( x ,Y )
range before it is used. For example, if f is of class u i n t 8 , we multiply T b
before using it. { 0 i f f ( x ,y ) < T ( x ,Y )
EXAMPLE 10.7: In this example we illustrate the iterative procedure described previou
Computing global as well as Otsu's method on the gray-scale image, f , of scanned text, show T ( x 7Y ) = f ~ ( xY, ) + TO
thresholds. Fig. 10.13(a).The iterative method can be implemented as follows: image ,f,(x, y) is the morphological opening of f , and the constant To is
result of function g r a y t h r e s h applied to f,.
>> T = 0 . 5 * ( d o u b l e ( m i n ( f ( : ) ) ) + d o u b l e ( m a x ( f ( : ) ) ) ) ;
>> done = f a l s e ; Region-Based Segmentation
>> w h i l e -done
g = f >= T ; objective of segmentation is to partition an image into regions. In
Tnext = 0 . 5 * ( m e a n ( f ( g ) ) + m e a n ( f ( - g ) ) ) ; tions 10.1 and 10.2 we approached this problem by finding boundaries be-
done = a b s ( T - T n e x t ) < 0 . 5 ; en regions based on discontinuities in intensity levels, whereas in
T = Tnext; tion 10.3 segmentation was accomplished via thresholds based on the dis-
end ution of pixel properties, such as intensity values. In this section we discuss
For this particular image, the w h i l e loop executes four times and terminal gmentation techniques that are based on finding the regions directly.
.4.1 Basic Formulation
t R represent the entire image region. We may view szgmentation as a
FIGURE 10.13 itions R into n subregions, R , . R 2 , .. . . R,,. such that
(a) Scanned text.
(b)Thresholded
text obtained
using function
graythresh. cted region. i = 1. 2.. . . . 17.
408 Chapter 10 Image Segmentation 10.4 28 Region-Based SE
(c) Rin R, = 0 for all i and j, i # j. f size, likeness between a candidate pixel and the pixels
(d) P(Ri) = TRUE for i = 1 , 2 , . .., n. omparison of the intensity of a candidate and the av-
117 rile contexr of the
( e ) P(Ri U R,) = FALSE for any adjacent regions Ri and R,. region), and the shape of the region being grown.
disoi~ssionirt Secrio~~
9.4, rwo disjoinr re-
descriptors is based on the assumption that a model
Here, P(Ri) is a logical predicate defined over the points in set Ri and at least partially available.
gions, R, and R,, are null set.
said to be adjacent if rinciples of how region segmentation can be handled in
their.~~rzion for17u a Condition (a) indicates that the segmentation must be compl
corinected AB, we develop next an M-function, called r e g i o n g r o w , to do basic re-
every pixel must be in a region. The second condition requires that point
component. region be connected in some predefined sense (e.g., 4- or &connected). C owing. The syntax for this function is
tion (c) indicates that the regions must be disjoint. Condition (d) deals
the properties that must be satisfied by the pixels in a segmented region [ g , NR, S I , T I ] = r e g i o n g r o w ( f , S, T )
example P(Ri) = TRUE if all pixels in Ri have the same gray level. Fi
condition (e) indicates that adjacent regions Ri and Rj are different in to be segmented and parameter S can be an array (the
sense of predicate P. calar. If S is an array, it must contain Is at all the coordi-
ts are located and 0s elsewhere. Such an array can be de-
16.4.2 Region Growing by inspection, or by an external seed-finding function. If S is a scalar,
an intensity value such that all the points in f with that value become
As its name implies, region growing is a procedure that groups pixels or
T can be an array (the same size as f ) or a scalar. If T is
gions into larger regions based on predefined criteria for growth.
proach is to start with a set of "seed" points and from these grow hreshold value for each location in f. If T is scalar, it de-
global threshold.The threshold value(s) is (are) used to test if a pixel in
appending to each seed those neighboring pixels that have predefined pro
ties similar to the seed (such as specific ranges of gray level or color). age is sufficiently similar to the seed or seeds to which it is 8-connected.
Selecting a set of one or more starting points often can be based on the a and T = b, and we are comparing intensities, then a
ture of the problem, as shown later in Example 10.8. When a priori infor (in the sense of passing the threshold test) if the
nce between its intensity and a is less than or
tion is not available, one procedure is to compute at every pixel the
1 to b. If, in addition, the pixel in question is 8-connected to one or more
properties that ultimately will be used to assign pixels to regions during
values, then the pixel is considered a member of one or more regions.
growing process. If the result of these computations shows clusters of val
mments hold if S and T are arrays, the basic difference being that
the pixels whose properties place them near the centroid of these clusters
be used as seeds. with the appropriate locations defined in S and corre-
The selection of similarity criteria depends not only on the problem un
output, g is the segmented image, with the members of each region
consideration, but also on the type of image data available. For example
eled with an integer value. Parameter NR is the number of different
analysis of land-use satellite imagery depends heavily on the use of color.
problem would be significantly more difficult, or even impossible, to ha s an image containing the seed points, and parameter T I
without the inherent information available in color images. When the pixels that passed the threshold test before they
are monochrome, region analysis must be carried out with a set of desc ity. Both S I and T I are of the same size as f.
g i o n g r o w is as follows. Note the use of Chapter 9
based on intensity levels (such as moments or texture) and spatial prop
uce to 1 the number of connected seed points in each
We discuss descriptors useful for region characterization in Chapter 11.
n array) and function i m r e c o n s t r u c t to find pixels
Descriptors alone can yield misleading results if connectivi
information is not used in the region-growing process. For examp nected to each seed.
random arrangement of pixels with only three distinct intensity val regiongrow
c t i o n [g, NR, S I , T I ] = r e g i o n g r o w ( f , S, T )
ing pixels with the same intensity level to form a "region" without GIONGROW Perform segmentation by r e g i o n growing. b~--------
tention to connectivity would yield a segmentation result that is m [G, NR, S I , T I ] = REGIONGROW(F, SR, T ) . S can be an a r r a y ( t h e
in the context of this discussion. same s i z e as F) w i t h a 1 a t t h e c o o r d i n a t e s o f every seed p o i n t
Another problem in region growing is the formulation of a st and 0s elsewhere. S can a l s o be a s i n g l e seed v a l u e . S i m i l a r l y ,
Basically, growing a region should stop when no more pixels satis T can be an a r r a y ( t h e same s i z e as F ) c o n t a i n i n g a t h r e s h o l d
for inclusion in that region. Criteria such as intensity values, text value f o r each p i x e l i n F. T can a l s o be a s c a l a r , i n which
are local in nature and do not take into account the "history" of re case i t becomes a g l o b a l t h r e s h o l d .
Additional criteria that increase the power of a region-growing
410 Chapter 10 Y Image Segmentation
FIGURE 10.15 as splitting. Satisfying the constraints of Section 10.4.1 requires merging only
Histogram of combined pixels satisfy the predicate P.That is, two adja-
Fig. lO.l4(a)
re merged only if P(Ri U Rk)= TRUE.
sion may be summarized by the following procedure
f is the input image and S is a sparse matrix containing the quadtree Other fowls of
,
f S ( k m) is nonzero, then ( k , m) is the upper-left corner of a block in qtdecornp are dis
position and the size of the block is S( k , m). Function s p l i t - t e s t clsssed in
Section 11.2.2.
erge below for an example) is used to determine whether
t or not, and parameters are any additional parameters
s) required by s p l i t - t e s t . T h e mechanics of this are sim-
r to those discussed in Section 3.4.2 for function c o l t f i l t .
To get the actual quadregion pixel values in a quadtree decomposition we
function q t g e t b l k , with syntax
with identical properties. This drawback can be remedied by allowing mergi
[ v a l s , r , c ] = q t g e t b l k ( f , S, m)
a b y containing the values of the blocks of size m x m in the
FIGURE 10.1 6 dtree decomposition of f, and S is the sparse matrix returned by
(a) Partitioned ecomp. Parameters r and c are vectors containing the row and column co-
image left corners of the blocks.
(b) Corresponding
quadtree. We illustrate the use of function qtdecomp by writing a basic split-and-
M-function that uses the simplification discussed earlier, in which two
s are merged if each satisfies the predicate individually. The function,
we call s p l i t m e r g e , has the following calling syntax:
g = s p l i t m e r g e ( f , mindim, @ p r e d i c a t e )
414 Chapter ! Image Segmentation 10.4 8 Region-Based Segmentation
where f is the input image and g is the output image in which each con FLAG = PREDICATE(REG1ON) which must return TRUE if the pixels
region is labeled with a different integer. Parameter mindim defines the i n REGION satisfy the predicate defined by the code i n the
the smallest block allowed in the decomposition; this parameter has t function; otherwise, the value of FLAG must be FALSE.
positive integer power of 2.
Function p r e d i c a t e is a user-defined function that must be included he following simple example of function PREDICATE i s used i n
MATLAB path. Its syntax is xample 10.9 of the book. I t sets FLAG t o TRUE i f the
tensities of the pixels i n REGION have a standard deviation
hat exceeds 10, and their mean intensity i s between 0 and 125.
f l a g = predicate(region) therwise FLAG i s set to false.
This function must be written so that it returns t r u e (a logical 1) if function flag = predicate(region)
in region satisfy the predicate defined by the code in the function; sd = std2(region);
the value o f f l a g must be f a l s e (a logical 0). Example 10.9 illustra m = mean2(region);
of this function. flag = (sd 7 10) & ( m > 0 ) & (m < 125);
Function s p l i t m e r g e has a simple structure. First, the image is par
using function qtdecomp. Function s p l i t - t e s t uses p r e d i c a t e to d image w i t h zeros t o guarantee that function qtdecomp w i l l
lit regions down t o size 1-by-1 .
if a region should be split or not. Because when a region is split into
2*nextpow2(rnax(size(f)));
not known which (if any) of the resulting four regions will pass the pre
test individually, it is necessary to examine the regions after the fact adarray(f, [ Q - M, Q - Nl, ' p o s t ' ) ;
which regions in the partitioned image pass the test. Function predic
used for this purpose also. Any quadregion that passes the test is filled rform s p l i t t i n g f i r s t .
Any that does not is filled with 0s. A marker array is created by select qtdecornp(f, @split-test, mindim, f u n ) ;
element of each region that is filled with Is. This array is used in conju w merge by looking a t each quadregion and setting a l l i t s
with the partitioned image to determine region connectivity (adjacency); ements to 1 i f the block s a t i s f i e s the predicate.
tion imreconstruct is used for this purpose.
The code for function splitmerge follows. The simple predicate fun t the size of the largest block. Use f u l l because S is sparse.
shown in the comments section of the code is used in Example 10.9. Note = full(max(S(:)));
t the output image i n i t i a l l y t o a l l zeros. The MARKER array i s
the size of the input image is brought up to a square whose dimensions a ed l a t e r t o establish connectivity.
minimum integer power of 2 that encompasses the image. This is a re zeros(size(f));
ment of function qtdecomp to guarantee that splits down to size 1are po KER = z e r o s ( s i z e ( f ) ) ;
egin the merging stage.
function g = splitmerge(f, mindim, fun)
%SPLITMERGE Segment an image using a split-and-merge algorithm. [vals, r , c] = qtgetblk(f, S, K ) ;
% G = SPLITMERGE(F, MINDIM, @PREDICATE)segments image F by using
% split-and-merge approach based on quadtree decomposition. MIND1 % Check the predicate for each of the regions
% (a positive integer power of 2 ) specifies the m i n i m u m dimension % of size K-by-K w i t h coordinates given by vectors
% of the quadtree regions (subimages) allowed. If necessary, the % r and c.
% program pads the input image w i t h zeros t o the nearest square for I = l : l e n g t h ( r )
% size that i s an integer power of 2. This guarantees that the xlow = r ( 1 ) ; ylow = c ( 1 ) ;
% algorithm used i n the quadtree decomposition w i l l be able to x h i g h = xlow + K - 1 ; yhigh = ylow + K - 1 ;
% s p l i t the image down t o blocks of size 1-by-I. The result i s region = f(xlow:xhigh, y1ow:yhigh);
% cropped back to the original size of the i n p u t image. In the flag = feval(fun, region) ;
% output, G , each connected region i s labeled w i t h a different
% integer. g(xlow:xhigh, y1ow:yhigh) = 1 ;
feval(fun,
% MARKER(xlow, ylow) = 1; param) evcr[lccrtes
% Note that i n the function c a l l we use @PREDICATEfor the value function f u n with
% f u n . PREDICATE i s a function i n the MATLAB path, provided by th pnrn~nrterparam.
% user. I t s syntax i s Srr rlrr help pngr for
f e v a l fur othersyn-
-6
tax forn7s ~rpplicab[e
ro thisfic~~crio~z.
416 Chapter 10 s Image Segmentation 10.5 a Segmentation Using the Watershed Transform 417
FIGURE 10.1 8
(a) Gray-scale
image of dark blot FIGURE 10.20
(b) Image viewed (a) B~naryimage.
a surface,with (b) Complement
labeled watershed of Image in (a).
ndge line and (c) D~stance
catchment basins. transform.
(d) Watershed
ridge lines of the
Understanding the watershed tr-ansform negatlve of the
scale image as a topological surface:,where distance
ed as heights. We can, for example, visualize transform.
as the three-dimensional surface in Fig. - 10.1 (e) Watershed
ridge llnes
this surface, it is clear that water would collect in the two areas labele
catchment basins. Rain falling exactlv on the labeled watershed r i d ~ e superimposed in
.
2
black over
would b e equally likely t o collect im either original binary
watershed transform finds the catcl~ m e n b~t image. Some
image. In terms of solving image selgmentati oversegmentation
change the starting image into anot her imaf is evident.
objects or regions we want to identjify.
Methods for computing the watershed transform are discussed in det
Gonzalez and Woods [2002] and in Soille [2003]. In particular, the algor
used in IPT is adapted from Vincent and Soille [1991].
D = bwdist ( f )
In this example we show how the distance transform can be used with IPT's EXAMPLE 10.10:
tershed transform to segment circular blobs, some of which are touching Segmenting a
ach other. Specifically, we want to segment the preprocessed dowel image, f, binary image
a b using the distance
n in Figure 9.29(b). First, we convert the image to binary using im2bw and and watershed
FIGURE 10.19 thresh, as described in Section 10.3.1. transforms.
(a) Small binary
image.
(b) Distance
transform.
ure 10.20(a) shows the result. The next steps are to complement the image,
ompute its distance transform, and then compute the watershed transform of
Chapter d Transform 421
FIGURE 10.21
(a) Gray-scale
image of small
blobs. (b) Gradient
magnitude image.
(c) Watershed
transform of (b),
showing severe
oversegmentation.
(d) Watershed
transform of the
smoothed gradient
image; some
oversegmentation
is still evident.
(Original image
courtesy of Dr. S.
Beucher,
CMMIEcole de
Mines de Paris.)
.. -- --. -.-- .- ..
EXAMPLE 10.12:
Illustration of
marker-controlled sults obtained from computing the watershed transform of the gradient
watershed
segmentation. without any other processing.
>> h = fspecial('sobelt);
>> f d = double(f);
>> g = sqrt(imfilter(fd, h, 'replicate') . ^ 2 + ...
imfilter(fd, h ' , 'replicate') . * 2);
>> L = watershed(g);
>> w r = L == 0;
rm = imregionalmin(f)
>> rm = imregionalmin(g);
Most of the regional minima locations shown in Fig. 10.22(c) are very sh
low and represent detail that is irrelevant to our segmentation problem
eliminate these extraneous minima we use IPT function imextended
424 Chapter 10 Image Segmentation Summary 425
which computes the set of "low spots" in the image that are deeper (by 2 = imimposemin(g, im I em);
tain height threshold) than their immediate surroundings. (See Soill
for a detailed explanation of the extended minima transform and relat e 10.22(f) shows the result. We are finally ready to compute the water-
ations.) The calling syntax for this function is transform of the marker-modified gradient image and look at the result-
atershed ridgelines:
i m = imextendedmin(f, h)
2 = watershed(g2);
where f is a gray-scale image, h is the height threshold, and i m is a binary
whose foreground pixels mark the locations of the deep regional minima. 2(L2 == 0) = 255;
we use function imextendedmin to obtain our set of internal markers:
ast two lines superimpose the watershed ridge lines on the original image.
esult, a much-improved segmentation, is shown in Fig. 10.22(g). @
>> im = imextendedmin(f, 2);
>> f i m = f;
>> fim(im) = 175; arker selection can range from the simple procedures just described to
derably more complex methods involving size, shape, location, relative
The last two lines superimpose the extended minima locations as gray ces, texture content, and so on (see Chapter 11regarding descriptors).
point is that using markers brings a priori knowledge to bear on the seg-
on the original image, as shown in Fig. 10.22(d). We see that the resulting
do a reasonably good job of "marking" the objects we want to segment. tation problem. Humans often aid segmentation and higher-level tasks in
Next we must find external markers, or pixels that we are confident b ryday vision by using a priori knowledge, one of the most familiar being the
of context. Thus, the fact that segmentation by watersheds offers a frame-
to the background. The approach we follow here is to mark the backgrou that can make effective use of this type of knowledge is a significant ad-
finding pixels that are exactly midway between the internal markers. Su
ingly, we do this by solving another watershed problem; specifically, we
pute the watershed transform of the distance transform of the internal m
image, im:
eliminary step in most automatic pictorial pat-
>> Lim = watershed(bwdist(im)); tion and scene analysis problems. As indicated by the range of examples
>> em = Lim == 0; this chapter,the choice of one segmentation technique over another is dic-
by the particular characteristics of the problem being considered. The
Figure 10.22(e) shows the resulting watershed ridge lines in the binary ough far from exhaustive, are representative of
em. Since these ridgelines are midway in between the dark blobs marked
they should serve well as our external markers.
Given both internal and external markers, we use them now to mo
gradient image using a procedure called minima imposition. The min
position technique (see Soille [2003] for details) modifies a gray-scale Im
so that regional minima occur only in marked locations. Other pixel values
"pushed up" as necessary to remove all other regional minima. IPT functi
imimposemin implements this technique. Its calling syntax is
m p = imimposemin(f, mask)
>> D = cellfun('length', C)
D = [Ax2 double] [I] [512x1 double] [1x512 double]
512 188 2
: .
430 Chapter 11 a Representation and Description 11.1 Background 431
>> v = G(1)
or
>> v = H(1,l)
te that s itself is a scalar, with four fields associated with it in this case.
where v is a 1 X 2 vector. Note that we did not use the familiar command see in this example that the logic of the code is the same as before, but
N ] = G(1) to obtain the size of the image. This would cause an error beca nization of the output data is much clearer. As in the case of cell ar-
only functions can produce multiple outputs. To obtain M and N we would advantage of using structures would become even more evident if we
M = ~ ( 1 andN
) = v(2). a larger number of outputs. lsll
As with cell arrays, when a value is assigned to a structure field, MATL e 2-D coordinates of regions or boundaries are organized in this chapter
makes a copy of that value in the structure. If the original value is changed form of np X 2 arrays, where each row is an (x, y) coordinate pair, and
later time, the change is not reflected in the structure. he number of points in the region or boundary. In some cases it is neces-
to sort these arrays. Function sortrows can be used for this purpose:
11.1.2 Some Additional MATLAB and IPT Functions Used in Th
Chapter z = sortrows(S) ,,&%;rows
-Sort '
Function imf ill was mentioned briefly in Table 9.3 and in Section 9.5.2. \ LI:A , <L,
function performs differently for binary and intensity image inputs, so, to unction sorts the rows of S in ascending order. Argument S must be either
clarify the notation in this section, we let f B and f I represent binary and x or a column vector. In this chapter, s o r t rows is used only with np X 2
tensity images, respectively. If the output is a binary image, we denote it by . If several rows have identical first coordinates, they are sorted in ascend-
otherwise we denote simply as g.The syntax er of the second coordinate. If we want to sort the rows of S and also
te duplicate rows, we use function unique, which has the syntax
gB = i m f i l l ( f B , l o c a t i o n s , conn) h$t::,.
ground pixels to 1)of the input binary image f B, starting from the p e z is the sorted array with no duplicate rows, and m and n are such that
ified in 1ocations.This parameter can be an n X 1vector (n is the (m, : ) a n d S = z ( n , :).Forexample,ifS=[l 2; 6 5; 1 2 ; 4 31,then
locations), in which case it contains the linear indices (see Section 1 2 ; 4 3 ; 6 5 ] , m = [ 3 ; 4; 2 ] , a n d n = [ l ; 3 ; 1 ; 2 l . N o t e t h a t z i s
starting coordinate locations. Parameter l o c a t i o n s can also be an n X 2 anged in ascending order and that m indicates which rows of the original
trix, in which each row contains the 2-D coordinates of one of the starting
cations in fB. Parameter conn specifies the connectivity to be used on quently, it is necessary to shift the rows of an array up, down, or sideways
background pixels: 4 (the default), or 8. If both l o c a t i o n and conn are o ified number of positions. For this we use function c i r c s h i f t :
ted from the input argument, the command gB = imf ill ( f B ) displays th
nary image, fB, on the screen and lets the user select the starting locatio
using the mouse. Click the left mouse button to add points. Press BackSpa
z = c i r c s h i f t ( S , [ u d 11-1)
A%@~\,
. 5 $ ~ : .hyi f@
.kw,{;,fy.'
t ~~~.,
Delete to remove the previously selected point. A shift-click, right-clic ud is the number of elements by which S is shifted up or down. If ud is
double-click selects a final point and then starts the fill operation. Press1 e, the shift is down; otherwise it is up. Similarly,if l r is positive, the array
Return finishes the selection without adding a point. ed to the right l r elements; otherwise it is shifted to the left. If only up
Using the syntax down shifting is needed, we can use a simpler syntax
fills holes in the input binary image. A hole is a set of background pixels t is an image, c i r c s h i f t is really nothing more than the familiar scrolling
cannot be reached by filling the background from the edge of the image. and down) or panning (right and left), with the image wrapping around.
before, conn specifies connectivity: 4 (the default) or 8.
The syntax
3 Some Basic Utility M-Functions
g = i m f i l l ( f 1 , conn, ' h o l e s ' ) s such as converting between regions and boundaries, ordering boundary
ts in a contiguous chain of coordinates, and subsampling a boundary to
fills holes in an input intensity image, f I. In this case, a hole is an area of its representation and description are typical of the processes that are
pixels surrounded by lighter pixels. Parameter conn is as before. d routinely in this chapter. The following utility M-functions are used
See Section 5.2.2 for Function f i n d can be used in conjunction with bwlabel to return vecto ese purposes. To avoid a loss of focus on the main topic of this chapter,
discrLssiono f b l n c - coordinates for the pixels that make up a specific object. For example, if [ iscuss only the syntax of these functions. The documented code for each
lion f i n d and
Sec,io,z 9,4forcrdis- num] = bwlabel ( f 8) yields more than one connected region (i.e., num > l ) , w MATLAB function is included in Appendix C. As noted earlier, bound-
cussion of'bwlabel. obtain the coordinates of, say, the second region using are represented as np X 2 arrays in which each row represents a 2-D pair
ordinates. Many of these functions automatically convert 2 X n p coordi-
[r, C] = f i n d ( g == 2 ) arrays to arrays of size np X 2.
434 Chapter 11 u Representation and Description 11.1 Background 435
Function ction inserts new boundary pixels wherever there is a diagonal con-
thus producing an output boundary in which pixels are only 4-
cted. Code listings for both functions can be found in Appendix C.
boundaries B = b o u n d a r i e s ( f , conn, d i r )
w-w" --"-"--~~--
traces the exterior boundaries of the objects in .f, which is assumed to be g = bound2im(b, M , N, xO, yo)
nary image with 0s as the background. Parameter conn specifies the de
connectivity of the output boundaries; its values can be 4 or 8 (the defa ates a binary image, g, of size M X N, with 1s for boundary points and a
Parameter d i r specifies the direction in which the boundaries are tra round of 0s. Parameters xO and y o determine the location of the mini-
values can be ' cw ' (the default) or ' ccw ' , indicating a clockwise or cou x- and y-coordinates of b in the image. Boundary b must be an n p x 2
clockwise direction. Thus, if 8-connectivity and a ' cw ' direction are ac X np) array of coordinates, where, as mentioned earlier, np is the num-
able, we can use the simpler syntax f points. If xO and yo are omitted, the boundary is centered approximate-
the M X N array. If, in addition, M and N are omitted, the vertical and
B = boundaries(f) ontal dimensions of the image are equal to the height and width of
b. If function boundaries finds multiple boundaries, we can get all
nates for use in function bound2im by concatenating the various el-
Output B in both syntaxes is a cell array whose elements are the coordinate
See Section 6.1.1 for
the boundaries found. The first and last points in the boundaries return on e.xp/anarion o,f t l ~ r
function boundaries are the same.This produces a closed boundary. b = c a t ( 1 , B{:)) c a t operator. See
As an example to fix ideas, suppose that we want to find the bounda also E.~anlple 11.13.
the object with the longest boundary in image f (for simplicity we assume t ere the 1 indicates concatenation along the first (vertical) dimension of the
the longest boundary is unique). We do this with the following sequenc
commands:
ment simply selects the first boundary of maximum length if there is more on 11.1.2. It is required that the points in b be ordered in a clockwise or
one such boundary. As noted in the previous paragraph, the first and last p ckwise direction.
of every boundary computed using function boundaries are the same, SO boundary is subsampled using bsubsamp, its points cease to be con-
v ( 1 , : ) isthesameasrowv(end, :). onnected by using
Function bound2eight with syntax
z = connectpoly(s(:, I ) , s ( : , 2 ) ) connectpoly
$>%.,
Computing the integer coordinates of a straight line joining two point lockwise direction in Fig. 11.1) that separate two adjacent elements of the
basic tool when working with boundaries (for example, function connectp e. For instance, the first difference of the 4-direction chain code 10103322 is
requires a subfunction that does this). IPT function i n t l i n e is well suite 030. If we elect to treat the code as a circular sequence, then the first ele-
this purpose. Its syntax is of the difference is computed by using the transition between the last
mponents of the chain. Here, the result is 33133030. Normalization
[ x , y] = i n t l i n e ( x 1 , x2, y l , y2) t to arbitrary rotational angles is achieved by orienting the bound-
respect to some dominant feature, such as its major axis, as discussed
intline is ail rln- where ( x l , y I ) and (x2, y2) are the integer coordinates of the two poin
docliinenfed IPT be connected. The outputs x and y are column vectors containing the in unction fchcode, with syntax
irtilityfLinclion. Ils x- and y-coordinates of the straight line joining the two points.
code is included iit
Appendix C. c = fchcode(b, conn, d i r ) fchcode
w8~---- --
Representation
tes the Freeman chain code of an n p X 2 set of ordered boundary
As noted at the beginning of this chapter, the segmentation techniques stored in array b. The output c is a structure with the following fields,
cussed in Chapter 10 yield raw data in the form of pixels along a boundar the numbers inside the parentheses indicate array size:
pixels contained in a region. Although these data sometimes are used dir
to obtain descriptors (as in determining the texture of a region), stan c .f cc = Freeman chain code (1 x np)
practice is to use schemes that compact the data into representations tha c .d i f f = First difference of code c .f cc ( 1 X np)
considerably more useful in the computation of descriptors. In this section c .mm = Integer of minimum magnitude (1 x np)
discuss the implementation of various representation approaches. c .d i f f mm = First difference of code c .mm (1 X np)
.xOyO = Coordinates where the code starts ( 1 x 2)
9 8.2.1 Chain Codes ter conn specifies the connectivity of the code; its value can be 4 or 8
Chain codes are used to represent a boundary by a connected sequenc default). A value of 4 is valid only when the boundary contains no diago-
straight-line segments of specified length and direction. Typically, this rep
sentation is based on 4- or 8-connectivity of the segments. The directi rameter d i r specifies the direction of the output code: If 'same ' is spec-
each segment is coded by using a numbering scheme such as the ones sho the code is in the same direction as the points in b. Using ' r e v e r s e '
Figs. l l . l ( a ) and (b). Chain codes based on this scheme are referred s the code to be in the opposite direction. The default is ' same ' . Thus,
Freeman chain codes. = fchcode(b, conn) uses the default direction,and c = fchcode(b)
The chain code of a boundary depends on the starting point. However, efault connectivity and direction.
code can be normalized with respect to the starting point by treating it as a
cular sequence of direction numbers and redefining the starting point so that ure 11.2(a) shows an image, f , of a circular stroke embedded in specular EXAMPLE 11.3:
se.The objective of this example is to obtain the chain code and first differ- Freeman chain
resulting sequence of numbers forms an integer of minimum magnitude. We code and some of
normalize for rotation [in increments of 90" or 45", as shown in Figs. ll.l(a) of the object's boundary. It is obvious by looking at Fig. 11.2(a) that the
its variations.
(b)] by using the first difference of the chain code instead of the code itself. fragments attached to the object would result in a very irregular bound-
difference is obtained by counting the number of direction changes (in a co ot truly descriptive of the general shape of the object. Smoothing is a rou-
process when working with noisy boundaries. Figure 11.2(b) shows the
It, g, of using a 9 x 9 averaging mask:
I 2
A A
FIGURE 1 1.1 h = fspecial('average', 9 ) ;
(a) Direction g = imfilter(f, h, 'replicate');
numbers for
(a) a 4-directional
chain code, and
(b) an 8-d~rect~onz
2 - - 0
3\/1*0 binary image in Fig. 11.2(c) was then obtained by thresholding:
0 2 2 0 2 0 0 0 0 6 0 6 6 6 6 6 6 6 6 4 4 4 4 4 4 2 4 2 2 2
0 0 6 0 6 6 6 6 6 6 6 6 4 4 4 4 4 4 2 4 2 2 2 2 2 0 2 2 0 2
Sklansky's approach uses a so-called cellular complex or cellular rn If a in fact is part of P, but it is not a convex vertex of P, then it lies on the
which, for our purposes, is the set of square cells used to enclose a boundary,
Fig. 11.3(a). Figure 11.4(a) shows the region (shaded) enclosed by the cel discussion, a vertex of a polygon is defined to be convex if its interior
complex. Note that the boundary of this region forms a 4-connected path. s in the range 0" < 8 < 180"; otherwise the vertex is concave. As in the The conditio~z6, = 0"
traverse this path in a clockwise direction, we assign a black dot (.) to the co respect to the interior region is not aNowed(lnd
0 = 180" is rrenred
corners (those with interior angles equal to 90') and a white dot ( 0 ) to the (IS a sl~ecic~l
case.
cave corners (those with interior angles equal to 270'). As Fig. 11.4(b) show
black dots are placed on the convex corners themselves. The white dot
finding the vertices of an MPPThere are
sky et al. [1972], and Kim and Sklansky
is designed to take advantage of two basic
B functions. The first is qtdecomp, which performs quadtree de-
FlGUKlE 1 1.3 that lead to the cellular wall enclosing the data of interest.The sec-
(a) Object determine which points lie outside, on, or
boundary ned by a given set of vertices.
enclosed by cells. cedure for finding MPPs in the context
(b) Minimum- d 11.4 again for this purpose. An ap-
per~meter
polygon. boundary of the shaded inner region in
section. After the boundary has been ob-
ed, the next step is to find its corners, which we do by obtaining its Free-
n chain code. Changes in code direction indicate a corner in the boundary.
ravel in a clockwise direction through
y task to determine and mark the convex
d concave corners, as in Fig. 11,4(b).The specific approach for obtaining the
442 Chapter 1 1 a Representation and Description 11.2 Representation 443
markers is documented in M-function minperpoly discussed later in th
tion. The corners determined in this manner are as in Fig. 11.4(b), wh'
show again in Fig. ll.S(a).The shaded region and background grid are
ed for easy reference.The boundary of the shaded region is not shown t
confusion with the polygonal boundaries shown throughout Fig. 11.5.
Next, we form an initial polygon using only the initial convex vertices (th
dots), as Fig. 11.5(b) shows. We know from property 2 that the set of MPP co
vertices is a subset of this initial set of convex vertices. We see that all the con
vertices (white dots) lying outside the initial polygon do not form concavi
the polygon. For those particular vertices to become convex at a later stage
algorithm, the polygon would have to pass through them. But, we know tha
can never become convex because all possible convex vertices are accoun
at this point (it is possible that their angle could become 180" later, but that
have no effect on the shape of the polygon).Thus, the white dots outside the
polygon can be eliminated from further analysis, as Fig. 11.5(c) shows.
The concave vertices (white dots) inside the polygon are associated
concavities in the boundary that were ignored in the first pass.Thus, thes
tices must be incorporated into the polygon, as shown in Fig. 11.5(d)
point generally there are vertices that are black dots but that have ceas
convex in the new polygon [see the black dots marked with arro
Fig. 11.5(d)]. There are two possible reasons for this. The first reason
that these vertices are part of the starting polygon in Fig. 11.5(b), wh
cludes all convex (black) vertices. The second reason could be that they
become convex as a result of our having incorporated additional (white)
tices into the polygon as in Fig. ll.S(d).Therefore, all black dots in the poly
must be tested to see if any of the vertex angles at those points now exc
180". All those that d o are deleted. The procedure in then repeated.
Figure 11.5(e) shows only one new black vertex that has become conc
during the second pass through the data. The procedure terminates when
further vertex changes take place, at which time all vertices with angles of 1
are deleted because they are on an edge, and thus do not affect the sha
the final polygon. The boundary in Fig. 11.5(f) is the MPP for our exa
This polygon is the same as the polygon in Fig. 11.3(b). Finally, Fig. 1
shows the original cellular complex superimposed o n the MPP.
The preceding discussion is summarized in the following steps for fin
the MPP of a region:
1. Obtain the cellular complex (the approach is discussed later in this section)
2. Obtain the region internal to the cellular complex.
3. Use function boundaries to obtain the boundary of the region in step 2 a
a 4-connected, clockwise sequence of coordinates.
4. Obtain the Freeman chain code of this Cconnected sequence using func
tion f chcode. URE 11.5 (a) Convex (black) and concave (white) vertices of the boundary in Fig. 11.4(a).(b) Initial
5. Obtain the convex (black dots) and concave (white dots) vertices from th ygon joining all convex vertices. (c) Result after deleting concave vertices outside of the polygon.
Result of incorporating the remaining concave vertices into the polygon (the arrows indicate black
chain code. ices that have become concave and will be deleted). ( e ) Result of deleting concave black vertices (the
6. Form an initial polygon using the black dots as vertices, and delete fro w indicates a black vertex that now has become concavel. IF) Fin al result showing the MPP. (g) MPP
further analysis any white dots that are outside this polygon (whlte dots with boundary cell5 supel~rn~osed.
on the polygon boundary are kept).
444 Chapter l l RI Representation and Description 11.2 Representation 445
7. Form a polygon with the remaining black and white dots as vertices.
8. Delete all black dots that are concave vertices.
9. Repeat steps 7 and 8 until all changes cease, at which time all vertice
angles of 180" are deleted.The remaining dots are the vertices of the FIGURE 1 1.6
(a) Original
Some of t h e M-Functions Used i n Implementing the M P P Algorith image, where the
small squares
We use function qtdecomp introduced in Section 10.4.2 as the first step in denote individual
taining the cellular complex enclosing a boundary. As usual, we consider pixels. (b) 4-
region, B, in question to be composed of Is and the background of 0s. connected
qtdecomp syntax applicable to our work here is boundary.
(c) Quadtree
decomposition
Q = qtdecomp(B, t h r e s h o l d , [mindim maxdim]) using blocks of
minimum size 2
where Q is a sparse matrix containing the quadtree structure. If Q ( k , pixels on the side.
nonzero, then ( k , m ) is the upper-left comer of a block in the decompo (d) Result of
and the size of the block is Q ( k , m ) . filling with 1s all
blocks of size
A block is split if the maximum value of the block elements minus th 2 X 2 that
mum value of the block elements is greater than threshold.The value contained at least
parameter is specified between 0 and 1, regardless of the class of the one element
image. Using the preceding syntax, function qtdecomp will not produce b valued 1.This is
the cellular
complex.
(e) Inner region
must be a power of 2. of (d).
If only one of the two values is specified (without the brackets), the (f) 4-connected
boundary points
obtained using
function
boundaries.The
chain code was
obtained using
function f chcode.
K >= max(size(B) ) and ~ l m i n d i m= 2^p, or K = mindim*(2^p).Solving fo
gives p = 8, in which case M = 768.
To get the block values in a quadtree decomposition we use functi
q t g e t b l k , discussed in Section 10.4.2:
[ v a l s , r, c ] = q t g e t b l k ( 6 , Q , mindim)
The synrax for Figure 11.6(b) shows the result. Note that B is still an image, which no -Function for Computing MPPs
bwperim is tains only a 4-connected boundary (keep in mind that the small squares
Q =
1 through 9 of the MPP algorithm are implemented in function
bwperim(f, corm) dividual pixels). rpoly, whose listing is included in Appendix C. The syntax is
where conn idenri- Figure 11.6(c) shows the quadtree decomposition of B, obtained usi
fies the desired con-
nectivity: 4 (rhe [x, y ] = minperpoly(B, c e l l s i z e ) minperpoly
defarrlt) or 8.The m%w----- --
connecrivityiswith > > Q = q t d e c o m p ( B , 0 , 2);
respect to the back-
ground pixels. Thus,
ro obtain 4-connect- where 0 was used for the threshold so that blocks were split down to the
edobject boundaries mum 2 x 2 size, regardless of the mixture of 1s and 0s they contained
we specify 8 for such block is capable of containing between zero and four pixels). Not
connecredbound- there are numerous blocks of size greater than 2 X 2, but they a
aries result from homogeneous. ure 11.7(a) shows an image, B, of a maple leaf, and Fig. 11.7(b) is the EXAMPLE 11.5:
specifying a vakre o f ary obtained using the commands Using function
minperpoly.
= boundaries(B, 4, 'cw');
objects in f. This
Figure 11.6(e) shows the result. We are interested in the 4-connected boun
of this region, which we obtain using the commands ample. Figure 11.7(c) is the result of using the commands
IN = inpolygon(X, Y , xv, y v )
where X and Y are vectors containing the x- and y-coordinates of the points
a b Y FIGURE 11.10
A
c d Axis convention
used by
FIGURE 1 1.9
MATLAB for
(a) and (b) performing
Circular and P conversions
square objects. between polar
(c) and (4 and Cartesian
Corresponding coordinates,and
distance versus vice versa.
angle signatures.
*X
H
FIGURE 1 1.1 2
FIGURE 11.1 1 (a) A region S
and its convex
Boundaries of an
(shaded).
a SI c
d e f oundary is 8-connected, we count vertical and horizontal transitions as 1,
25
agonal transitions as fi.
man chromosome. (b) Image ~11100th~~ using a 25
extract the boundary of objects contained in image f using function
n. (e) Skeleton after 8 applica
erim, introduced in Section 11.2.2:
s = diameter(L)
Chaincode: 0 0 0 0 3 0 0 3 2 2 3 2 2 2 1 2 1 1
where L is a label matrix (Section 9.4) and s is a structure with the fol Difference: 3 0 0 0 3 1 0 3 3 0 1 3 0 0 3 1 3 0
Shapeno.: 0 0 0 3 1 0 3 3 0 1 3 0 0 3 1 3 0 3
fields:
s.Diameter
in the corresponding region. to rotations that are multiples of 90". An approach used frequently
s.MajorAxis A 2 X 2 matrix. The rows contain the row and
coordinates for the endpoints of the major axi
corresponding region.
s.MinorAxis
coordinates for the endpoints of the minor axis ave been developed already. They consist of function boundaries to ex-
the boundary, function diameter to find the major axis, function
EXAMPLE 11.8:
Fourier but obtained using a Gaussian mask of size 15 X 15 with sigma = The result obtained using 110 descriptors [Fig. 11.17(c)] shows
descriptors. thresholded at 0.7.The purpose was to generate an image that was not
a b
FIGURE 1 1.1 8
(a) Boundary
segment.
(b) Representat~on
as a 1-D funct~on.
ndary reconstructed using 546, 110, 56,28, 14, and 8 Fourier descriptors out
Regional Descriptors
13.3.4 Statistical Moments his section we discuss a number of IPT functions for region processing and
oduce several additional functions for computing texture, moment invari-
The shape of 1-D boundary representations (e.g., boundary segments and , and several other regional descriptors. Keep in mind that function
ture waveforms) can be described quantitatively by using statistical mo orph discussed in Section 9.3.4 is used frequently for the type of process-
such as the mean, variance, and higher-order moments. Consider Fig. 11. e outline in this section. Function roipoly (Section 5.2.4) also is used
which shows a boundary segment, and Fig. 11.18(b),which shows the segme ently in this context.
represented as a 1-D function, g ( r ) , of an arbitrary variable r.This function w
obtained by connecting the two end points of the segment to form a "major"
and then using function x2maj oraxis discussed in Section 11.3.2 to align .%Function regionprops
major axis with the x-axis. ction regionprops is IPT's principal tool for computing region descrip-
One approach for describing the shape of g ( r ) is to normalize it to unit area a .This function has the syntax
treat it as a histogram. In other words, g ( r i ) is treated as the probability of val
occurring. In this case, r is considered a random variable and the moments are D = regionprops(L, properties)
464 Chapter 11 R Representation and Description
EXAMPLE 11.9: a As a simple illustration, suppose that we want to obtain the area and
Using function bounding box for each region in an image 0. We write
regionprops.
>> B = b w l a b e l ( 0 ) ; % Convert B t o a l a b e l matrix.
2 > D = regionprops(B, ' a r e a ' , 'boundingbox');
region.The eccentricity is the ratio of the distance between the foci of th
and its major axis 1ength.The value is between 0 and 1,with 0 and 1bein
To extract the areas and number of regions we write degenerate cases (an ellipse whose eccentricity is 0 is a circle, while an el
an eccentricity of 1is a line segment).
>> w = [D.Area]; EquivDiameter ' Scalar; the diameter of a circle with the same area as the region. Comput
>> NR = l e n g t h ( w ) ; s q r t (4*Area/pi).
EulerNumber' Scalar; equal to the number of objects in the region minus the number o
where the elements of vector w are the areas of the regions and NR is the n
ber of regions. Similarly, we can obtain a single matrix whose rows are
bounding boxes of each region using the statement
V = c a t ( 1 , D.BoundingBox);
l e f t , left-bottom, l e f t - t o p ] .
This array is of dimension NR X 4. The c a t operator is explained The number of on pixels in FilledImage.
Section 6.1.1. Binary image of the same size as the bounding box of the region.The on
correspond to the region, with all holes filled.
1 1.4.2 Texture Binary image of the same size as the bounding box of the region; the on
correspond to the region, and all other pixels are o f f .
MajorAxisLength ' The length (in pixels) of the major axist of the ellipse that has the same s
texture based on statistical and spectral measures.
Statistical Approaches moments as the region.
The angle (in degrees) between the x-axis and the major axist of the elli
has the same second moments as the region.
A matrix whose rows are the [ x , y ] coordinates of the actual pixels in the
465
466 Chapter 11 a Representation and Description 11.4 D Regional Descriptors 467
TABLE 1 1.2 TABLE 1 1.3
Some descriptors Texture measures
of texture based for the regions
on the intensity 00th 87.02 11.17 0.002 -0.011 shown in
histogram of a Fig. 11.19.
region. Standard deviation u = = fi A measure of average contrast.
R = 1 - 1/(1 + u2) Measures the relative smoothness 0
the intensity in a region. R is 0 for
region of constant intensity and t = statxture(f, scale) statxture
L-""-.-
approaches 1for regions with large
excursions in the values of its e f is an input image (or subimage) and t is a 6-element row vector whose
intensity levels. In practice, the onents are the descriptors in Table 11.2, arranged in the same order. Para-
variance used in this measure is s c a l e also is a 6-element row vector, whose components multiply the cor-
normalized to the range [O, 11by
dividing it by (L - I ) ~ . nding elements of t for scaling purposes. If omitted, s c a l e defaults to all Is.
Tnird moment p3 = (zi- m)3p(zi) Measures the skewness of a histogram e three regions outlined by the white boxes in Fig. 11.19 represent, from EXAMPLE 11.10:
?his measure is 0 for symmetric o right, examples of smooth, coarse and periodic texture. The histograms Statistical texture
these regions, obtained using function i m h i s t , are shown in Fig. 11.20. The measures.
histograms, positive by histograms
skewed to the right (about the mean tries in Table 11.3 were obtained by applying function s t a t x t u r e to each
and negative for histograms skewed to the subimages in Fig. 11.19.These results are in general agreement with the
the 1eft.Values of this measure are ture content of the respective subimages. For example, the entropy of the
brought into a range of values rse region [Fig. 11.19(b)] is higher than the others because the values of
comparable to the other five measures pixels in that region are more random than the values in the other
by dividing p3by (L - 1)2also, whi
is the same divisor we used to
normalize the variance.
L-1
U = p2(zi) Measures uniformity.This measure
i=O
maximum when all gray levels are
equal (maximally uniform) and
decreases from there.
a~b C'
[ s r a d , sang, S] = s p e c x t u r e ( f ) specxture
ammr--- -----
regions. This also is true for the contrast and for the average intensity in
case. On the other hand, this region is the least smooth and the least unif srad is S(r), sang is S(O), and S is the spectrum image (displayed using
as revealed by the values of R and the uniformity measure. The histogra , as explained in Chapter 4).
the coarse region also shows the greatest lack of symmetry with respect to
location of the mean value, as is evident in Fig. 11.20(b), and also by
largest value of the third moment shown in Table 11.3.
Spectral Measures of Texture
Spectral measures of texture are based on the Fourier spectrum, which is
of high-energy bursts in the spectrum, generally are quite difficult to detect d by the random orientation of the strong edges in Fig. 11.21(a). By
spatial methods because of the local nature of these techniques.Thus spectral ,the main energy in Fig. 11.21(d) not associated with the background
ture is useful for discriminating between periodic and nonperiodic texture
terns, and, further, for quantifying differences between periodic patterns.
function and r and 0 are the variables in this coordinate system. For eac
rection 0, S(r, 8) may be considered a 1-D function, SB(r).Similarly, for
frequency r, Sr(8) is a 1-D function. Analyzing Se(r) for a fixed value
yields the behavior of the spectrum (such as the presence of peaks) along xis([horzmin horzmax vertmin vertmax])
dial direction from the origin, whereas analyzing Sr(8) for a fixed value
yields the behavior along a circle centered on the origin.
A global description is obtained by integrating (summing for discrete
ables) these functions:
IT
S(r) = C, S d r )
B=O
ot of S(r) corresponding to the ordered matches shows a strong peak
and = 15 and a smaller one near r = 25. Similarly,the random nature of the
Ro
s(0) = C, Sr(0)
r=l
- (p + q ) is defined as
, where
Y L
F 3 A.3 Moment Invariants
The 2-D moment of order ( p + q ) of a digital image f ( x , y ) is defined as
+ q = 2 , 3 , ... .
set of seven 2-D moment i n v a r i a n t s that are insensitive to translation, scale
e, mirroring, and rotation can be derived from these equations.They are
mpq= C, C, x P Y y 4 ( x , Y )
" Y 41 = 7720 + 7702
for p, q = 0 , 1 , 2 , . . . , where the summations are over the values of the 42 = (7720 - 7 7 0 2 ) ~f 477:1
coordinates x and y spanning the image. The corresponding c e n t r a l mo 43 = (77311 - 3 ~ 1 2 ) ~(37721 - 7 7 0 3 ) ~
defined as
44 = ( 7 3 0 + ~ 1 2 +) (7721
~ +~ 0 3 ) ~
= (7730 - 37712)(7730 + 7 7 1 2 ) [ ( ~ 3 0+ ~ 1 2 ) ~
Ppr, = C, C, ( X - y ) P ( ~- 7)"f( 4 Y ) $5
X Y -3(%I + ~ 0 3 ) + ~ l (37721 - ~ 0 3 ) ( ~ 2+1 7703)
where [3(7730 + ~ 1 2 -) (7721~ + 7703)~1
- mrn
... L" mnr
... $6 = (7720 - ~ 0 2 ) [ ( ~ 3+0 7 7 1 2 ) ~- (7721 7703)~I
x = -- and =2
moo moo
472 Chapter 11 Descriptors 473
a
b c
d e'
FIGURE 1 1.23
(a) Original,
padded image.
(b) Half size
image.
(c) Mirrored
Image. (d) Image
invmoments rotated by 2".
-,-
(e) Image rotated
45". The zero
padd~ngln (a)
through (d) was
done to make the
EXAMPLE 11.12 images cons~stent
Moment in slze with (e) for
invariants. viewing purposes
only.
B = fliplr(A)
returns A with the
columns flipped
about the vertical
axis, and
B = flipud(A)
retltrns A with the
rowsflipped abo~tr
the horizontal axis.
-- Image 2
>> f r 2 = i m r o t a t e ( f , 2 , ' b i l i n e a r ' ) ; column vector
>> f r 2 p = p a d a r r a y ( f r 2 , [76 761, ' b o t h ' ) ; Image 1
>> f r 4 5 = i m r o t a t e ( f , 45, ' b i l i n e a r ' ) ;
images are of size M X N,there will be total of MN such n-dimensional
rs comprising all pixels in the n images.
image in the set. The 0s in both rotated images were generated by IPT i e mean vector, m,, of a vector population can be approximated by the
process of rotation.
The seven moment invariants of the five images just discussed were g
ated using the commands m , = --Exk
l K
K k=l
>> phiorig = abs(log(invmoments(f)));
>> phihalf = abs(log(invmoments(fhs))); imilarly, the n X n covariance matrix, C , , of the population
>> phimirror = abs(log(invmoments(fm))); be approximated by
>> p h i r o t 2 = abs(log(invmoments(fr2)));
>> phirot45 = abs(log(invmoments(fr45))); l 2
c, = - K( ~ -
k mx)(xk - mX)*
K - 1k = ,
Note that the absolute value of the log was used instead of the mome
re K - 1 instead of K is used to obtain an unbiased estimate of C , from
use C, is real and symmetric, finding a set of n orthonormal
vectors always is possible.
onents trrinsfornz (also called the Hotelling transform) is
y = A ( x - m,)
s not difficult to show that the elements of vector y are uncorrelated.Thus,
trix Cy is diagonal. The rows of matrix A are the normalized
tion for more than four decades. nvectors of C , . Because C, is real and symmetric, these vectors form an
nd it follows that the elements along the main diagonal of
Using Principal Components for Description lues of C , . The main diagonal element in the ith row of Cy
variance of vector element yi.
Suppose that we have n registered images, "stacked" in the arrangem ecause the rows of A are orthonormal, its inverse equals its transpose.
shown in Fig. 11.24.There are n pixels for any given pair of coordinates (i, e x's by performing the inverse transformation
one pixel at that location for each image. These pixels may be arranged in
form of a column vector x = ATY + m x
importance of the principal components transform becomes evident when
s are used, in which case A becomes a q X n matrix, A,,.
tion is an approximation:
476 Chapter 1 1 E Representation and Description 11.5 !# Using Principal Components for Description 477
The mean square error == 1 % Handle s p e c i a l case.
the x's is given by the expression
n 4
ems= 2 Ai - i=1
i=1
I]Ai Compute an unbiased e s t i m a t e o f m.
FIGURE 1 1.25
Six multispectral
[V, Dl = e i g ( A ) images in the
returns the eigenvec- (a) visible blue,
tors of A as the (b) visible green,
columns of matrix V , (c) visible red,
and the correspond- (d) near infrared,
ing eigenval~ies (e) middle
along the main diag-
onal of diagonal ma-
infrared, and
trix D. (f) thermal
infrared bands.
(Images courtesy
of NASA.)
EXAMPLE 11.13:
Principal
components.
Next, we obtain the six principal-component images by using q = 6 in fun e first component image is generated and displayed with the commands
tion princomp: gl = P . Y ( : , 1);
gl = reshape(g1, 512, 512);
2> P = princomp(X, 6); imshow(g1, I I )
D Chapter 11 a , Description
FIGURE 11.26
Principal-
component
images
corresponding
the images in
Fig. 11.25.
>> Dl = d o u b l e ( f 1 ) - d o u b l e ( h 1 ) ;
>> Dl = gscale(D1) ;
>> imshow(D1)
Figure 11.28(a) shows the result.The low contrast in this image is an ind'
tion that little visual data was lost when only two principal component ima
were used to reconstruct the original image. Figure 11.28(b) shows the di
ence of the band 6 images. The difference here is more pronounced bec
the original band 6 image is actually blurry. But the two principal-compo
images used in the reconstruction are sharp, and they have the strongest i
ence on the reconstruction. The mean square error incurred in using only two
principal component images is given by
P. ems
ans =
TABLE
IABLt 11 1.5
1.3
1 .7311 e+003 A2 A3 A, As A6
Eigenvalues of
10352 2959 1403 203 94 31 P.Cy whenq = 6.
which is the sum of the four smaller eigenvalues
eieenvalues in Table 11.5.
X = I:[
xn
12.2 Computing Distance Measures in MATLAB
re each component, xi,represents the ith descriptor and n is the total num-
of such descriptors associated with the pattern. Sometimes it is necessary
putations to use row vectors of dimension 1 x n, obtained simply by
g the transpose, xT, of the preceding column vector.
nature of the components of a pattern vector x depends on the ap-
oach used to describe the physical pattern itself. For example, consider the
of automatically classitying alphanumeric characters. Descriptors
for a decision-theoretic approach might include measures such 2-D
invariants or a set of Fourier coefficients describing the outer bound-
where D ( i ,j ) is the Euclidean distance between the ith and jth rows o D = MAHALANOBIS(Y, CX, MX) computes the Mahalanobis distance
populations; that is, the distance between X ( L'I , : ) and Y ( j , : ) . between each vector i n Y and the given mean vector, MX. The
The syntax for function permute in the preceding expression is results are output i n vector D, whose length i s size(Y, 1 ) . The
vectors i n Y are assumed to be organized as the rows of this
B = permute(A, o r d e r ) array. The input data can be real or complex. The outputs are
real quantities. In addition to the mean vector MX, the
This function reorders the dimensions of A according to the elements of the ve covariance matrix CX of a population of vectors X also must be
order (the elements of this vector must be unique). For example, if A is a provided. Use function COVMATRIX (Section 11.5) to compute MX and
array, the statement B = permute ( A , [2 1 1 ) simply interchanges th
columns of A, which is equivalent to letting B equal the transpose of A. I y Manipulation Tips
of vector order is greater than the number of dimensions of A,
home .online. no/-pjacklam/matlab/doclmtt/index.h t m l
the third dimension, each being a column (dimension 1) of X. Since there www,prenhall.com/gonzalezwoodseddins
columns in X, n such arrays are created, with each array being of dimension p ram = varargin; % Keep i n mind that param i s a c e l l array.
Therefore, the command perrnute(X, [ I 3 I!: ) creates an array of dimen
p X 1 X n. Similarly,the command permute (Y, [3 1 21 ) creates an array o = size(Y, 1 ) ; % Number of vectors i n Y .
length(param) == 2
d = sqrt(sum(abs(M - repmat(x, W, 1 ) ) . ^ 2 , 2 ) )
ause all distances are positive, this statement can be simplified by ignoring
s q r t operation. The minimum of d determines the class membership of
-
ferent location of the minimum.
If, instead of a single
of a matrix, X, then we
Section 12.2 to obtain a matrix D, whose element D ( I , J ) is the Euclidean f (x, y)w*(x,y) F(u, v ) o H ( u , v )
tance between the ith pattern vector in X and the jth mean vector in M.Thu second aspect of the correlation theorem is included for completeness. It
find the class membership of, say, the ith pattern in X, we find the column 10 t used in this chapter.
tion in row i of D that yields the smallest value. Multiple minima yield multi mplementation of the first correlation result in the form of an M-function
-
values, as in the single-vector case discussed in the last paragraph. traightforward, as the following code shows.
It is not difficult to show that selecting the s~nallestdistance is equivalen
evaluating the functions -------
dftcorr
"--
1 G = DFTCORR(F, W) performs the correlation of a mask, W, w i t h
d,(x) = xrmi - -mTmi j = 1 , 2 , .. .,w image F. The output, G , i s the correlation image, of class
2 double. The output i s of the same size as F. When, as i s
and assigning x to class generally true i n practice, the mask image i s much smaller than
mulation agrees with the concept of a decision function defined earlier. G , wraparound error i s negligible i f W i s padded t o s i z e ( F ) .
The decision boundary between classes wi and wi for a minimum dista
classifier is
conj (fft2(w, M, N ) ) ;
d i j ( x ) = d i ( x ) - d,(x) real(ifft2(w.*f));
1
-
- xT(mi - mi) - - ( m i - mi)T(mi
2
+ mi) = 0 Figure 12.l(a) shows an image of Hurricane Andrew, in which the eye of EXAMPLE 12.1:
storm is clearly visible. As an example of correlation, we wish to find the Using correlation
tion of the best match in (a) of the eye image in Fig. 12.l(b). The image is for image
The surface given by this equation is the perpeindicular bisector of the line se matching.
ment joining mi and mi. For n = 2, the perpendicular bisector is a line, fo ze 912 X 912 pixels; the mask is of size 32 x 32 pixels. Figure 12.l(c) is the
n = 3 it is a plane, and for n > 3 it is called a hyperplane.
uit of the following commands:
g = d f t c o r r ( f , w);
'6 2-3-3 Matching by Correlation gs = g s c a l e ( g ) ;
Correlation is quite sim
tion problem is to find e blurring evident in the correlation image of Fig. 12.l(c) should not be a
(also called a mask or template) w ( x , y). Typically, w ( x , y ) prise because the image in 12.l(b) has two dominant, nearly constant re-
than f ( x ,y ) . One approach for finding m a t c h ~ is
: ~to treat w ( x , ns, and thus behaves similarly to a lowpass filter.
filter and compute the sum of products (or a normalized version o
location of w in f , in exactly the same manner explained in Sectio
the best match (matches) of w ( x , y ) in f ( x ,y ) is (are) the location(
maximum value(s) in the resulting correlation image. Unless w ( x , y )
the approach just described generally becomes computationally inten [ I , J ] = f i n d ( g == m a x ( g ( : ) ) )
this reason, practical implementations of spatial correlation typically re
hardware-oriented solutions.
For prototyping, an alternative approach is to implement correlation in
frequency domain, making use of the correlation theorem, which, like the c
volution theorem discussed in Chapter 4, relates spatial correlation to
product of the image transforms. Letting " 0 " denote correlation an
complex conjugate, the correlation theorem stmatesthat this case the highest value is unique.As explained in Section 3.4.1, the coor-
ates of the correlation image correspond to displacements of the template,
f ( x ,Y ) o ~ ( xy ), o F ( u , v ) H * ( u ,v) coordinates [ I , J ] correspond to the location of the bottom, left corner of
492 Chapter 12 s Object Recognition 12.3 Recognition Based on Decision-TheoreticMethods 493
FIGURE 12.1
(a) Multlspectral
Image of
Hurricane
Andrew.
(b) Template.
(c) Correlation of
Image and
template.
(d) Locatlon of
the best match.
(Ong~nalImage
courtesy of
NOAA.)
1 1
d , ( x ) = In P ( w j ) - - l n J ~ , ]- -[(x - m j ) T ~ 7 ' ( -x m j ) ]
See Fig. 3.14for nn the template. If the template were so located on top of the image, we wou 2 2
ofthe find that the template aligns quite closely with the eye of the hurricane
mechanics of j = 1 , 2 , . . . , W. The term inside the brackets is recognized as the Maha-
correlation. those coordinates. Another approach for finding the locations of the matche
is to threshold the correlation image near its maximum, or threshold its scale obis distance discussed in Section 12.2, for which we have a vectorized imple-
version, gs, whose highest value is known to be 255. For example, the image in entation. We also have an efficient method for computing the mean and
Fig. 12.l(d) was obtained using the command variance matrix from Section 11.5, so implementing the Bayes classifier for the
ultivariate Gaussian case is straightforward, as the following function shows.
'/$ '
\
- % conserve memory.
M
I = eye(slze(CA,l));
Next we obtain the vectors corresponding to each region. The four images
for J = 1 : W lready are registered spatially, so they simply are concatenated along the
third dimension to obtain an image stack:
!ye(n)retunlsthen CA(:J:JJ)=IM/CA(:~:~J);
: n identity matrix; end
! y e ( m , n ) or
! y e ( [ m n ] ) returns
% Evaluate the decision functions. The sum terms are the 1 >> s t a c k = c a t ( 3 , f l , f 2 , f 3 , f 4 ) ;
m m x n matrix % Mahalanobis distances discussed i n Sectiorn 12.2. where f 1 thorough f 4 are the four images in Figs. 12.2(a) through (d). Any
vith I s along the di- MA = MA'; % Organize the mean vectors as rows. point, when viewed through these four images, corresponds to a four-
rgonal and 0s else- for I = l:K dimensional pattern vector (see Fig. 11.24). We are interested in the vectors
vhere. The syntax for J = 1:W
?ye(size(A) contained in the three regions shown in Fig. 12.2(e), which we obtain by using
rives the same result
m = W(J, :);
Y = X - m(ones(size(X, I ) , I ) , : ) ; function imstack2vectors discussed in Section 11.5:
rs the previous for-
nat, with m and n i f P(J) == 0
)eing the ni~nlberof D(1, J ) = -1nf; >> [ X I R ] = imstack2vectors(stackJ 6 ) ;
.ows and columns in else
4, respectively.
D ( 1 , J ) = l o g ( P ( J ) ) - 0.5*10g(DM(J)) ... where X is an array whose rows are the vectors, and R is an array whose rows
- 0.5*sum(Y(IJ :)*(CA(:, :, J)*Y(I, : ) I ) ) ;
are the locations (2-D region coordinates) corresponding to the vectors in X.
496 Chapter 12 1 Object Recognition 12.3 a Recognition Based o n Decision-Theoretic Methods 497
Using imstack2vectors with the three masks BI,B2, and 83 yielded three
ector sets, XI, X2, and X3, and three sets of coordinates, R1, R2, and R3. Then
ree subsets Y1, Y2, and Y3 were extracted from the X's to use as training Sam-
FIGURE 12.2 es to estimate the covariance matrices and mean vectors. The Y's were gen-
Bayes rated by skipping every other row of XI, X2, and X3. The covariance matrix
classification of nd mean vector of the vectors in Yl were obtained with the command
multispectral
data.
(a)-(c) Images in
the blue, green,
and red visible and similarly for the other two classes. Then we formed arrays CA and M
A for
wavelengths. use in bayesgauss as follows:
(d) Infrared
image. (e) Mask
showing sample >> CA = c a t ( 3 , C1, C2, C3);
regions of water >> MA = c a t ( 2 , ml, m2, 1713);
(1),urban
development (2), The performance of the classifier with the training patterns was determined by
and vegetation classifying the training sets:
(3). (f) Results of
:lassification. The
black dots denote >> dY1 = bayesgauss(Y1, CA, MA);
points classified
incorrectly.The and similarly for the other two cla~~ses.
The number of misclassified patterns of
3ther (white) class 1 was obtained by writing
Joints in the
:egions were
classified
correctly.
,Original images e class into which the patterns were misclassified is straightforward.
:ourtesy of ce, length ( fi n d (dY1 =:= 2 ) ) gives the number of patterns from
NASA.)
were misclassified into c:lass 2.The other pattern sets were handled
a similar manner.
Table 12.1 summarizes the recognition results obtained with the training
nd independent pattern sets.The piercentage of training and independent pat-
rns recognized correctly was about the same with both sets, indicating stabil-
y in the parameter estimates. The largest error in both cases was with
patterns from the urban area. This is not unexpected, as vegetation is present
there also (note that no patterns in the urban or vegetation areas were mis-
classified as water). Figure 12.2(f) shows as black dots the points that were
isclassified and as white dots the points that were classified correctly in each
ion. No black dots are readily visible in region 1because the 7 misclassified
are very close to, or on, the boundary of the white region.
ditional work would be required to design an operable recognition sys-
or multispectral classification. However, the important point of this ex-
ample is the ease with which such a system could be prototyped using
MATLAB and IPT functions, comp;lemented by some of the functions devel-
498 Chapter 12 . Object R e c o g n l t i o ~ ~ 12.4 % Structural Recognition 499
TABLE 12.1 Bayes classif~cat~on
of mult~spectralimage data ect botindaries in a given application are represented by minimum-perimeter
polygons. A decision-theoretic approach might be based on forming vectors
Training Patterns Independent Patterns
whose elements are the numeric values of the interior angles of the polygons,
No. of Classified into Class a
/'
while a structural approach might be based on defining symbols for runges of
Class Samples 1 2 3 angle values and then forming a string of such symbols to describe the patterns.
1 484 482 2 0 99.6 1 483 Strings are by far the most common representation used in structural recogni-
2 933 0 885 48 94.9 2 932 880 52 94.4 tion, so we focus on this approach in this section. A s will become evident shortly,
3 483 0 19 464 96.1 3 482 16 466 96.7 MATLAB has an extensive set of functions specialized for string manipulation.
al networks suitable for image-processing applications is not beyond the capabil- >> S = [ ' a b c ' ; ' d e f g ' ; 'hi '1 % Note t h e b l a n k s .
ities of the functions available to us in MATLAB and IPT. However, this effort S =
would be unwarranted in the present context because a comprehensive neural- abc
def g
networks toolbox has been available from The Ma.thWorks for several years.
C hi
Structural Recognition
Structural recognition techniques are based generally on representing objects of
interest as strings, trees, or graphs and then defining descriptors and recognition
I Typing whos S at the prompt displays the following information:
>> whos S
Size Bytes Class
rules based on those representations. The key difference between decision- 3x4 24 char array
theoretic and structural methods is that the former uses quantitative descriptors
1
expressed in the form of numeric vectors. Struc1:ural techniques, on the other
hand, deal principally with symbolic information. For instance. suppose that ob- 1 'iorne of the rtrln. funcl~onsdncuned in lhls reclion were introduced in earlier chapters.
500 Chapter 12 @ Object Recognition 12.4 jaf Structural Recognition
TABLE 12.2 - Note in the first command line that two of the three strings in S have trailing
Category Function Name Explanation
MATLAB's - blanks because all rows in a string matrix must have the same number of char-
,tring- General blanks String of blanks. acters. Note also that no quotes enclose the strings in the output because S is a
~nanipulation cellstr Create cell array of strings from character character array. The following command returns a 3 x 1 cell array:
functions. array. Use function char to convert back to a
character string.
char Create character array (string).
deblank Remove trailing blanks. C =
eval Execute string with MATLAB expression. ' abc'
String tests iscellstr True for cell array of strings. 'defg'
ischar True for character array. 'hi'
isletter True for letters of the alphabet.
isspace True for whitespace characters. >> whos c
String operations lower Convert string to lowercase. Name Size Bytes Class
regexp Match regular expression. C 3x1 294 c e l l array
regexpi Match regular expression, ignoring case.
regexprep Replace string using regular expression. where, for example, c ( 1) = ' abc ' .Note that quotes appear around the strings
strcat Concatenate strings. in the output, and that the strings have n o trailing blanks.To convert back to a
strcmp Compare strings (see Section 2.10.5). string matrix we let
strcmpi Compare strings, ignoring case.
strfind Find one string within another. Z = char(c)
strjust Justify string. z =
strmatch Find matches for string. abc
strncmp Compare first n characters of strings. def g
strncmpi Compare first n characters, ignoring case. hi
strread Read formatted data from a string. See
Section 2.10.5 for a detailed explanation.
strrep Replace a string within another. Function e v a l evaluates a string that contains a MATLAB expression.The
strtok Find token in string. call e v a l ( e x p r e s s i o n ) executes e x p r e s s i o n , a string containing any valid
strvcat Concatenate strings vertically. MATLAB expression. For example, if t is the character string t = ' 3 " 2 ' , typ-
upper Convert string to uppercase. ing e v a l ( t ) returns a 9.
String to number double Convert string to numeric codes. The next category of functions deals with string tests. A 1 is returned if the
conversion int2str Convert integer to string. funtion is t r u e ; otherwise the value returned is O.Thus, in the preceding exam-
mat2str Convert matrix to a string suitable for ple, i s c e l l s t r ( c ) would return i3 1 and i s c e l l s t r ( S ) would return a 0. lstr
processing with the eval function. Similar comments apply to the other functions in this category.
num2str Convert number to string. String operations are next. Functions lower (and upper) are self explana-
sprintf Write formatted data to string. tory. They are discussed in Section 2.10.5. The next three functions deal with
str2double Convert string to double-precision value.
str2num Convert string to number (see Section 2.10.5).
regular expression^,^ which are sets of symbols and syntactic elements used
sscanf Read string under format control. commonly to match patterns of text. A simple example of the power of regular
Base number base2dec Convert base B string to decimal integer. expressions is the use of the familiar wildcard symbol " * " in a file search. For
conversion bin2dec Convert binary string to decimal integer. instance, a search for imagex.m in a typical search command window would re-
dec2base Convert decimal integer to base B string. turn all the M-files that begin with the word "image." Another example of the
dec2bin Convert decimal integer to binary string. use of regular expressions is in a search-and-replace function that searches for
dec2hex Convert decimal integer to hexadecimal string. an instance of a given text string and replaces it with another. Regular expres-
hex2dec Convert hexadecimal string to decimal integer. sions are formed using metacharacters, some of which are listed in Table 12.3.
hex2num Convert IEEE hexadecimal to double-
precision number.
Regular expresslons can be traced to the work of Amencan mathematlclan Stephen Kleene, who devel-
oped regular expresslons as a notatlon for descr~b~ng
what he called "the algebra of regular sets.'
1
1
502 Chapter 12 r Object Recognition 12.4 Structural Recognition 503
In the context of this discussion, a "word" is a substring within a string, preced- The value of i d x identifies the point in this case where a 0 is followed by three
ed by a space or the beginning of the string, and ending with a space or the end 3s. More complex expressions are formed in a similar manner.
of the string. Several examples are given in the following paragraph. Function r e g e x p i behaves in the manner just described for regexp, except ,*$eg?xpl
Function regexp matches a regular expression. Using the basic syntax that it ignores character (upper and lower) case. Function regexprep, with
syntax
idx = regexp(str, expr)
returns a row vector, i d x , containing the indices (locations) of the substrings s = regexprep(str, expr, replace)
in s t r that match the regular expression string, expr. For example, suppose
that e x p r = ' b . * a 4 .Then the expression i d x = r e g e x p ( s t r , e x p r ) would replaces with string r e p l a c e all occurrences of the regular expression e x p r in
mean find matches in string s t r for any b that is followed by any character (as string, str.The new string is returned. If n o matches are found regexprep re-
specified by the metacharacter ".") any number of times, including zero times turns st r, unchanged.
(as specified by *),followed by a n a.The indices of any locations in s t r meet- Function s t r c a t has the syntax
ing these conditions are stored in vector idx. If n o such locations are found, i
>> a = ' h e l l o ' % Note t h e t r a i l i n g blank space. where S and T can be cell arrays of strings, returns an array R the same size as
>> b = 'goodbye' S and T containing 1for those elements of S and T that match (up to n charac-
>> s t r c a t ( a , b ) ters), and 0 otherwise. S and T must be the same size (or one can be a scalar
ans = cell). Either one can also be a character array with the correct number of rows.
hellogoodbye The command strncmp is case sensitive. Any leading and trailing blanks in ei-
[ a bl ther of the strings are included in the comparison. Function strncmpi per- ; "stcncmpl
ans = forms the same operation as strncmp, but ignores character case.
h e l l o goodbye Function s t rf ind, with syntax
Function s t r v c a t , with syntax I = s t r f i n d ( s t r , pattern) a
+<' \
estvflnd
I*
m = s t r m a t c h ( ' s t r l , STRS)
where either S or T is a cell array of strings, and K is an array (of the same size
as S and T) containing Is for the elements of S and T that match, and 0s for the looks through the rows of the character array or cell array of strings, STRS, to
ones that do not. S and T must be of the same size (or one can be a scalar cell). find strings that begin with string st r, returning the matching row indices.The
Either one can also be a character array with the proper number of rows. alternate syntax
Function s t rcmpi performs the same operation as st rcmp, but it ignores char-
acter case.
m = s t r m a t c h ( ' s t r ' , STRS, ' e x a c t ' )
Function s t rncmp, with syntax
returns only the indices of the strings in STRS matching s t r exactly. For exam-
ple, the statement
returns a logical t r u e (1) if the first n characters of the strings s t r l and s t r 2
are the same, and returns a logical f a l s e (0) otherwise. Arguments s t r l and >> m = s t r m a t c h ( ' m a x ' , s t r v c a t ( ' m a x l , 'minimax', 'maximum'));
s t r 2 can be cell arrays of strings also.The syntax
returns m = [ 1 ; 31 because rows 1 and 3 of the array formed by s t r v c a t begin
with ' max ' . On the other hand. the statement
,506 Chopter 12 3 Object Recognition 12.4 r Structural Recognition 507
>> m = strmatch('max', s t r v c a t ( ' m a x ' , 'minimax', 'maximum'), ' e x a c t ' ) ; converts an integer to a string with integer format. The input N can be a single
integer or a vector or matrix of integers. Noninteger inputs are rounded before
returns m = 1, because only row 1 matches ' max ' exactly. conversion. For example, i n t 2 s t r ( 2 + 3 ) is the string ' 5 ' . For matrix or vec-
Function s t r r e p , with syntax tor inputs, i. n t 2 s t r returns a string matrix:
fi
=
returns the first token in the text string s t r , that is, the first set of characters
before a delimiter in delim is encountered. Parameter delim is a vector con- converts matrix A using n digits of precision. For example, consider the matrix
taining delimiters (e.g., blanks, other characters, strings). For example,
>> t = s t r t o k ( s t r , d e l i m )
t =
An image i s an o r d e r e d s e t of p i
where b IS a string of 9 characters, including the square brackets, spaces, and a
The next set of functions in Table 12.2 deals with conversions between semicolon. The command
strings and numbers. Function i n t 2 s t r , with syintax
>> e v a l ( m a t 2 s t r ( A ) )
lnt2str s t r = int2str(N)
1
reproduces A.The other functions in this category have sinl~larinterpretations.
508 Chapter 12 B Object Recognition 12.4 B Structural Recognition 509
The last category in Table 12.2 deals with base number conversions. For ex- Because matching is performe'd between corresponding symbols, it is re-
ample, function deczbase, with syntax quired that all strings be "registered" in some position-independent manner in
order for this method to make sen:se.One way to register two strings is to shift
s t r = dec2base(d, base) one string with respect to the other until a maximum value of R is obtained.
This and other similar matching strategies can be developed using some of the
converts the decimal integer d to the specified base, where d must be a non- string operations detailed in Table 12.2. Typically, a more efficient approach is
negative integer smaller than 2^52, and base must be an integer between 2 to define the same starting point for all strings based on normalizing the
and 36.The returned argument st r is a string. For example, the following com- boundaries with respect to size anti orientation before their string representa-
mand converts 2310 to base 2 and returns the result as a string: tion is extracted.This approach is illustrated in Example 12.3.
The following M-function computes the preceding measure of similarity for
>> s t r = dec2base(23, 2 ) two character strings.
str =
function R = strsimilarity(a, b) strsimilarity
10111 %STRSIMILARITYComputes a similarity measure between two strings.
,mr-. "
W
warning Display warning message.
while Repeat statements an indefinite number of times.
whitebg Change background color.
whos List variables in the workspace.
X
xlabel Label the x-axis.
xlim Set or query x-axis limits.
xor Exclusive or.
xtick Set horizontal axis tick.
Y
ylabel Label the y-axis.
ylim Set or query y-axis limits.
ytick Set vertical axis tick.
z
zeros Generate array of zeros.
Preview
In this appendix we develop the i c e interactive color editing (ICE) function
introduced in Chapter 6. The discussion assumes familiarity on the part of the
reader with the material in Section 6.4. Section 6.4 provides many examples of
using i c e in both pseudo- and full-color image processing (Examples 6.3
through 6.7) and describes the i c e calling syntax, input parameters, and
graphical interface elements (they are summarized in Tables 6.4 through 6.6).
The power of i c e is its ability to let users generate color transformation curves
interactively and graphically, while displaying the impact of the generated
transformations o n images in real or near real time.
E
18 Appendix B e ICE and MATLAB Graplucal User Interfaces B.l @ Creating ICE'S Graphical User Interface 529
when the GUI is launched and exited, and cclllbnck functions that are push buttons (Reset and Reset A l l ) , a popup menu for selecting a color trans-
executed when a user interacts with GUI objects-for example, when a formation curve, and three axes objects for displaying the selected curve (with
button is pushed.The GUI M-file for ICE ( i c e . m) is described in the next associated control points) and its effect on both a gray-scale wedge and hue
section. wedge. A hierarchical list of the elements comprising ICE (obtained by clicking
the Object Browser button in the task bar at the top of the Layout Editor) is
To launch GUIDE from the MATLAB command 'window, type shown in Fig. B.2(a). Note that each element has been given a unique name or
tag. For example, the axes object for curve display (at the top of the list) is as-
guide filename
signed the identifier curve-axes [the identifier is the first entry after the open
parenthesis in Fig. B.2(a)J.
where filename is the name of an existing FIG-file on the current path. If
Tags are one of several properties that are common to all GUI objects. A
filename is omitted, GUIDE opens a new (i.e., blank) window.
scrollable list of the properties characterizing a specific object can be obtained
Figure B.l shows the GUIDE Layout Editor (launched by entering guide
by selecting the object [in the Object Browser list of Fig. B.2(a) or layout area
i c e at the MATLAB >> prompt) for the Interactive Color Editor (ICE) lay-
of Fig. B.l using the Selection Tool] and clicking the Property Inspector button
out. The Layout Editor is used to select, place, size, align, and manipulate
on the Layout Editor's task bar. Figure B.2(b) shows the list that is generated
graphic objects on a mock-up of the user interface under development. The
when the f i g u r e object of Fig. B.2(a) is selected. Note that the f i g u r e ob-
buttons on its left side form a Component Palette containing the GUI objects
ject's Tag property [highlighted in Fig. B.2(b)] is ice. This property is impor-
that are supported-Push Buttons, Toggle Buttons, Radio Buttons, Checkboxes,
tant because GUIDE uses it to automatically generate f i g u r e callback TheGUIDEgener-
Edit Texts, Static Texts, Sliders, Frames, Lzstboxes,Popup Menus, and Axes. Each
function names.Thus, for example, the WindowButtonDownFcn property at the "led f i g u r e Object
object is similar in behavior to its standard Windows' counterpart. And any is u conlniner for nil
bottom of the scrollable Property Inspector window, which is executed when a objects in
combination of objects can be added to the figure object in the layout area on
mouse button is pressed over the figure window, is assigned the name inrerfnce.
the right side of the Layout Editor. Note that the I'CE GUI includes checkbox-
ice~WindowButtonDownFcn.Recall that callback functions are merely
es (Smooth, Clamp Ends, Show PDF, Show CDF, Map Bars, and Map Image), static
M-functions that are executed when a user interacts with a GUI object. Other
text ("Component:", "Curve", . . .), a frame outlining the curve controls, two
!
U I C O ~ C L U ~(componenc~opup"RCE") SelecUonHi~hllghl on
TW uicontrol ( t e x t 2 "Input:")
,- Sha~BCOIOrS
& . I normal
ral uicancrol (cexc3 "D~cpuc:")
ulconrrol (snoom-checkbou "Snooch")
/ W u i c o n t r o l ( r e s e t ~ u s h b u c c o n"Rcscc")
9
: ~ I ~ m t (~t eox tl4 "Curve")
Or uicontrol (Inpu.c_ceYt ""1
"",
I
1
-Units
~ser~ala z:~e amad
Id
i
rrr mcorrtrol (ourput-text Llsible d o n
FIGURE B.2 (a)The GUIDE Object Browser and (b) Property Inspector for the ICE "figure" object.
Appendix B .B ICE and MATLAB Graphical User Interfaces
B.2 !Y Programming the ICE Interface 531
530
notable (and common to all GUI objects) properties include the P0siti.0 function component~popup~Callback(hObject,eventdata, handles)
function smooth~checkbox~Callback(h0bject,eventdata, handles)
and Units properties, which define the size and locatio~~ of an object. function reset-pushbutton-Callback(hObject, eventdata, handles)
Finally, we note that some properties are unique to particular objects.A push- function slope~checkbox~Callback(h0bject,eventdata, handles)
button object, for example, has a Callback property that defines the functi function resetall~pushbutton~Callback(hObject , eventdata, handles)
that is executed when the button is pressed and the S t r i n g property that det function pdf~checkbox~Callback(hObject,eventdata, handles)
mines the button's label. The Callback property of the ICE Reset button is function cdf~checkbox~Callback(h0bject,eventdata, handles)
reset-pushbutton-Callback [note the incorporation of its Tag property from function m a p b a r ~ c h e c k b o x ~ C a l l b a c k ( h 0 b j e c teventdata,
, handles)
Fig. B.2(a) in the callback function name]; its S t r i n g property is "Reset". Note, function mapimage~checkbox~Callback(h0bject,eventdata, handles)
however, that the Reset pushbutton does not have a WindowButtonMotionFcn
property; it is specific to "figure" objects. This automatically generated file is a useful starting point or prototype for the
development of the fully functional i c e interface. (Note that we have stripped
ml Programming the ICE Interface the file of many GUIDE-generated comments to save space.) In the sections
that follow,we break this code into four basic sections: (1)the initialization code
When the ICE FIG-file of the previous section is first saved or the GUI is first between the two "DO NOT EDIT" ciomment lines, (2) the figure opening and out-
run (e.g., by clicking the Run button on the Layout Editor's task bar). GUIDE put functions (ice-0peningFcn isnd ice-OutputFcn), (3) the figure callback
generates a starting GUI M-file called i c e . m. This file, which can be modified functions (i.e., the ice-WindowButtonDownFcn, ice-WindowButtonMotion-
using a standard text editor or MATLAB's M-file editor, determines how the Fcn, and ice-WindowButtonUpFcn functions), and (4) the object callback func-
interface responds to user actions. The automatically generated GUI M-file tions (e.g., reset-pus hbu t t on-Call bac k). When considering each section,
for ICE is as follows: completely developed versions of the i c e functions contained in the section are
given, and the discussion is focuse~don features of general interest to most GUI
ice
.- .--
function varargout = ice(varargin) M-file developers. The code introduced in each section will not be consolidated
-e* i
. .. % Begin initialization code - DO NOT EDIT
(for the sake of brevity) into a single comprehensive listing of i c e . m. It is intro-
GUIDEge,~ernted gui-singleton = 1 ; duced in a piecemeal manner.
srar?0zg M-file. gui-State = struct ( 'gui-Name', mfilename, ...
'gui-Singleton', gui-Singleton, ... The operation of i c e was described in Section 6.4. It is also summarized in
'gui-OpeningFcnl, @ice-OpeningFcn, ... the following Help text block from the fully developed i c e . m M-function:
gui-OutputFcni, @ice-OutputFcn, ...
gui-LayoutFcn', [ I , . .. %ICE Interactive Color Editor.
'gui -Callback1, [ I ) ;
if nargin & ischar (varargin{l) ) OUT = ICE('Property Name', 'Property Value', ;..) transforms an Help text block of
gui-State.gui-Callback = str2func(varargin{l}); image ' s color components bas;ed on interactively specified mapping Pnn1
end functions. Inputs are Property NameIProperty Value pairs:
if nargout
[varargout{l:nargout)] = gui-mainfcn(gui-State, varargin{:)); Name Value
else
gui-mainf cn(gui-State, varargini:)) ; ' image ' An RGB or monochrome i n p u t image to be
end transformed by interactively specified
% End initialization code - DO NOT EDIT mappings.
function ice-OpeningFcn(hObject, eventdata, handles, varargin) ' space' The col~orspace of the components to be
handles.output = h0bject; modified. Possible values are 'rgb' , 'cmy ' ,
guidata(hObject, handles) ; ' h s i ' , 'hsv', 'ntsc' (or ' y i q ' ) , 'ycbcr' . When
% uiwait (handles.figure1); omitted, the RGB color space i s assumed.
function varargout = ice-OutputFcn(hObject, eventdata, handles) 'wait' If 'on' (the default), OUT i s the mapped i n p u t
varargout{l) = handles .output; image and ICE returns to the calling function
or workspace when closed. If ' o f f ' , OUT i s the
function ice-WindowButtonDownFcn ( h O b j ect , eventdata, handles)
function ice~WindowButtonMotionFcn(h0bject, eventdata, handles) handle of the mapped i n p u t image and ICE
function ice-WindowButtonUpFcn(h0b ject , eventdata, handles) returns immediately.
Appendix B ICE and MATLAB Graphical User Interfaces
i%
,
% EXAMPLES:
, ice OR i c e ( ' w a i t l , ' o f f ' ) 96 Demo user interface
, ice('imagei, f ) 96 Map RGB or mono image % -------------- ____________----___--~--.~~~~~
% ice('image',f,'space','hsv') g6 Map HSV of RGB image % Reset I n i t the currently displayed mapping function
% g=ice('imagel,f) 96 Return mapped image % and uncheck a l l curve parameters.
% g = ice('imagel, f , 'wait', 'off ' ) ; % Return i t s handle % Reset A l l I n i t i a l i z e a l l mapping functions.
%
%
%
ICE displays one popup menu selectable mapping function a t a
time. Each image component i s mapped by a dedicated curve ( e . g . ,
I!' B.2.1 Initialization Code
% R, G , or 8 ) and then by an all-component curve ( e . g . , RGB). Each The opening section of code in the starting G U I M-file (at the beginning of
% curve's control points are depicted as c i r c l e s that can be moved, Section B.2) is a standard GUIDE-generated block of initialization code. Its
% added, or deleted w i t h a two- or three-button mouse: purpose is to build and display ICE'S G U I using the M-file's companion FIG-
, file (see Section B.l) and control access to all internal M-file functions. As the
% Mouse Button Editing Operation enclosing "DO NOT EDIT" comment lines indicate, the initialization code
should not be modified. Each time i c e is called, the initialization block builds
% Left Move control point by pressing and dragging. a structure called gui-State, which contains information for accessing i c e
% Middle Add and position a control point by pressing functions. For instance, named field gui-Name (i.e., gui-State .gui-Name)
% and dragging. (Optio~nallyShift-Left) contains the MATLAB function mf ilename, which returns the name of the name
% Right Delete a control point. (Optionally currently executing M-file. I n a similar manner, fields gui-OpeningFcn and
% Control-Left) gui-OutputFcn are loaded with the G U I D E generated names of i c e ' s open-
%
ing and output functions (discussed in the next section). If an I C E G U I object
% Checkboxes determine how mapping functions are computed, whether is activated by the user (e.g., a button is pressed), the name of the object's call-
% the input image and reference pseudo- and full-color bars are back function is added as field gui-Callback [the callback's name would
% mapped, and the displayed reference curve information ( e . g . , have been passed as a string in v a r a r g i n ( 1 ) 1.
% PDF):
After structure gui-State is formed, it is passed as an input argument,
%
along with v a r a r g i n ( : ) , to function gui-mainf cn. This MATLAB function ainfcn
% Checkbox Function handles G U I creation, layout, and callback dispatch. For i c e , it builds and dis-
% ______________ .............................................. plays the user interface and generates all necessary calls to its opening, output,
and callback functions. Since older versions of MATLAB may not include this
% Smooth Checked f o r cubic spline (smooth curve)
function, G U I D E is capable of generating a stand-alone version of the normal
%
% Clamp Ends
interpolation. If unchecked, piecewise linear.
Checked t o force the starting and ending curve GUI M-file (i.e., one that works without a FIG-file) by selecting Export. ..
, slopes i n cubic spline interpolation to 0. No from the File menu. In the stand-alone version, function gui-mainf cn and two
supporting routines, ice-LayoutFcn and local-openf i g , are appended to the
% effect on piecewise linear.
normally FIG-file dependent M-file. The role of ice-LayoutFcn is to create
0
Show PDF Display probability density function(s) [ i . e . ,
, histogram(s)] of the image components affected the I C E GUI. In the stand-alone version of i c e , it begins with the statement
% by the mapping function.
hl = f i g u r e ( . . .
0
Show CDF Display cumulative distributions function(s)
'Units', 'characters', . . .
O
o instead of PDFs.
0
<Note: Show PDFICDF are mutually exclusive.> ' Color ' , [O .87843137254902 0.874509803921569 0.8901 960784313731 , . . .
'Colormap', [ O 0 0.5625;O 0 0.625;O 0 0.6875;O 0 0.75;. . .
O
sets named field colortype to the value of the second argument of the
set(handles.ice, 'Units', ' p i x e l s ' ) ; input pair. This value is then used to setup ICE'S color component popup
uisz = get(handles.ice, ' P o s i t i o n ' ) ; options (i.e., the S t r i n g property of object component-popup). Later, it is
used to transform the compont:nts of the input image to the desired map-
to access the size and location of the ICE GUI (in pixels). This is accom- ping space via
plished by setting the Units property of the i c e figure, whose handle is
available in handles. i c e , to ' p i x e l s ' and then reading the Position
property of the figure (using the g e t function). The g e t function, which
returns the value of a property associated with a graphics object, is also
used to obtain the computer's display area via the s s z = get (0, where built-in function e v a l ( s ) causes MATLAB to execute string s as
' Screensize ' ) statement near the end of the opening function. Here, 0 is an expression or statement (see Section 12.4.1 for more on function
the handle of the computer display (i.e., root figure) and ' Screensize ' is eval). If handles. input is ' h s v ' , for example, eval argument [ ' rgb2'
a property containing its extent. ' hsv ' ' (handles. i n p u t ) ' ] becomes the concatenated string
In addition to providing access to GUI objects, the handles structure is ' rgb2hsv (handles. i n p u t ) ' , which is executed as a standard MATLAB
a powerful conduit for sharing application data. Note that it holds the default expression that transforms the l7GB components of the input image to the
values for twenty-three global i c e parameters (ranging from the mouse HSV color space (see Section 6.2.3).
state in handles. updown to the entire input image in handles. input).TneY
540 Appendix B % ICE and MATLAB Graphical User Interfaces 8.2 Programming the ICE Interface 541
% Scan forward for non-unique x ' s and bump the higher indexed x - - % delete for left, middle, and right button mouse clicks ('normal',
% but don't exceed 1. Scan the entire range. % 'extend',and 'alt'cases) over plot area.
nudge = 1 1 256; set(handles.curve-axes, ' Units ' , 'pixels') ;
for i = 2:size(inJ 1 ) - 1 handles.plotbox = get (handles.curve~axes,' Position') ;
if in(i, 1 ) <= in(i - 1, 1 ) set (handles.curve-axes, ' Units ' , ' normalized ' ) ;
in(i, 1 ) = min(in(i - 1, 1 ) + nudge, 1); [inplot, x, y] = cursor(hObject, handles);
end if inplot
end nodes = getfield(handles, handles.curve);
% Scan in reverse for non-unique x ' s and decrealje the lower indexed i=find(x>=nodes(:,I)); below=max(i);
% x - - but don't go below 0. Stop on the first non-unique pair. above = min(be1ow + 1 , size(nodes, 1));
if in(end, 1 ) == in(end - 1 , 1 ) if (x - nodes(below, 1)) > (nodes(above, 1) - x)
for i = size(in, 1):-1 :2 node = above;
if in(i, 1 ) <= in(i - 1, 1) else
in(i - 1 , 1) = max(in(i, 1) - nudge, 0); node = below;
else end
break; deletednode = 0;
end switch get (hobject, 'SelectionType')
end case 'normal'
end if node == above
% If the first two x's are now the same, init the curve. above = min(above + 1 , size(nodes, 1));
if in(1, 1) == in(2, 1 ) elseif node == below
in = [0 0; 1 I]; below = max(be1ow - 1 , 1);
end end
out = in; if node == size(nodes, 1)
8.2 a Programming the ICE Interface 547
below = above; else
elseif node == 1 if x > nodes(handles.above, 1 )
above = below; x = nodes(handles.abovc?, 1 ) ;
end elseif x < nodes (handles.below, 1 )
if x > nodes(above, 1) x = nodes(handles.below, 1 ) ;
x = nodes(above, 1); end
elseif x < nodes(below, 1) end
x = nodes(below, 1); nodes(handles.node, : ) = [x y] ;
end handles = setfield(handles, h~andles.curve,nodes);
handles.node = node; handles.updown = 'down' ; guidata(hObject, handles) ;
handles.below = below; handles.above = above; graph (handles);
nodes(node, : ) = [x y]; end
case 'extend' %-----------------------------*-.----------------.-----------.--------- %
if -length(find(nodes(:, 1) == x)) function ice-WindowButtonUpFcn (hobj ect , eventdata, handles)
nodes = [nodes(l:below, : ) ; [x y]; nodes(above:end, : ) I ; % Terminate ongoing control point move or add operation. Clear
handles.node = above; handles.updown = ' down'; % coordinate text below plot a~ndupdate display.
handles.below = below; handles.above = above + 1 ;
end update = strcmpi(hand1es. updown, 'down' ) ;
case ' alt ' handles.updown = 'up'; handlessnode= 0;
if (node -= 1) & (node -= size(nodes, 1)) guidata(hObject, handles);
nodes(node, : ) = [ I ; deletednode = 1 ; if update
end set(handles.input-text, 'String', ' I ) ;
tive to the figure as a two-element row vector [Xy]. 1 8.2. I Object Callback Functions
The final nine lines of the starting GUI M-file at the beginning of Section B.2
2. Since i c e is designed to work with two- and three-button mice,
are object callback function stubs. Like the automatically generated figure call-
ice-WindowButtonDownFcn must determine ,which mouse button causes
backs of the previous section, they are initially void of code. Fully developed
each callback. As can be seen in the code, this is done with a switch con-
versions of the functions follow. Note that each function processes user inter-
struct using the figure's 'SelectionType' property. Cases 'normal',
action with a different i c e u i c o n t r o l object (pushbutton, etc.) and is named
' e x t e n t ' , and ' a l t ' correspond to the left, middle, and right button
by concatenating its Tag property with string '-Callback ' . For example, the
clicks on three-button mice (or the Ieft, shift-left, and control-left clicks of
callback function responsible for handling the selection of the displayed map-
two-button mice), respectively, and are used to trigger the add control
ping function is named the c o m p o n e n t ~ p o p u p ~ C a l l b a cIt
k .is called when the
point, move control point, and delete control point operations.
user activates (i.e., clicks on) the popup selector. Note also that input argu-
3. The displayed ICE mapping function is updated (via internal function ment hob j e c t is the handle of the popup graphics object-not the handle of
graph) each time a control point is modified, but the output figure, whose the i c e figure (as in the figure callbacks of the previous section). ICE'S object
handle is stored in handles. output, is updatied on mouse button releases callbacks involve minimal code and are self-documenting.
oialy. This is because the computation of the output image, which is per-
formed by internal function render, can be time-consuming. It involves %-------------------------------------.--------------------------------
% ice
mapping separately the input image's three color components, remapping function c o m p o n e n t ~ p o p u p ~ C a l l b a c k ( h O b j e c teventdata,
, handles) Object Callbacks
each by the "all-component" curve, and conlierting the mapped compo- % Accept Color component s e l e c t i o n , update component s p e c i f i c '+w~~.--'--.""'~--
nents to the RGB color space for display. Nott: that without adequate pre- % parameters on GUI, and draw t h e selected mapping function.
cautions, the mapping function's control points could be modified c = get(hObject, ' V a l u e ' ) ;
inadvertently during this lengthy output mapping process. handles.cindex = c;
To prevent this, i c e controls the interruptibility of its various callbacks.All handles.curve = s t r c a t ( ' s e t l , num2str(c));
MATLAB graphics objects have an I n t e r r u p t i b l e property that determines guidata(hObject, handles);
whether their callbacks can be interrupted.The default value of every object's set(handles.smooth~checkbox, ' V a l u e ' , handles.smooth(c));
' I n t e r r u p t i b l e ' property is 'on ' ,which means that object callbacks can be set(handles.slope~checkbox, 'Value', h a n d l e s . s l o p e ( c ) ) ;
interrupted. If switched to ' off ' ,callbacks that occur during the execution of set(handles.pdf~checkbox, 'Value', h a n d l e s . p d f ( c ) ) ;
set(handles.cdf~checkbox, ' V a l u e ' , h a n d l e s . c d f ( c ) ) ;
the now noninterruptible callback are either ignored (i.e., cancelled) or placed graph(hand1es);
in an event queue for later processing.The dispclsition of the interrupting call-
back is determined by the ' BusyAction ' property of the object being inter-
rupted. If ' Bus yAct ion ' is ' cancel ' , the callback is discarded; if ' queue ' ,
the callback is processed after the noninterruptilble callback finishes.
The ice-WindowButtonUpFcn function uses the mechanism just de-
scribed to suspend temporarily (i.e., during output image computations) the
I
1
%----------------------------------------------------------------------%
function srnooth~checkbox~Callback(hObject,eventdata, handles)
%
%
Accept smoothing parameter f o r currently selected color
component and redraw mapping function.
i f get(hObject, 'Value')
user's ability to manipulate mapping function control points. The sequence handles.smooth(handles.cindex) = 1 ;
nodes = getfield(handles, handles.curve);
set(handles.ice, 'Interruptible', ' o f f ' ) ; nodes = spreadout(nodes);
set(handles.ice, 'Pointer', 'watch'); handles = s e t f i e l d ( h a n d l e s , handles.curve, nodes);
else
set(handles.ice, 'Pointer', 'arrow'); handles.smooth(handles.cindex) = 0 ;
set(handles.ice, 'Interruptible', ' o n ' ) ; end
guidata(hObject, handles);
set(handles.ice, 'Pointer', 'watch');
in internal function render sets the i c e figure: window's ' I n t e r r u p t i b l e ' graph(hand1es); render(hand1es);
property to ' off ' during the mapping of the output image and pseudo- and set(handles.ice, 'Pointer', 'arrow');
full-color bars.This prevents users from modifying mapping function control
.. .
50 Appendix B &T ICE and MATLAB Graphical User Interfaces B.2 Programming the ICE Interface 551
%-----------*-------------------------------------------------------------------% % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - O -6
f u n c t i o n r e s e t ~ p u s h b u t t o n ~ C a l l b a c k ( h O b j e c t eventdata,
l handles) f u n c t i o n p d f ~ c h e c k b o x ~ C a l l b a c k ( h O b j e c t e, v e n t d a t a , h a n d l e s )
% I n i t a l l d i s p l a y parameters f o r c u r r e n t l y selected c o l o r % Accept PDF ( p r o b a b i l i t y d e n s i t y f u n c t i o n o r h i s t o g r a m ) d i s p l a y
% component, make map 1 :1 , and r e d r a w i t . % p a r a m e t e r f o r c u r r e n t l y s e l e c t e d c o l o r component and r e d r a w
% mapping f u n c t i o n if smocithing i s on. I f s e t , c l e a r CDF d i s p l a y .
handles = s e t f i e l d ( h a n d l e s , h a n d l e s . c u r v e , [ 0 0; 1 1 1 ) ;
c = handles.cindex; i f get(hObject, 'Value')
h a n d l e s . s m o o t h ( c ) = 0; set(handles.smooth~checkbox, ' V a l u e ' , 0); handles.pdf(handles.cindex) = 1;
h a n d l e s . s l o p e ( c ) = 0; set(handles.slope~checkbox, ' V a l u e ' , 0 ) ; set(handles.cdf-checkbox, ' V a l u e ' , 0 ) ;
h a n d l e s . p d f ( c ) = 0; set(handles.pdf~checkbox, ' V a l u e ' , 0); handles.cdf(hand1es.cindex) = 0;
handles . c d f ( c ) = 0 ; set(handles.cdf~checkbox, ' V a l u e ' , 0 ) ; else
guidata(hObject, handles); handles.pdf(hand1es.cindex) = 0;
set(handles.ice, ' P o i n t e r ' , ' w a t c h ' ) ; end
graph(hand1es); render(hand1es); guidata(hObject, handles); graph(hand1es);
set(handles.ice, ' P o i n t e r ' , ' a r r o w ' ) ;
%-------------------------------------------------------.---------------------- % f u n c t i o n c d f ~ c h e c k b o x ~ C a l l b a ~ c k ( h O b j e cetvl e n t d a t a , h a n d l e s )
f u n c t i o n s l o p e ~ c h e c k b o x ~ C a l l b a c k ( h O b j e c t el v e n t d a t a , h a n d l e s ) % Accept CDF ( c u m u l a t i v e d i s t r i b u t i o n f u n c t i o n ) d i s p l a y p a r a m e t e r
% Accept s l o p e clamp f o r c u r r e n t l y s e l e c t e d c o l o r component and % f o r s e l e c t e d c o l o r compolnent and redraw mapping f u n c t i o n i f
% draw f u n c t i o n i f smoothing i s on. % smoothing i s on. I f s e t , c l e a r CDF d i s p l a y .
processUsingLevelB = (zmed > zmin) & (zmax > zmed) & ...
-alreadyProcessed;
zB = ( g > zmin) & (zmax > g ) ;
outputzxy = processUsingLevelB & zB;
outputzmed = processUsingLevelB & -zB;
f(0utputZxy) = g(0utputZxy);
f (outputzmed) = zrned(outputZmed) ;
alreadyprocessed = alreadyprocessed I processUsingLevelB;
i f all(alreadyProcessed( :) )
break;
end
end
% Output zmed f o r any remaining unprocessed p i x e l s . Note t h a t t h i s
% zmed was computed u s i n g a window o f s i z e Smax-by-Smax, which i s
% t h e f i n a l value o f k i n t h e l o o p .
f(-alreadyprocessed) = zmed(-alreadyprocessed);
11
% F = ADPMEDIAN(G, SMAX) performs adaptive median f i l t e r i n g of ~f - ~ s e m p t y ( r c ) & - i s e q u a l ( r c ( l , : ) , rc(end, : ) )
% image G. The median f i l t e r s t a r t s a t s i z e 3 - b y - 3 and i t e r a t e s up e r r o r ( ' E x p e c t e d l n p u t boundary t o be c l o s e d . ' ) ;
% t o s i z e SMAX-by-SMAX. SMAX must be an odd i n t e g e r g r e a t e r than 1. end
% SMAX must be an odd, p o s i t i v e i n t e g e r g r e a t e r than 1. i f size(rc, 1) < = 3
i f (Smax <= 1 ) I (Smax12 == round(Smaxt2)) I (Smax -= round(Smax))
error('SMAX must be an odd i n t e g e r > 1 . ' ) ! % Degenerate case.
rc-new = r c ;
end t return;
[MI N] = s i z e ( g ) ; end
% I n i t i a l setup. % Remove l a s t row, which equals t h e f i r s t row.
f = g; rc-new=rc(l:end-1, :);
f ( : ) = 0;
alreadyprocessed = f a l s e ( s i z e ( g ) ) ; 1' %
%
Remove t h e mlddle p l x e l I n four-connected r l g h t - a n g l e t u r n s . We
can do t h l s I n a v e c t o r l z e d fashlon, but we c a n ' t do l t a l l a t
% Begin f i l t e r i n g . % once. S l m l l a r t o t h e way t h e ' t h l n ' algorithm works l n bwmorph,
f o r k = 3:2:Smax % w e ' l l remove f l r s t t h e mlddle p l x e l s I n four-connected t u r n s where
zmin = o r d f i l t 2 ( g , 1, ones(k, k ) , ' s y m m e t r i c ' ) ; % t h e row and column a r e b o t h even; then t h e mlddle p l x e l s I n a l l
zmax = o r d f i l t 2 ( g , k * k , ones(k, k ) , ' s y m m ~ e t r i c ' ) ; % t h e remaining four-connected t u r n s where t h e row 1s even and t h e
zmed = m e d f i l t 2 ( g , [ k k ] , ' s y m m e t r i c ' ) ; % column 1s odd; then agaln where the row 1s odd and t h e column 1 s
% even; and f l n a l l y where both t h e row and column are odd.
54 Appendix C a M-Funtions Appendix C e M-Funtions 555
1
num-old-pixels = s i z e ( r c 1 , 1) ;
% Locations o f N, S, E, and W t r a n s i t i o n s f o l l o w e d by
num-new-pixels = num-old-pixels + sum(diagona1-locations);
% a right-angle turn.
rc-new = zeros(num-new-pixels, 2);
remove = - a l l ( d , 2 ) & ( d o t p r o d == 0 ) ;
% I n s e r t t h e o r i g i n a l values i n t o t h e proper l o c a t l o n s i n t h e new RC
% But we r e a l l y want t o remove t h e middle p i x e l o f t h e t u r n .
% matrix.
remove = [remove(end, : ) ; remove(1:end - 1, : ) I ;
i d x = (1:num-old-pixels)' + [O; cumsum(diagonal-locations)];
i f -any (remove) rc-new(idx, : ) = r c l ;
done = 1 ;
else
l d x = find(remove); !
4 % Compute t h e new p i x e l s t o be ~ n s e r t e d .
new_plxel-offsets = [O 1 ; -1 0; 1 0; 0 -1 1;
rc(ldx(I), :) = [ I ; b f f s e t - c o d e s = 2 * ( 1 - ( c o l d l f f (diagonal-locations) + 1 ) / 2 ) + .
end ( 2 - ( r o w d l f f (diagonal-locatl~ons) + 1 ) 12) ;
4 new-plxels = r c l (diagonal-locati~ons, : ) + . ..
function rc-new = bound2four(rc) new-plxel-of f sets ( o f f set-codes , : ) ;
%BOUND2FOUR Convert 8-connected boundary t o 4-connected boundary.
% RC-NEW = BOUND2FOUR(RC) converts an elght-connected boundary t o a I % Where do t h e new p l x e l s go?
%
%
four-connected boundary. RC 1 s a P-by-2 m a t r l x , each row of
which c o n t a i n s t h e row and column c o o r d i n a t e s o f a boundary 1 l n ~ e r t l ~ ~ - l ~ ~= azeros(num-.new-plxels,
t l ~ f l ~
insertion-locatlons(~dx) = 1 ;
1);
BG = s q r t ( B x . * 2 + B y . * 2 ) ; s w i t c h method
% Form t h e composite by adding t h e i n d i v i d u a l r e s u l t s and case ' e u c l i d e a n '
% scale t o [0, I ] . % Compute t h e Euclidean d i s t a n c e between a l l rows o f X and m. See
PPG = matZgray(RG + GG + BG); % S e c t i o n 12.2 o f DIPUM f o r an e x p l a n a t i o n o f t h e f o l l o w i n g
% expression. D ( i ) i s t h e Euclidean d i s t a n c e between v e c t o r X ( i , : )
% Threshold t h e r e s u l t . % and v e c t o r m.
i f n a r g i n == 2 p = length(f);
VG = (VG > T).*VG; D = sqrt(sum(abs(f - repmat(m, p, 1 ) ) . " 2 , 2 ) ) ;
PPG = (PPG > T).*PPG; case 'mahalanobis'
end C = varargin(5);
function I = colorseg(varargin) D = mahalanobis(f, C, m);
%COLORSEG Performs segmentation of a color imagc?. otherwise
% S = COLORSEG('EUCLIDEAN', F, T, M) performs segmentation o f c o l o r error('Unknown segmentation method.')
% image F using a Euclidean measure o f s i r n i l a t - i t y . M i s a 1 - b y - 3 end
% v e c t o r r e p r e s e n t i n g t h e average c o l o r used f o r segmentation ( t h i s % D i s a v e c t o r o f s i z e MN-by-1 c o n t a i n i n g t h e distance computations
% i s t h e center o f t h e sphere i n F i g . 6.26 o f DIPUM). T i s t h e % from a l l t h e c o l o r p i x e l s t o v e c t o r m. F i n d t h e d i s t a n c e s <= T.
% t h r e s h o l d a g a i n s t which t h e d i s t a n c e s a r e compared. J = f i n d ( D <= T);
%
% S = COLORSEG('MAHALANOBIS', F, T, MI C) performs segmentation o f % Set t h e values o f I ( J ) t o 1 . These a r e t h e segmented
% c o l o r image F u s i n g t h e Mahalanobis d i s t a n c e as a measure o f % color pixels.
% s i m i l a r i t y . C i s t h e 3 - b y - 3 covariance m a t r i x o f t h e sample c o l o r I ( J ) = 1;
% vectors of t h e c l a s s o f i n t e r e s t . See f u n c t i o n covmatrix f o r t h e % Reshape I i n t o an M-by-N image.
% computation o f C and M. I = reshape(1, M, N ) ;
%
% S i s t h e segmented image (a b i n a r y m a t r i x ) i.n which 0s denote t h e function c = connectpoly(x, y)
% background. %CONNECTPOLY Connects vertices of a polygon.
% C = CONNECTPOLY(X, Y) connects t h e p o i n t s w i t h coordinates given
% Preliminaries. % i n X and Y w i t h s t r a i g h t l i n e s . These p o i n t s are assumed t o be a
% Recall that varargin i s a c e l l array. % sequence o f polygon v e r t i c e s organized i n t h e clockwise o r
f = varargin(2); % counterclockwise d i r e c t i o n . The output, C, i s t h e s e t o f p o i n t s
i f (ndims(f) -= 3 ) 1 ( s i z e ( f , 3 ) -= 3) % along t h e boundary of t h e polygon i n t h e form o f an n r - b y - 2
e r r o r ( ' 1 n p u t image must be RGB.'); % c o o r d i n a t e sequence i n t h e same d i r e c t i o n as t h e i n p u t . The l a s t
end % p o i n t i n t h e sequence i s equal t o t h e f i r s t .
M = size(f, 1); N = size(f, 2);
% Convert f t o v e c t o r format u s i n g f u n c t i o n imstack2vectors. v = [ x ( : ) , y(:)1;
[ f , L] = imstack2vectors(f); % Close polygon.
f = double(f) ; i f -isequal(v(end, :), v(1, : ) )
% I n i t i a l i z e I as a column v e c t o r . I t w i l l be reshaped l a t e r v(end + 1, : ) = v ( 1 , : ) ;
% i n t o an image. end
I = zeros(M*N, 1 ) ;
% Connect vertices.
T = varargin{3);
m = varargin(4);
a segments = c e l l ( 1 , l e n g t h ( v ) - 1);
f o r I= 2:length(v)
m = m ( : ) ' ; % Make sure t h a t m i s a row v e c t o r .
[ x , y ] = i n t l r n e ( v ( 1 - 1, I ) , v ( 1 , I ) , v ( 1 - 1, 2 ) , v ( 1 , 2 ) ) ;
i f l e n g t h ( v a r a r g i n ) == 4 segments(1 - 1) = [ x , y ] ;
method = ' e u c l i d e a n ' ; end
e l s e i f l e n g t h ( v a r a r g i n ) == 5
method = 'mahalanobis' ;
else
e r r o r ( ' W r o n g number o f i n p u t s . ' ) ;
end function s = diameter(L)
%DIAMETER Measure diameter and related properties of image regions.
% S = DIAMETER(L) computes t h e diameter, t h e major a x l s endpoints,
Appendix C is M-Funtions 567
66 Appendix C rn M-Funtions
% t h e minor a x i s endpoints, and t h e b a s i c r e c t a n g l e o f each l a b e l e d
% r e g i o n i n t h e l a b e l m a t r i x L. P o s i t i v e i n t e g e r elements of L num-pixels = l e n g t h ( r p ) ;
% correspond t o d i f f e r e n t regions. For example, t h e s e t o f elements
s w i t c h nun-pixels
% o f L equal t o 1 corresponds t o r e g i o n 1; t h e s e t o f elements of L
% equal t o 2 corresponds t o r e g i o n 2; and so on. S i s a s t r u c t u r e
% a r r a y o f l e n g t h m a x ( L ( : ) ) . The f i e l d s o f t h e s t r u c t u r e a r r a y
% include:
% Diameter
% MajorAxis
% MinorAxis
% BasicRectangle d = (rP(2) - r p ( l ) ) * 2 + (cp(2) - cp(1))^2;
% majoraxis = [ r p cp];
% The Diameter f i e l d , a s c a l a r , i s t h e maximum d i s t a n c e between any
% two p i x e l s i n t h e corresponding r e g i o n .
% Generate a l l combinations o f 1:num-pixels taken two a t a t t i m e ,
% The MajorAxis f i e l d i s a 2 - b y - 2 m a t r i x . The rows c o n t a i n t h e row % Method suggested by Peter i2cklam.
% and column coordinates f o r t h e endpoints o f t h e major a x i s of t h e [ i d x ( : , 2) i d x ( : , I ) ] = f i n d i ( t r i l ( o n e s ( n u m - p i x e l s ) , - 1 ) ) ;
% corresponding r e g i o n . rr = r p ( i d x ) ;
% cc = c p ( i d x ) ;
% The MinorAxis f i e l d i s a 2 - b y - 2 m a t r i x . The rows c o n t a i n t h e row dist-squared = ( r r ( : , 1) - r r ( : , 2 ) ) . ^ 2 + ...
% and column coordinates f o r t h e endpoints o f t h e minor a x i s of t h e (cC(:, 1) - c c ( : , 2 ) ) . * 2 ;
% corresponding r e g i o n . [max-dist-squared, i d x ] = max(dist-squared);
% majoraxis = [ r r ( i d x , : ) ' ~ ~ ( i d x , : ) ' ] ;
% The BasicRectangle f i e l d i s a 4 - b y - 2 m a t r i x . Each row c o n t a i n s
% t h e row and column c o o r d i n a t e s o f a corner o f t h e d = s q r t (max-dist-squared) ;
% r e g i o n - e n c l o s i n g r e c t a n g l e defined by t h e major and minor axes. .
uPPer-image-row = s BoundingBox (2) + 0.5;
S,
For more i n f o r m a t i o n about these measurements, see S e c t i o n 11.2.1
.
l e f t-image-col = s BoundingBox ( i) + 0 . 5 ;
%
% o f D i g i t a l Image Processing, by Gonzalez and Woods, 2nd e d i t i o n , m a l o r a x i s ( : , 1 ) = majoraxis(:, 1) + upper-image-row - 1;
% Prentice H a l l . m a l o r a x i s ( : , 2) = m a j o r a x i s ( : , 2 ) + left-image-col - 1;
s = regionprops(L, { ' I m a g e ' , 'BoundingBox')); %----------------.---------------------------------------------------
%
f o r k = l:length(s) function [basicrect, minoraxis] = compute-basic-rectangle(s, ...
[s(k).Diameter, s ( k ) . M a j o r A x i s , perim-r, perim-c] = ... perim-r, perim-c)
compute-diameter(s(k)); % [BASICRECT,MINORAXIS] = COMPLJTE-BASIC-RECTANGLE(S, PERIM-R,
[s(k).BasicRectangle, s ( k ) . M i n o r A x i s ] = ... % PERIM-C) computes t h e basic r e c t a n g l e and t h e minor a x i s
compute-basic-rectangle ( s ( k ) , perim-r, perim-c) ; % end-points f o r t h e r e g i o n represented by t h e s t r u c t u r e S. S must
end % c o n t a i n t h e f i e l d s Image, Bo~lndingBox, MajorAxis, and
%--------------------------------------------------------------------%
% Diameter. PERIM-R and PERIM-C: are t h e row and column c o o r d i n a t e s
% o f perimeter o f s . Image. BASICRECT i s a 4 - b y - 2 m a t r i x , each row
f u n c t i o n [d, majoraxis, r, c ] = compute-diameter(s) % of which c o n t a i n s t h e row and column c o o r d i n a t e s o f one c o r n e r o f
% [D, MAJORPXIS, R, C] = COMPUTE-DIAMETER(S) computes t h e diameter
% the basic rectangle.
% and major a x i s f o r t h e r e g i o n represented by t h e s t r u c t u r e S. S
% must c o n t a i n t h e f i e l d s Image and BoundingBox. COMPUTE-DIAMETER % Compute t h e o r i e n t a t i o n o f t h e major a x l s .
% a l s o r e t u r n s t h e row and column c o o r d i n a t e s (R and C ) o f t h e t h e t a = atan2(s.MajorAxis(2, 1) - s.Ma]orAxls(l, I ) , ...
% perimeter p i x e l s o f s.Image. s.MajorAxis(2, 2) - s.MajorAxls(1, 2 ) ) ;
% Compute row and column c o o r d i n a t e s o f perimeter p i x e l s . % Form r o t a t i o n m a t r i x .
[ r , c ] = find(bwperim(s.Image)); T = [cos(theta) sin(theta); -sin(theta) cos(theta)];
r = r(:);
Appendix C M-Funtions Appendix C a M-Funtions 569
% Which p o l n t s are i n s i d e t h e lower c i r c l e ?
% Rotate perimeter p i x e l s .
p = [perim-c perim-r]; y = bottom;
p = p * T'; inside-lower = ( ( c - x ) . " 2 + ( r - y ) . ^ 2 ) < r a d i u s A 2 ;
% Which p o i n t s are i n s i d e t h e l e f t c i r c l e ?
% C a l c u l a t e minimum and maximum x - and y - c o o r d i n a t e s f o r t h e r o t a t e d
x = left;
% perimeter p i x e l s .
y = ( t o p + bottom)/2;
x = p(:, 1);
radius = r i g h t - l e f t ;
Y = p(:, 2); inside-left = ( (c - x).^2 + ( r - y ) .^2 ) < radiusA2;
min-x = m i n ( x ) ;
max-x = max(x) ; % Which p o i n t s a r e i n s i d e t h e r i g h t c i r c l e ?
min-y = m i n ( y ) ; x = right;
max-y = max(y); inside-right = ( (c - x).^2 + ( r - y).^2 ) < radiusA2;
corners-x = [min-x max-x max-x min-x]'; % E l i m i n a t e p o i n t s t h a t are i n s i d e a l l f o u r c i r c l e s .
corners-y = [min-y min-y max-y max-y]'; delete-idx = f i n d ( i n s i d e - l e f t & i n s i d e - r i g h t & ...
% Rotate corners o f t h e b a s i c r e c t a n g l e . inside-upper & inside-lower);
corners = [corners-x corners-y] * T; r(de1ete-idx) = I ] ;
c(de1ete-idx) = [ I ;
% T r a n s l a t e according t o t h e r e g i o n ' s bounding box.
.
upper-image-row = s BoundingBox (2) + 0.5;
left-image-col = s.BoundingBox(1) + 0.5;
b a s i c r e c t = [ c o r n e r s ( : , 2) + upper-image-row - 1, ... f u n c t i o n c = fchcode(b, conn, d i r )
c o r n e r s ( : , 1 ) + left-image-col -1] ; %FCHCODE Computes t h e Freeman chain code o f a boundary.
% Compute minor a x i s e n d - p o i n t s , r o t a t e d . % C = FCHCODE(B) computes t h e 8-connected Freeman chain code o f a
x = (min-x + max-x) 1 2; % s e t o f 2-D coordinate p a i r s contained i n 6, an n p - b y - 2 a r r a y . C
y l = min-y ; % i s a structure with the following f i e l d s :
y2 = max-y; %
endpoints = [ x y l ; x y21; % c.fcc = Freeman chain code ( I - b y - n p )
% c.diff = F i r s t d i f f e r e n c e o f code c . f c c ( I - b y - n p )
% Rotate minor a x i s e n d - p o i n t s back. % c.mm = I n t e g e r of minimum magnitude from c . f c c ( 1 - b y - n p )
endpoints = endpoints * T; % c.diffmm = F i r s t d i f f e r e n c e o f code c.mm (1-by-np)
% T r a n s l a t e according t o t h e r e g i o n ' s bounding box. % c.xOy0 = Coordinates where t h e code s t a r t s (1 - b y - 2 )
minoraxis = [endpoints(:, 2 ) + upper-image-row 1,- .. . %
endpoints ( : , 1 ) + l e f t-image-col - 1 ] ; % C = FCHCODE(6, CONN) produces t h e same outputs as above, b u t
%--------------------------------------------------------------------% % w i t h t h e code c o n n e c t i v i t y s p e c i f i e d i n CONN. CONN can be 8 f o r
% an 8-connected c h a i n code, o r CONN can be 4 f o r a 4-connected
function [ r , c] = prune-pixel-list(r, c)
% chain code. S p e c i f y i n g CONN=4 i s v a l i d o n l y i f t h e i n p u t
% [R, C ] = PRUNE-PIXEL-LIST(R, C) removes p i x e l s from t h e v e c t o r s
% sequence, 6, contains t r a n s i t i o n s w i t h values 0, 2, 4, and 6,
% R and C t h a t cannot be endpoints o f t h e major a x i s . T h i s
% exclusively.
% e l i m i n a t i o n i s based on g e o m e t r i c a l c o n s t r a i n t s described i n
%
% Russ, Image Processing Handbook, Chapter 8.
% C = FHCODE(B, CONN, DIR) produces t h e same o u t p u t s as above, b u t ,
top = min(r); % i n a d d i t i o n , t h e desired code d i r e c t i o n i s s p e c i f i e d . Values f o r
bottom = max(r); % DIR can be:
l e f t = min(c) ; %
0
r i g h t = max(c); O 'same' Same as t h e o r d e r o f t h e sequence o f p o i n t s i n b.
, This i s the default.
% Which p o i n t s a r e i n s i d e t h e upper c i r c l e ?
%
x = ( l e f t + right)/2;
% 'reverse' Outputs t h e code i n t h e d i r e c t i o n opposite t o t h e
y = top;
% d i r e c t i o n o f t h e p o i n t s i n 6. The s t a r t i n g p o i n t
r a d i u s = bottom - t o p ;
% f o r each D I R i s t h e same.
inside-upper = ( ( c - x) . ^ 2 + ( r - y ) . * 2 ) < r a d i ~ u s ~ 2 ;
Appendix 6 3 M-Funtions Appendix C @ M-Funtions 571
e % t h e same. I f t h i s i s t h e Case, e l i m i n a t e t h e l a s t p o i n t .
% The elements o f B a r e assumed t o correspond t o a 1 - p i x e l - t h i c k , i f i s e q u a l ( b ( 1 , : ) , b(np, : ) )
% f u l l y - c o n n e c t e d , closed boundary. B cannot c o n t a i n d u p l i c a t e np = np - 1;
% c o o r d i n a t e p a i r s , except i n t h e f i r s t and l a s t p o s i t i o n s , which b = b(l:np, : ) ;
% i s a common f e a t u r e o f boundary t r a c i n g programs. end
% % B u i l d t h e code t a b l e u s i n g t h e s i n g l e i n d i c e s from t h e f o r m u l a
% FREEMAN CHAIN CODE REPRESENTATION % f o r z g i v e n above:
% The t a b l e on t h e l e f t shows t h e 8-connected Freeman chain codes C ( l l ) = O ; C(7)=1; C(6)=2; C(5)=3; C(9)=4;
% corresponding t o a l l o w e d d e l t a x , d e l t a y p a i r s . An 8 - c h a i n i s C(13)=5; C(14)=6; C(15)=7;
% converted t o a 4 - c h a i n i f ( 1 ) i f conn = 4; and (2) o n l y
t r a n s i t i o n s 0, 2, 4, and 6 occur i n t h e 8-code. Note t h a t % End o f P r e l i m i n a r i e s .
%
% d i v i d i n g 0, 2, 4, and 6 by 2 produce t h e 4-code. % Begin processing.
0,
xO = b(1, 1 ) ;
% ........................ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ - yo -= b(1,
- 2 )-;
% d e l t a ~I d e l t a y
I 8-code corresp 4-code c.xoy0 = [XO, yo];
% _ _ _ _ _ _ _ _ ____ ____ _ ___ ._
. __
_ -_
- -_
- __.----
% Make sure t h e coordinates a r e organized s e q u e n t i a l l y :
% 0 1 0 0
% Get t h e d e l t a x and d e l t a y between successive p o i n t s i n b. The
% -1 1 1
9 % l a s t row o f a i s t h e f i r s t row o f b.
-1 0 2 1
% -1 -1 3 a = c i r c s h i f t ( b , [-I, 01);
% 0 -1 4 2 % DEL = a - b i s an n r - b y - 2 m a t r i x i n which t h e rows c o n t a i n t h e
% 1 -1 5 % d e l t a x and d e l t a y between succ:essive p o i n t s i n b. The two
% 1 0 6 3 % components i n t h e k t h row o f m a t r i x DEL are d e l t a x and d e l t a y
9
1 1 7 % between p o i n t ( x k , y k ) and ( x k + l , y k + l ) . The l a s t row o f DEL
0
, _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ____- _- _- -_-_ _ _ _ _ - - - - % contains t h e d e l t a x and d e l t a y between ( x n r , y n r ) and ( x i , y l ) ,
0
% ( i . e . , between t h e l a s t and f i r s t p o i n t s i n b ) .
% The f o r m u l a z = 4 * ( d e l t a x + 2) + ( d e l t a y + 2) g i v e s t h e f o l l o w i n g DEL = a - b;
% sequence corresponding t o rows 1 - 8 i n t h e preceding t a b l e : z =
% I f t h e abs value of e i t h e r ( o r both) components o f a p a i r
% 11,7,6,5,9,13,14,15. These values can be u s e d a s i n d i c e s i n t o t h e
% ( d e l t a x , d e l t a y ) i s g r e a t e r than 1, then by d e f i n i t i o n t h e curve
% t a b l e , improving t h e speed o f computing t h e c h a i n code. The
% i s broken ( o r t h e p o i n t s a r e out o f o r d e r ) , and t h e program
% preceding f o r m u l a i s n o t unique, b u t i t i s based on t h e s m a l l e s t
% terminates.
% i n t e g e r s ( 4 and 2 ) t h a t a r e powers o f 2.
i f any(abs(DEL(:, 1 ) ) > 1 ) ( any(abs(DEL(:, 2 ) ) > 1 ) ;
% Preliminaries. e r r o r ( ' T h e i n p u t curve i s broken o r p o i n t s a r e out o f o r d e r . ' )
i f n a r g i n == 1 end
d i r = 'same';
% Create a s i n g l e index v e c t o r u s i n g t h e formula described above.
conn = 8;
z = 4*(DEL(:, 1) + 2) + (DEL(:, 2) + 2 ) ;
e l s e i f n a r g i n == 2
d i r = 'same'; % Use t h e i n d e x t o map i n t o t h e t a b l e . The f o l l o w i n g a r e
e l s e i f n a r g i n == 3 % t h e Freeman 8 - c h a i n codes, organized i n a I - b y - n p a r r a y .
% Nothing t o do here. f c c = C(z);
else % Check i f d i r e c t i o n o f code sequence needs t o be reversed.
e r r o r ( ' 1 n c o r r e c t number o f i n p u t s . ' )
i f strcmp(dir, 'reverse')
end fcc = c o d e r e v ( f c c ) ; % See below f o r f u n c t i o n coderev.
[np, n c ] = s i z e ( b ) ; end
i f np < nc
e r r o r ( ' B must be o f s i z e n p - b y - 2 . ' ) ; % I f 4 - c o n n e c t i v i t y i s s p e c i f i e d , check t h a t a l l components
end % o f f c c a r e 0, 2, 4, o r 6 .
i f conn == 4
% Some boundary t r a c i n g programs, such as boundaries.m, output a
v a l = f i n d ( f c c == 1 I f c c == 3 / f c c == 5 1 f c c ==7 ) ;
% seauence i n which t h e c o o r d i n a t e s o f t h e f i r s t and l a s t p o i n t s a r e
i f isempty(va1)
72 Appendix C $8 M-Funtions Appendix C e M-Funtions 573
f c c = fcc.12; % M a t r i x A contains a l l t h e p o s s i b l e candidates f o r t h e i n t e g e r o f
else % minimum magnitude. S t a r t i n g w i t h t h e 2nd column, s u c c e s i v e l y f i n d
warning('The s p e c i f i e d 4-connected code cannot be s a t i s f i e d . ' ) % t h e minima i n each column o f A. The number o f candidates decreases
end % as t h e seach moves t o t h e r i g h t on A. T h i s i s r e f l e c t e d i n t h e
end % elements o f J . When l e n g t h ( J ) = l , one candidate remains. T h i s i s
% Freeman chain code f o r s t r u c t u r e o u t p u t % t h e i n t e g e r o f minimum magnitude.
c.fcc = fcc; [M, N] = size(A) ;
J = (1:M)';
% Obtain t h e f i r s t d i f f e r e n c e o f f c c . f o r k = 2:N
c . d i f f = c o d e d i f f ( f c c , c o n n ) ; % See below f o r f u n c t i o n c o d e d i f f . D(l:M, 1) = I n f ;
% Obtain code o f t h e i n t e g e r o f minimum magnitude. D(J, 1 ) = A(J, k ) ;
c.mm = minmag(fcc); % See below f o r f u n c t i o n minmag. amin = min(A(J, k ) ) ;
J = f i n d ( D ( : , 1 ) == amin);
% Obtain t h e f i r s t d i f f e r e n c e o f f c c i f length(J)==l
c.diffmm = codediff(c.mm, conn); z = A(J, : ) ;
return
function c r = coderev(fcc) end
% Traverses t h e sequence o f 8-connected Freemain chain code f c c i n end
% t h e opposite d i r e c t i o n , changing t h e values o f each code %....-.-----.......----------------------------------.---------------%
% segment. The s t a r t i n g p o i n t i s n o t changed. f c c i s a 1-by-np f u n c t i o n d = c o d e d i f f ( f c c , conn)
% array. %CODEDIFF Computes t h e f i r s t d i f f e r e n c e o f a c h a i n code.
% F l i p the array l e f t t o r i g h t . This redefines the s t a r t i n g p o i n t % D = CODEDIFF(FCC) computes t h e f i r s t d i f f e r e n c e o f code, FCC. The
% as t h e l a s t p o i n t and reverses t h e o r d e r o f " t r a v e l " through t h e % code FCC i s t r e a t e d as a c i r c u l a r sequence, so t h e l a s t element
% code. % o f D i s t h e d i f f e r e n c e between t h e l a s t and f i r s t elements o f
cr = fliplr(fcc); % FCC. The i n p u t code i s a 1-by-np v e c t o r .
%
% Next, o b t a i n t h e new code values by t r a v e r s i n g t h e code i n t h e The f i r s t d i f f e r e n c e i s found by counting t h e number o f d i r e c t i o n
%
% opposite d i r e c t i o n . ( 0 becomes 4, 1 becomes 5, ...
, 5 becomes 1, % changes ( i n a counter-clockwise d i r e c t i o n ) t h a t separate two
% 6 becomes 2, and 7 becomes 3 ) . % adjacent elements o f t h e code.
i n d l = f i n d ( 0 <= c r & c r <= 3 ) ;
i n d 2 = f i n d ( 4 <= c r & c r <= 7 ) ; s r = c i r c s h i f t ( f c c , [O, -11); % S h i f t i n p u t l e f t by 1 l o c a t i o n .
c r ( i n d 1 ) = c r ( i n d 1 ) t 4; delta = s r - fcc;
c r ( i n d 2 ) = c r ( i n d 2 ) - 4; d = delta;
I = find(de1ta < 0);
f u n c t i o n z = minmag(c) type = conn;
%MINMAG Finds t h e i n t e g e r o f minimum magnitude i n a c h a i n code. switch type
% Z = MINMAG(C) f i n d s t h e i n t e g e r o f minimum magnitude i n a g i v e n case 4 % Code i s 4-connected
% 4- o r 8-connected Freeman c h a i n code, C. The code i s assumed t o d(1) = d(1) + 4;
% be a I - b y - n p a r r a y . case 8 % Code i s 8-connected
d(1) = d(1) + 8;
% The i n t e g e r o f minimum magnitude s t a r t s w i t h m i n ( c ) , b u t t h e r e end
% may be more than one such value. F i n d them a l l ,
I= f i n d ( c == m i n ( c ) ) ;
% and s h i f t each one l e f t so t h a t i t s t a r t s w i t h m i n ( c ) .
J = 0;
A = zeros(length(I), length(c)); function g = gscale(f, varargin)
f o r k = I; %GSCALE Scales the intensity o f the input image.
J = J t l ; % G = GSCALE(F, ' f u 1 1 8 ' ) scales t h e i n t e n s i t i e s o f F t o t h e f u l l
A(J, : ) = c i r c s h i f t ( c , [O - ( k - l ) ] ) ; % 8 - b i t i n t e n s i t y range [O, 2551. T h i s i s t h e d e f a u l t i f t h e r e i s
end
Appendix C BII M-Funtions
Appendix C M-Funtions 575
% o n l y one i n p u t argument.
%
% G = GSCALE(F, ' f u 1 1 1 6 ' ) s c a l e s t h e i n t e n s i t i e s o f F t o t h e f u l l
% 1 6 - b i t i n t e n s i t y range [O, 655351. function [X, R ] = imstack2vectors(S, MASK)
% SIMSTACK2VECTORS Extracts vectors from an image stack.
% G = GSCALE(F, 'minmax' , LOW, HIGH) s c a l e s t h e i n t e n s i t i e s of F t o % [X, R ] = imstack2vectors(S, MASK) e x t r a c t s v e c t o r s from S, which
% t h e range [LOW, HIGH]. These values must be provided, and t h e y % i s an M-by-N-by-n stack a r r a y o f n r e g i s t e r e d images o f s i z e
% must be i n t h e range [0, I ] , independently o f t h e c l a s s o f t h e % M-by-N each (see Fig. 11.24). The e x t r a c t e d v e c t o r s a r e arranged
% i n p u t . GSCALE performs any necessary s c a l i n g . I f t h e i n p u t i s o f % as t h e rows o f a r r a y X. I n p u t MASK i s an M-by-N l o g i c a l o r
% c l a s s double, and i t s values a r e n o t i n t h e range [O, I ] , then % numeric image w i t h nonzero values ( I s i f i t i s a l o g i c a l a r r a y )
% GSCALE scales i t t o t h i s range b e f o r e processing. % i n t h e l o c a t i o n s where elelnents o f S a r e t o be used i n forming X
5 % and 0s i n l o c a t i o n s t o be ignored. The number o f row v e c t o r s i n X
% The c l a s s o f t h e o u t p u t i s t h e same as t h e c l a s s o f t h e i n p u t . % i s equal t o t h e number o f nonzero elements of MASK. I f MASK i s
% omitted, a l l M*N l o c a t i o n s a r e used i n f o r m i n g X. A simple way t o
i f l e n g t h ( v a r a r g i n ) == 0 % I f o n l y one argument i t must be f . % o b t a i n MASK i n t e r a c t i v e l y i s t o use f u n c t i o n r o i p o l y . F i n a l l y , R
method = ' f u 1 1 8 ' ; % i s an a r r a y whose rows a r e t h e 2-D c o o r d i n a t e s c o n t a i n i n g t h e
else % r e g i o n l o c a t i o n s i n MASK from which t h e v e c t o r s i n S were
method = v a r a r g i n t l ) ; % e x t r a c t e d t o form X.
end
% Preliminaries.
i f strcmp(class(f), 'double') & (max(f(:)) > 1 I m i n ( f ( : ) ) < 0) [M, N, n ] = s i z e ( S ) ;
f = mat2gray(f); i f n a r g i n == 1
end MASK = true(M, N);
% Perform t h e s p e c i f i e d s c a l i n g . else
s w i t c h method MASK = MASK -= 0;
case ' f u118 ' end
g = im2uint8(mat2gray (double ( f ) ) ) ; % F i n d t h e s e t o f l o c a t i o n s where t h e v e c t o r s w i l l be kept b e f o r e
case ' f ~ 1 1 1 6 ' % MASK i s changed l a t e r i n t h e program.
g = im2uintl6(mat2gray(double(f))); [ I , J] = find(MASK);
case 'minmax' R = [ I , J];
l o w = varargin(2); h i g h = varargin(3);
i f low > 1 I l o w s 0 I h i g h > 1 I h i g h < 0 % Now f i n d X.
e r r o r ( ' P a r a m e t e r s l o w and h i g h must be i n t h e range [ 0 , I ] . ' ) % F i r s t reshape S i n t o X by t u r n i n g each s e t o f n values along t h e t h i r d
end % dimension o f S so t h a t i t betimes a row o f X. The o r d e r i s from t o p t o
i f strcmp(class(f), 'double') % bottom along t h e f i r s t column, t h e second column, and so on.
low-in = m i n ( f ( : ) ) ; Q = M*N;
high-in = m a x ( f ( : ) ) ; X = reshape(S, Q, n) ;
e l s e i f strcmp(class(f), ' u i n t 8 ' )
low-in = d o u b l e ( m i n ( f ( : ) ) ) . / 2 5 5 ; % Now reshape MASK so t h a t i t corresponds t o t h e r i g h t l o c a t i o n s
high-in = d o u b l e ( m a x ( f ( : ) ) ) . / 2 5 5 ; % v e r t i c a l l y along t h e elements o f X.
e l s e i f strcmp(c1ass ( f ) , ' u i n t l 6 ' ) MASK = reshape(MASK, Q, 1 ) ;
low-in = double(min(f(:)))./65535; % Keep t h e rows o f X a t l o c a t i o n s where MASK i s n o t 0.
high-in = double(max(f(:)))./65535; X = X(MASK, : ) ;
end
% i m a d j u s t a u t o m a t i c a l l y matches t h e c l a s s o f t h e i n p u t . function [x, y] = intline(x1, x2, yl, y2)
g = imadjust ( f , [low-in high-in] , [low high] ) ; %INTLINE Integer-coordinate line! drawing algorithm.
otherwise % [X, Y] = INTLINE(X1, X2, Y1, Y2) computes an
error('Unknown method.') % approximation t o t h e l i n e segment j o i n i n g ( X l , Y1) and
end % (X2, Y2) w i t h i n t e g e r coordinates. X I , X2, Y1, and Y2
% should be i n t e g e r s . INTLINE i s r e v e r s i b l e ; t h a t i s ,
% INTLINE(X1, X2, Y1, Y2) produces t h e same r e s u l t s as
% FLIPUD(INTLINE(X2, XI, Y2, Y I ) ) .
6 Appendix C r; M-Funtions 14 Appendix C M-Funtions 577
% Copyright 1993-2002 The Mathworks, I n c . Used w i t h permission.
% $Revision: 5.11 $ $Date: 2002/03/15 15:57:47 $ ));
phi = compute~phi(compute~eta(compute~m(F)
dx = abs(x2 - xl); %-------------------------------------------------------*------------%
dy = abs(y2 - yl); f u n c t i o n m = compute-m(F)
% Check f o r degenerate case. [M, N] = s i z e ( F ) ;
i f ( ( d x == 0) & (dy == 0 ) ) [ x , y ] = meshgrid(l:N, 1:M);
x = xl;
Y = yl; % Turn x, y, and F i n t o column v e c t o r s t o make t h e summations a b i t
return; % e a s i e r t o compute i n t h e f o l l o w i n g .
end x = x(:);
Y = y(:);
f l i p = 0; F = F(:);
i f (dx >= dy)
i f (x1 > x2) % DIP equation (11.3-12)
% Always "draw" from l e f t t o r i g h t . m.mOO = sum(F);
t = x l ; x l = x2; x2 = t ; % P r o t e c t against d i v i d e - b y - z e r o warnings.
t = y l ; y l = y2; y2 = t; i f (m.mO0 == 0)
f l i p = 1; m.mOO = eps;
end end
m = (y2 - y l ) / ( x 2 - x l ) ; % The o t h e r c e n t r a l moments:
x = (xl:x2).'; m . m l O = sum(x . * F);
y = round(y1 + m*(x - x l ) ) ; m . m O l = sum(y .* F ) ;
else m . m l l = sum(x . * y .* F ) ;
i f ( y l > y2) m.m20 = sum(x.^2 .* F ) ;
% Always "draw" from bottom t o t o p . m.m02 = sum(y.^2 . * F);
t = x l ; x l = x2; x2 = t; m.m30 = sum(x.^3 .* F);
t = y l ; y l = y2; y2 = t; m.mO3 = sum(y.*3 .* F);
f l i p = 1; m.ml2 = sum(x .* y . ^ 2 .* F ) ;
end m.m21 = sum(xmA2. * y .* F);
m = (x2 - x l ) / ( y 2 - y l ) ;
y = (yl:y2).'; f u n c t i o n e = compute-eta(m)
x = round(x1 + m*(y - y l ) ) ;
end % DIP equations (11.3-14) through (11.3-16).
% S c r o l l d a t a so t h a t t h e f i r s t p o i n t i s ( x i , y l ) .
I = f i n d ( x == x l & y == y l ) ; % Nothing t o do here.
x = c i r c s h i f t ( x , [ - ( I - I ) , 01); end
y = c i r c s h i f t ( y , [ - ( I - I ) , 01); end
end
% The same s h i f t a p p l i e s t o B.
B = c i r c s h i f t ( B , [-(I - I ) , 01); % The r e s t o f minperpo1y.m processes t h e v e r t i c e s t o
% a r r i v e a t t h e MPP.
% Get t h e Freeman c h a i n code. The f i r s t row o f B i s t h e r e q u i r e d
% s t a r t i n g p o i n t . The f i r s t element o f t h e code i s t h e t r a n s i t i o n flag = I;
% between t h e 1 s t and 2nd element o f B, t h e secon~d element o f while f l a g
% t h e code i s t h e t r a n s i t i o n between t h e 2nd and 3rd elements of B, % Determine which v e r t i c e s l i e on o r i n s i d e t h e
% and so on. The l a s t element o f t h e code i s t h e t r a n s i t i o n between % polygon whose v e r t i c e s a r e t h e Black Dots. Delete a l l
% t h e l a s t and 1 s t elements o f B. The elements o f B form a cw % other points.
% sequence (see above), so we use 'same' f o r t h e d i r e c t i o n i n I= f i n d ( v e r t i c e s ( : , 3) == 1 ) ;
% f u n c t i o n fchcode. xv = v e r t i c e s ( 1 , 1 ) ; % Coordinates o f t h e Black Dots.
code = fchcode(B, 4, ' s a m e ' ) ; yv = v e r t i c e s ( 1 , 2 ) ;
code = c o d e - f c c ; . X = v e r t i c e s ( : , 1 ) ; % Coordinates o f a l l v e r t i c e s .
Y = vertices(:, 2);
% F o l l o w t h e code sequence t o e x t r a c t t h e Black Dots, BD, (convex
I N = inpolygon(X, Y, xv, y v ) ;
% corners) and White Dots, WD, (concave c o r n e r s ) . The t r a n s i t i o n s a r e I= f i n d ( 1 N -= 0 ) ;
% as f o l l o w s : 0-to-l=WD; 0-to-3=BD; I-to-O=BD; I-to-2=WD; 2-to-l=BD; vertices = vertices(1, :);
% 2-to-3=WD; 3-to-O=WD; 3 - t o - 2 = d o t . The formula t = 2 * f i r s t - second
% g i v e s t h e f o l l o w i n g unique values f o r these t r a n s i t i o n s : -1, -3, 2, % Now check f o r any Black Dots t h a t may have been t u r n e d i n t o
% 0, 3, 1, 6, 4. These are a p p l i c a b l e t o t r a v e l i n t h e cw d i r e c t i o n . % concave v e r t i c e s a f t e r t h e previous d e l e t i o n step. D e l e t e
% The WD's are d i s p l a c e d o n e - h a l f a d i a g o n a l from t h e 8 D ' s t o form % any such Black Dots and recompute t h e polygon as i n t h e
% t h e h a l f - c e l l expansion r e q u i r e d i n t h e a l g o r i t h m . % previous s e c t i o n of code. When no more changes occur, s e t
% f l a g t o 0, which causes t h e l o o p t o t e r m i n a t e .
% V e r t i c e s w i l l be computed as a r r a y " v e r t i c e s " o f dimension n v - b y - 3 , x = vertices(:, 1);
% where nv i s t h e number o f v e r t i c e s . The f i r s t two elements o f any y = vertices(:, 2);
% row o f a r r a y v e r t i c e s a r e t h e ( x , y ) c o o r d i n a t e s o f t h e v e r t e x angles = polyangles(x, y ) ; % Find a l l t h e i n t e r i o r angles.
% corresponding t o t h a t row, and t h e t h i r d element i s 1 i f t h e I= find(ang1es > 180 & v e r t i c e s ( : , 3 ) == 1 ) ;
% v e r t e x i s convex (BD) o r 2 i f i t i s concave (WD). The f i r s t v e r t e x i f isempty ( I )
% i s known t o be convex, so i t i s b l a c k . f l a g = 0;
vertices = [ x l , y l , I ] ; else
n = 1; J = 1: l e n g t h ( v e r t i c e s ) ;
k = 1; for k = l:length(I)
f o r k = 2:length(code) K = f i n d ( J -= I ( k ) ) ;
i f code(k - 1) -= code(k) J = J(K);
n = n + l ; end
t = 2*code(k-I) - c o d e ( k ) ; % t = value o f formula. vertices = vertices(J, :);
ift == -3 1 t == 2 1 t == 3 1 t == 4 % Convex: Black Dots. end
v e r t i c e s ( n , 1:3) = [ x ( k ) , y ( k ) , I ] ; end
e l s e i f t == -1 I t == 0 I t == 1 I t == 6 % Concave: White Dots.
if t == -1 % F i n a l pass t o d e l e t e t h e v e r t i c e s w i t h angles o f 180 degrees.
v e r t i c e s ( n , 1 :3) = [ x ( k ) - c e l l s i z e , y ( k ) - c e l l s i z e , 2 1 ; x = vertices(:, 1);
e l s e i f t==O y = vertices(:, 2);
v e r t i c e s ( n , 1 :3) = [ x ( k ) t c e l l s i z e , y ( k ) - c e l l s i z e , 2 1 ; angles = polyangles ( x , y ) ;
elseif t==l I = find(ang1es -= 180);
v e r t i c e s ( n , 1:3) = [ x ( k ) + c e l l s i z e , y ( k ) + c e l l s i z e , 2 ] ; % V e r t i c e s o f t h e MPP:
else x = vertices(1, 1);
v e r t i c e s ( n , 1:3) = [ x ( k ) - c e l l s i z e , y ( k ) + c e l l s i z e , 2 ] ; y = vertices(1, 2);
end
i Appendix C a M-Funtions Appendix C 3 M-Funtions 587
% Convert t o columns. np = n p - 1;
x = x(:); 3%
Bc
end
Y = y(:); - % Compute parameters.
% Preliminary calculations. . ~f n a r g l n == 1
L = length(x); xO = round(sum(b(:, 1 ) ) l n p ) ; % Coordinates o f t h e centroid.
xnoise = rand(L, 1 ) ; yo = round(sum(b(:, 2 ) ) l n p ) ;
ynoise = rand(L, 1 ) ; e l s e i f n a r g i n == 3
xdev = npix*xnoise.*sign(xnoise - 0 . 5 ) ; xO = varargin(1);
ydev = npix*ynoise.*sign(ynoise - 0.5); yo = varargin(21;
else
% Add n o i s e and round.
e r r o r ( ' 1 n c o r r e c t number o f i n p u t s . ' ) ;
xn = round(x + xdev);
end
yn = round(y + ydev);
% S h i f t o r i g i n o f coord system t o (xO, y o ) ) .
% A l l p i x e l l o c a t i o n s must be no l e s s than 1.
b ( : , I ) = b(:, I)- xo;
xn = max(xn, 1 ) ;
b ( : , 2) = b ( : , 2 ) - YO;
yn = max(yn, 1 ) ;
% Convert t h e coordinates t o p o l a r . But f i r s t have t o c o n v e r t t h e
% g i v e n image coordinates, ( x , y ) , t o t h e c o o r d i n a t e system used by
% MATLAB f o r conversion betweell C a r t e s i a n and p o l a r c o r d i n a t e s .
% Designate these coordinates by (xc, y c ) . The two c o o r d i n a t e systems
function [st, angle, xO, y o ] = signature(b, varargin) % a r e r e l a t e d as f o l l o w s : xc = y and yc = -x.
%SIGNATURE Computes t h e signature of a boundary.
xc = b ( : , 2 ) ;
% [ST, ANGLE, XO, YO] = SIGNATURE(B) computes t h e yc = -b(:, 1 ) ;
% s i g n a t u r e o f a g i v e n boundary, B, where B i s an n p - b y - 2 a r r a y [theta, rho] = cart2pol(xc, yc);
% (np > 2) c o n t a i n i n g t h e (x, y ) c o o r d i n a t e s o f t h e boundary
% ordered i n a clockwise o r counterclockwise d i r e c t i o n . The % Convert angles t o degrees.
% amplitude o f t h e s i g n a t u r e as a f u n c t i o n o f i n c r e a s i n g ANGLE i s theta = theta.*(180/pi);
% output i n ST. (X0,YO) a r e t h e c o o r d i n a t e s o f t h e c e n t r o i d o f t h e % Convert t o a l l nonnegative angles.
% boundary. The maximum s i z e o f a r r a y s ST and ANGLE i s 360-by-1, j = t h e t a == 0; % Store t h e i n d i c e s o f t h e t a = 0 f o r use below.
%
%
i n d i c a t i n g a maximum r e s o l u t i o n o f one degree. The i n p u t must be
a o n e - p i x e l - t h i c k boundary obtained, f o r example, by u s i n g t h e
theta = theta.*(0,5*abs(l + s i g n ( t h e t a ) ) ) . . .
- 0.5*(-1 + s i g n ( t h e t a ) ) . * ( 3 6 0 + t h e t a ) ;
% f u n c t i o n boundaries. By d e f i n i t i o n , a boundary i s a closed curve. t h e t a ( j ) = 0; % To preserve t h e 0 values.
%
% [ST, ANGLE, XO, YO] = SIGNATURE(B) computes t h e s i g n a t u r e , u s i n g temp = t h e t a ;
% t h e c e n t r o i d as t h e o r i g i n o f t h e s i g n a t u r e v e c t o r . % Order temp so t h a t sequence s t a r t s w i t h t h e s m a l l e s t angle.
0, % T h i s w i l l be used below i n a check f o r m o n o t o n i c i t y .
% [ST, ANGLE, XO, YO] = SIGNATURE(B, XO, YO) computes t h e boundary I = f i n d ( t e m p == min(temp));
% u s i n g t h e s p e c i f i e d (XO, YO) as t h e o r i g i n of t h e s i g n a t u r e % S c r o l l up so t h a t sequence s t a r t s w i t h t h e s m a l l e s t angle.
% vector. % Use I ( 1 ) I n case t h e min i s nlot unique ( i n t h i s case t h e
% Check dimensions o f b. % sequence w i l l not be monoton~canyway).
[np, nc] = s i z e ( b ) ; temp = c i r c s h i f t ( t e m p , [ - ( I ( 1 ) - I ) , 0 1 ) ;
if(np nc I nc -= 2 ) % Check f o r monotoniclty, and i s s u e a warning i f sequence
e r r o r ( ' B must be o f s i z e n p - b y - 2 . ' ) ; % i s n o t monotonic. F i r s t determine i f sequence i s
end % cw o r ccw.
% Some boundary t r a c i n g programs, such as boundaries.m, end where k1 = abs(temp(1) - t e m p ( 2 ) ) ;
% they started, r e s u l t i n g i n a sequence i n which t h e c o o r d i n a t e s k2 = abs(temp(1) - temp(3));
% o f t h e f i r s t and l a s t p o i n t s a r e t h e same. I f t h i s i s t h e case, i f h2 > k l
% i n b, e l i m i n a t e t h e l a s t p o i n t . sense = I; % ccw
i f i s e q u a l ( b ( 1 , : ) , b(np, : ) ) e l s e l f k2 < k l
b = b(1:np - 1 , : ) ; sense = -1; % cw
588 Appendix C @ M-Funtions Appendix C 3 M-Funtions 589
else % Obtain the centered spectrum, S, o f f . The v a r i a b l e s o f S are
w a r n i n g ( [ ' T h e f i r s t 3 p o i n t s i n B do n o t form a monotonic ' ... % (u, v ) , running from 1 :M and 1 :N, w i t h t h e c e n t e r (zero frequency)
'sequence.']); % a t [MI2 + 1, N/2 + 1 ] (see Chapter 4 ) .
end S = fftshift(fft2(f));
% Check t h e r e s t o f t h e sequence f o r m o n o t o n i c i t y . Because S = abs(S);
% t h e angles a r e rounded t o t h e n e a r e s t i n t e g e r l a t e r i n t h e [M, N] = size(S) ;
% program, o n l y d i f f e r e n c e s g r e a t e r than 0.5 degrees a r e x0 = MI2 + 1;
% considered i n t h e t e s t f o r m o n o t o n i c i t y i n t h e r e s t o f y0 = N/2 + 1;
% t h e sequence.
% Maximum r a d i u s t h a t guarantees a c i r c l e centered a t (xO, yo) t h a t
f l a g = 0;
% does not exceed t h e boundaries o f S.
f o r k = 3:length(temp) - 1
d i f f = sense*(temp(k + 1 ) - t e m p ( k ) ) ;
rmax = min(M, N ) / 2 - 1;
i f d i f f < -.5 % Compute srad.
f l a g = I; srad = zeros(1, rmax);
end srad(1) = S(x0, y o ) ;
end f o r r = 2:rmax
i f flag [xc, y c ] = h a l f c i r c l e ( r , xO, yo);
warning('Ang1e.s do n o t form a monotonic seqluence.'); s r a d ( r ) = sum(S(sub2ind(size(S), xc, y c ) ) ) ;
end end
% Round t h e t a t o 1 degree increments. % Compute sang.
theta = round(theta); [xc, y c ] = h a l f c i r c l e ( r m a x , xO, yo);
sang = zeros(1, l e n g t h ( x c ) ) ;
% Keep t h e t a and r h o t o g e t h e r .
f o r a = l:length(xc)
tr = [theta, rho];
[ x r , y r ] = r a d i a l ( x 0 , yo, x c ( a ) , y c ( a ) ) ;
% Delete d u p l i c a t e angles. The unique o p e r a t i o n sang(a) = sum(S(sub2ind(size(S), x r , y r ) ) ) ;
% a l s o s o r t s t h e i n p u t i n ascending o r d e r . end
[w, u, V ] = u n i q u e ( t r ( : , 1 ) ) ;
% Output t h e l o g o f t h e spectrum f o r e a s i e r viewing, scaled t o t h e
t r = t r ( u , : ) ; % u i d e n t i f i e s t h e rows kept by unique.
% range [0, 1 ] .
% I f t h e l a s t angle equals 360 degrees p l u s t h o f i r s t S = mat2gray(log(l + S ) ) ;
% angle, d e l e t e t h e l a s t angle. % . . - . . . . . . . . . . . - . - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -%- - - - - - - - - - - - - - - - - - -
i f t r ( e n d , 1 ) == t r ( 1 ) + 360
f u n c t i o n [xc, yc] = h a l f c i r c l e ( r , xO, yo)
tr = t r ( 1 : e n d - 1, : ) ; Computes t h e i n t e g e r coordinates o f a h a l f c i r c l e o f r a d i u s r and
%
end
% center a t (x0,yO) u s i n g one degree increments.
% Output t h e angle values. %
angle = t r ( : , 1 ) ; % Goes from 91 t o 270 because we want t h e h a l f c i r c l e t o be i n t h e
% The s i g n a t u r e i s t h e s e t o f values o f r h o corresponding
% region defined by t o p r i g h t and t o p l e f t quadrants, i n t h e
% t o t h e angle values.
% standard image coordinates.
s t = t r ( : , 2);
theta=91:270;
f u n c t i o n [srad, sang, S] = s p e c x t u r e ( f ) theta = theta*pi/l80;
%SPECXTURE Computes s p e c t r a l t e x t u r e o f an imagle. [xc, yc] = pol2cart(theta, r ) ;
% [SRAD, SANG, S] = SPECXTURE(F) computes SRPtD, t h e s p e c t r a l energy xc = r o u n d ( x c ) ' + xO; % Column v e c t o r .
% distribution as a f u n c t i o n o f r a d i u s from t h e c e n t e r o f t h e yc = r o u n d ( y c ) ' + yo;
% spectrum, SANG, t h e s p e c t r a l energy d i s t r i b ~ u t i o nas a f u n c t i o n o f
%--....-.-.......-.----------------.----------------------.----------%
% angle f o r 0 t o 180 degrees i n increments o f 1 degree, and S =
% l o g ( 1 + spectrum o f f ) , normalized t o t h e range [ 0 , 11. The f u n c t i o n [ x r , y r ] = r a d i a l ( x 0 , yo, x, y ) ;
% maximum v a l u e o f r a d i u s i s min(M,N), where M and N a r e t h e number % Computes t h e coordinates o f a s t r a i g h t l i n e segment extending
% o f rows and columns o f image ( r e g i o n ) f . Thus, SRAD i s a row % from (xO, yo) t o ( x , y ) .
0
,
% v e c t o r o f l e n g t h = (min(M, N ) / 2 ) - 1; and SANG i s a row Vector of
% l e n g t h 180.
5 Appendix C @ M-Funtions Appendix C s M-Funt~ons 591
else
X e r r o r ( ' 1 n p u t must be a boundary o r a b i n a r y image'.)
f u n c t i o n [B, t h e t a ] = xZmajoraxis(A, B, t y p e ) end
%XPMAJORAXIS A l i g n s c o o r d i n a t e x w i t h t h e major aixis o f a region. % Major a x i s i n v e c t o r form.
% [B2, THETA] = X2MAJORAXIS(A, B, TYPE) a l i g n s t h e X - c o o r d i n a t e ~ ( 1 =) A(2, 1 ) - A(1 , 1 ) ;
% a x i s w i t h t h e major a x i s o f a r e g i o n o r boundary. The y - a x i s i s v ( 2 ) = A(2, 2) - A(1, 2 ) ;
% p e r p e n d i c u l a r t o t h e x - a x i s . The rows o f 2 - b y - 2 m a t r i x A a r e t h e
v = v ( : ) ; % v i s a c o l vector
% c o o r d i n a t e s of t h e two end p o i n t s o f t h e major a x i s , i n t h e form
% A = [ x l y l ; x2 y 2 ] . On i n p u t , B i s e i t h e r a b i n a r y image ( i . e . , % U n i t v e c t o r along x - a x i s .
% an a r r a y o f c l a s s l o g i c a l ) c o n t a i n i n g a sing1.e region, o r i t i s u = [ l ; 01;
% an n p - b y - 2 s e t o f p o i n t s r e p r e s e n t i n g a (connected) boundary. I n % F i n d angle between major a x i s and x - a x i s . The angle i s
% t h e l a t t e r case, t h e f i r s t column o f B must represent % g i v e n by acos o f t h e i n n e r product o f u and v d i v i d e d by
% x - c o o r d i n a t e s and t h e second column must represent t h e % t h e product of t h e i r norms. Because t h e i n p u t s a r e image
% corresponding y - c o o r d i n a t e s . On output, B c o n t a i n s t h e same data, % p o i n t s , they a r e i n t h e f i r s t quadrant.
% as t h e i n p u t , b u t a l i g n e d w i t h t h e major a x i s . I f t h e i n p u t i s an nv = norm(v);
% image, so i s t h e o u t p u t ; s i m i l a r l y t h e outpul: i s a sequence o f nu = norm(u);
% c o o r d i n a t e s ift h e i n p u t i s such a sequence. Parameter THETA i s t h e t a = acos(u'*v/nv*nu);
% t h e i n i t i a l angle between t h e major a x i s and t h e x - a x i s . The i f theta > pi12
% o r i g i n o f t h e x y - a x i s system i s a t t h e bottorn l e f t ; t h e x - a x i s i s theta = -(theta - pi12);
% t h e h o r i z o n t a l a x i s and t h e y - a x i s i s t h e v e r t i c a l . end
0
,
t h e t a = t h e t a * l E O / p i ; % Convert angle t o degrees.
% Keep i n mind t h a t r o t a t i o n s can i n t r o d u c e r o u n d - o f f e r r o r s when
% t h e d a t a a r e converted t o i n t e g e r coordinates, which i s a % Rotate by angle t h e t a and crop t h e r o t a t e d image t o o r i g i n a l s i z e .
% requirement. Thus, postprocessing ( e . g . , w i t h bwmorph) o f t h e B = imrotate(6, theta, ' b i l i n e a r ' , 'crop');
% o u t p u t may be r e q u i r e d t o reconnect a bounda~ry. % Ift h e i n p u t was a boundary, r e - e x t r a c t i t .
% Preliminaries. i f strcmp(type, 'boundary')
i f islogical(8) B = boundaries(8);
type = ' r e g i o n ' ; B = B(1);
e l s e i f s i z e ( B , 2) == 2 % S h i f t so t h a t c e n t r o i d o f t h e e x t r a c t e d boundary i s
type = 'boundary'; % approx equal t o t h e c e n t r o i d o f t h e o r i g i n a l boundary:
[M, N ] = s i z e ( B ) ; B ( : , 1) = B ( : , 1) - min(B(:, 1 ) ) + c ( 1 ) ;
i f M < N B ( : , 2) = B ( : , 2) - min(B(:, 2 ) ) + c ( 2 ) ;
e r r o r ( ' B i s boundary. I t must be o f s i z e np-by-2; np > 2 . ' ) end
end
% Compute c e n t r o i d f o r l a t e r use. c i s a 1 - b y - 2 v e c t o r .
% I t s 1 s t component i s t h e mean o f t h e boundary i n t h e x - d i r e c t i o n .
% The second i s t h e mean i n t h e y - d i r e c t i o n .
c ( 1 ) = r o u n d ( ( m i n ( B ( : , 1 ) ) + max(B(:, 1 ) ) 1 2 ) ) ;
c ( 2 ) = round((min(B(:, 2 ) ) + max(B(:, 2 ) ) / 2 ) ) ;
% I t i s p o s s i b l e f o r a connected boundary t o develop s m a l l breaks
% a f t e r r o t a t i o n . To p r e v e n t t h i s , t h e i n p u t boundary i s f i l l e d ,
% processed as a r e g i o n , and t h e n t h e boundary i s r e - e x t r a c t e d . T h i s
% guarantees t h a t t h e o u t p u t w i l l be a connected boundary.
m = max(size(0));
% The f o l l o w i n g image i s o f s i z e m-by-m t o ma~ke sure t h a t t h e r e
% t h e r e w i l l be no s i z e t r u n c a t i o n a f t e r r o t a t i o n .
B = bound2im(B,m,m);
B = imfill(8,'holes');
8 Bibliography 595
Floyd, R. W. and Steinberg, L. [1975]. "An Adaptive Algorithm for Spatial
Gray Scale," International Synzposi~lnzDigest of Technical Papers. Society
for Information Displays. 1975.,p. 36.
Gardner. M. [1970]. "Mathemati~calGames: The Fantastic Combinations of
John Conway's New Solitaire (Game 'Life', " Scientific American, October,
pp. 120-123.
Gardner, M. [1971]. "Mathematical Games On Cellular Automata, Self-
Reproduction, the Garden of Eden, and the Game 'Life', Scientific Amer-
"