0% found this document useful (0 votes)
411 views302 pages

2003 - Digital Image Processing Using MATLAB - R.C. Gonzalez, R.E. Woods, S.L. Eddins

Uploaded by

hjjura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
411 views302 pages

2003 - Digital Image Processing Using MATLAB - R.C. Gonzalez, R.E. Woods, S.L. Eddins

Uploaded by

hjjura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 302
saewtindeererntinainiomasiasctn inthis Contents 18 24 22 23 24 25 26 27 28 Preface xi Acknowledgments xii About the Authors xiii Introduction 1 Preview 1 Background 1 What Is Digital Image Processing? 2 Background on MATLAB and the Image Processing Toolbox 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 Desktop 7 1.7.2. Using the MATLAB Editor to Create M-Files 9 173. Getting Help 9 L7A_ Saving and Retrieving a Work Session 10 How References Are Organized in the Book 11 Summary 1 Fundamentals 12 Preview 12 Digital Image Representation 12 21.1 Coordinate Conventions 13 21.2 Images as Matrices 14 Reading Images 14 Displaying Images 16 Writing Images 18 Data Classes 23 Image Types 24 26.1 Intensity Images 24 262 Binary images 25 263 ANoteon Terminology 25 Converting between Data Classes and Image Types 25 27.1 Converting becween Data Classes 25 27.2 Converting between Image Classes and Types 26 Anay Indexing 30 281 VectorIndexing 20° 282 Matrix Indexing 3 283 Selecting Array Dimensions 37 29 2.10 3a 32 33 34 4a 42 43 4a a3 Some Important Standard Arrays 37 Introduction to M-Function Programming 38 2101 M-Files 38 2102 Operators 40 2103 Flow Control 49 210.4 Code Optimization 55 2105 Interactive /O 59 2106 A Briel Introduction to Cell Arrays and Structures 62 Summary 64 Intensity Transformations and Spatial Filtering 65 Preview! 65 Background 65 Intensity Transformation Functions 66 S21" Function inadjust 66 332. Lopanittmic and Contrast Stetching Transformations 68 333. Some Uuty MoFunctions for intensity Transformations 70 Histogram Processing and Function Potting 7 531 “Generating and Poting image Histograms 76 332 Histogram Equalization 81 333. Histogram Matching (Specification). 84 Spatial Filtering 89 SHI Linear Spatial Fitering. 69 342 Nonlinear Spatial Filtering 96 Image Processing Toolbox Standard Spatial Filters 99 B51" Linear Spatal Filters 99 352 Nonlinene Spatial ites 104 Summary. 107 Frequency Domain Processing 108 Preview 108 The 2-D Discrete Fourier Transform 108 Computing and Visualizing the 2-D DFT in MATLAB 112 Filtering in the Frequency Domain 115 [31 fundamental Concepts 3 432 Basie Steps in DFT Filtering 121 $33 Am Mefunction for Filtering in the Frequency Domain 122 Oblaining Frequency Domain Filters from Spatial Filters 122 Ceneatigg Fives Directly inthe Trequency Doma 127 sat Cheating Meshgrid Arrays for Ute in Implementing Filters inthe Frequency Domain 128 Towpass Frequency Domain Fters_ 129 WWieerame and Surface Poting, 132 46 5a 52 53 5 55 56 37 38 59 510 sit 61 62 63 6a = Contents vii Sharpening Frequency Domain Filters 136 461 Basic Highpass Filtering 136 462 High-Frequency Emphasis Filtering 138 Summary 140 Image Restoration 141 Preotwo 101 ‘A Model ofthe Image Degradation Restoration Process 142 Noise Models 133 B21" Adding Noe with Function nose 143 522. Goering Spatial Random Note with a Specified Disuibuton 1s 523. Period Nese 150 524. Estimating Noise Parameters 153 Restoration inthe Presence of Noise Only—Spatial Filtering. 158 SS Spatial Nose Filters 159 532. Aaptve Spatial ters 168 Periodic Nese Reduction by Frequency Domain Filtering, 166 Modeling the Degradation Fancion I6e Ditect Inverse Filtering 169 Wiener Filtering 170 Conrtrsned Lest Squares (Regularized) Filtering 173 erative Nonlinear Restoration Using the Luy-Richandson Algorithm 176 Bind Deconvolution 179 Geometic Transformations and Image Registration 182 S11 Geometric Spatal Tarsformatons “182 5112 Applying Spatal Transformation to Images 187 Bits image Reyetraton 1 Summary "193 Color Image Processing 194 Preview 194 Color Image Representation in MATLAB 194 GL RGBlmages 194 612 Indexed images 197 61.3 IPT Functions for Manipulating RGB and indexed Images 199 Converting to Other Color Spaces 204 621 NTSCColor Space 204 622 TheYCbCrColor Space 205 62% The HSV Calor Spare 205 624 The CMY and CMYK Color Spaces 206 625 The HSIColor Space 207 The Basics of Color Image Processing 215 Color Transformations 216 «Contents 65. 66 7a 72 73 Spatial Filtering of Color Images 227 65:1 Color Image Smoothing 227 65.2 Color Image Sharpening 230 Working Directly in RGB Vector Space 251 66.1 Color Edge Detection Using the Gradient 232 662. Image Segmentation in RGB Vector Space 237 Summary 241 Wavelets 242 Preview 242 Background 242 The Fast Wavelet Transform 245 7.2.1 FWTs Using the Wavelet Toolbox 246 72.2 FWTswithout the Wavelet Toolbox 252 Working with Wavelet Decomposition Structures 259 7.3.1 Editing Wavelet Decomposition Coetficients without the Wavelet Toolbox 262, 7.3.2. Displaying Wavelet Decomposition Coefficients 266 The Inverse Fast Wavelet Transform 271 Wavelets in Image Processing 276 Summary 281 Image Compression 282 Proviso 282 Background 283 Coding Redundancy 286 S21 Huffman Coges 289 322 Hulfman Encoding 295 823. Huffman Decoding. 301 Interpixel Redundancy "309 Paychovisual Redundancy 315 JPEG Compression 317 B51 yee. 38 852 JPEG2000 305 Summary 338 Morphological Image Processing 334 Preview 334 Preliminaries 335 9.141 Some Basic Concepts from Set Theory 385 91.2 Binary Images, Sets, and Logical Operators 337 Dilation and Erosion 337 921 Dilation 338 92.2 Structuring Element Decomposition 341 923 The strel Function 341 924 Erosion 345 93 94 95. 96 10 10a 102 103 4 105 i ua 8 Contents ix Combining Dilation and Erosion 347 93.1 Opening and Closing 347 93.2 The Hittor-Miss Transformation 350 933 Using Lookup Tables 353 934 Function banorpn 356 Labeling Connected Components 359 Morphological Reconstruction 362 95.1 Opening by Reconstruction 363 952 Filling Holes 365 95.3. Clearing Border Objects 366 Gray-Scale Morphology 366 9.6.1 Dilation and Erosion 366 92 Opening and Closing 369 9.63 Reconstruction 374 Summary 377 Image Segmentation 378 Previtw 373 Point Line, and Edge Detection 373 1041 Point Detection 979 1012 Line Detection 381 1013 Edge Detection Using Function edge 384 Line Detection Using the Hough Transform 33 1021 Hough Transform Peak Detection 399 1022 Hough Transform Line Detection and Linking 401 Thresholding 404 1031 Global Tresholding 405 1032 Local Thresholding 407 Regioa-Based Segmentation 407 1041 Basic Formulation 407 1042 Region Growing "408 1043 Region Spliting and Merging 412 Segmentation Using the Watershed Transform 417 1051 Watershed Segmentation Using the Distance Transform 418 105.2 Watershed Segmentation Using Gradients 420 1053 Marker-Contolled Watershed Segmentation 42 Summary 425 Representation and Description 426 Proview 426 Background 425 1123 Cell Arays and Structures 427 112 Some Advitional MATLAB and IPT Functions Used inthis Chapter 422 113 Some Basie Uilty M-Functons 483 Contents| 112 Representation 436 3121 Chain Codes 436 11.22 Polygonal Approximations Using Minimum-Perimeter Polygons 439 11.23 Signatures 449 112.4 Boundary Segments 452 1125 Skeletons 453 113 Boundary Descriptors 455 113.1 Some Simple Descriptors’ 455 3132 Shape Numbers 456 11.33 Fourier Descriptors 458 134 Statistical Moments 462 1A Regional Descriptors $63, ALA Function regionprops 463, 1142 Texture 464 i143 Moment Invariants 470 115 Using Principal Components for Description 474 Summary 483 12 object Recognition 484 | 122 Background 484 122 Computing Distance Measures in MATLAB 485 123 Recognition Based on Decsion-Theoretic Methods 488 123 Forming Patern Verto 488 1232 ator Matching Using MinimarDistance Classifiers 489 1233 Matching by Correlation 50 1234 Opaimum Statistical Casiiers $92 1235 Adaptive Leaning Systems, 498 Cy rene meer TLD Working th Strings in MATLAB 499 1242 String Matching 508 pasneach Appendix A Function Summary 514 Aupentic Bice and MATLAB Graphical User Interfaces 527 Aapentx C M-Functions 552 Bibliography 594 Index 597 Preface Solutions to problems in the field of digital image processing generally requite extensive experimental work involving software simulation and testing wit large sets of sample images Although algorithm development typically is based on theoretical ‘underpinnings. the actual implementation of these algorithms almost always requires parameter estimation and, requenty algorithm revision and comparison of candidte Solutions Thus selection ofa flexible, comprehensive, and well-documented software ‘evelopment envisonment isa key factor that has important implications in the cost, ‘development time, and portability of image processing solutions. In spite of its importance, surprisingly litle has been writen on this aspect of the field in the fom of textbook material dealing with both theoretical principles and sof- ‘ware implementation of digital image processing concepts This book was written for just this purpose. Is main objective so provide foundation for implementing image processing algorithms using modem software tools complementary objective was to prepare a book that is selcontained and easly readable by individuals witha basic buckground in digital image processing, mathematical analysis, and computer pro- gramming all at a level typical of that found in a juniorsenior eurticulum ina techai- ‘al discipline Rudimentary knowledge of MATLAB also i desirable. ‘To achieve these objectives, we felt that two key ingredients were needed. The frst was to select image processing material that is representative of material cov- cred in formal course of instruction inthis field, The second was to select soft ‘ware tools that are well supported and documented, and which have a wide range of applications inthe “real” world. “Tomect the fst objective most ofthe theoretical eonceptsin the following chapters were selected fom Digital Image Processing by Gonzsez and Woods, which has been the choie introductory textbook used by educators all ver the world for over two decades The software tools selected ate from the MATLAB Image Processing Toolbox {OPT).which similarly occupies a postion of eminence in both education and industrial pplcations A basic strategy followed in the preparation ofthe book was to provide a seamles integration of wellestablished theoretical concepts and their implementation wing state-of the-art software tools ‘The book is organized along the same lines as Digial Image Processing. In this way, the reader has easy access to a more detailed treatment ofall the image processing ‘concepts discused here, 5 well as an up-to-date set of references for further reading. Following this approach made it possible to present theoretical mate ina sucinct ‘manner and thus we were able to maintain afocuson the software implementation as- ects of image processing problem solutions Because it works in the MATLAB com- Puting environment, the Image Processing Toolbox offers some significant advantages, ot ony in the breadth ofits computational took, but also because it is supported under most operating systems in se today. nique feature of this book sits empia- sis on showing How to develop new code o enhance existing MATLAB and TPT func- tionality. This i an important feature in an area Such as image processing, which, as ‘oted ale, is characterized by the need for extensive algorithm development and experimental work ‘Aller an introduction to the fundamentals of MATLAB functions and program- ming, the book proceeds to address the mainstream areas of image processing. The 1 Preface major areas covered include intensity transformations, linear and nonlinear spatial fk tering, filering in the frequency domain, image restoration and registration, color image processing wavelets image data compression, morphological image prosesing, image Seementation, region and boundary representation and description, and object recognition This material is complemented by numerous illustrations of how to solve jmage processing problems using MATLAB and IPT functions In cases where a fune- tion cid not exist, a new function was written and documented as part ofthe instruc- tional focus ofthe book, Over 60 new functions are included in the following chapters. “These functions inerease the scope of IPT by approximately 35 percent and also serve the important purpose of futher illustrating how to implement new image processing, software solutions “The material is presented in textbook format, not a a software manual. Although the book is self-contained, we have established a companion Web site (See Section 13) signed to provide support in a number of areas For students following a forrmal ‘course of study or individuals embarked on a program of set study, the site contains tutorials and reviews on background material, as wel as projects and image databases, ‘including all images in the book, For instructors, te site contains classroom presenta~ tion materials that inelude PowerPoint slides ofall the images and graphics used inthe book, Individuals already familiar with image processing and IPT fundamentals will find the site a useful place for up-to-date references new implementation techniques, ‘and a host of other support material not easly ound elsewhere. Al purchasers ofthe ‘book are eligible to download executable files of all the new functions developed in the text. ‘Asis true of most writing efforts of this nature, progress continues after work on the _manuscript stops For ths reason, we devoted significant effort to the selection of ma- terial that we believe is Findamental, and whose value is likely to remain applicable in 2 rapidly evolving body of knowledge. We trust that readers ofthe book will benefit from this effort and thus find the material imely and usefl in their work. Acknowledgments ‘We are indebted to a numberof individuals in academic circles as wel asin industry and government who have contributed tothe preparation ofthe book. Their contribu- tions have been important in so many different ways that we find it dificult to ac- Knowledge them in any other way but alphabetically. We wish to extend our appreciation to Mongi A. Abid, Peter J.Acklam, Serge Beucher, Emesto Bribesca, Michael W, Davidson, Courtney Esposito, Naomi Fernandes, Thomas R. Gest, Roger Head, Brian Johnson, Lisa Kempler, Roy Lurie, Ashley Mohamed, Joseph E. Paseente, David R. Pickens, Edgardo Felipe Riveron, Michael Robinson, Loren Shure, Jack Sklanski, Sally Stowe, Craig Watson, and Greg Wolodkin, We also wish to ac knowledge the organizations cite inthe captions of many of th figures inthe book fr their permission to use that materia 1 oUt tne go to-Tom Robins, Rose Kemi, Ace Dworkin, Xohong Zhu, Bruce Kenselaa, and Jayne Conte at Prentice Hal for their commitment to excellence in all aspects of the production ofthe book. Their creativity assistance, and patience are trly appreciated. Ragas. C GonzaLez Ricuaro E.Wooos ‘Steven L. EDDINS About the Authors Rafael C. Gonzalez R.C. Gonzalez received the BSE.E, degree from the University of Mian ia 1965 and the MEE and Ph.D. degrees in electrical engincering from the University of Florida, Gainesville, in 1967 and 1970, respectively. He joined the Electrical and Computer Engineering Department at the University of Tennessee, Knowvile (UTK) in 1970, where he became Associate Professor in 1973, Professor in 1978, and Distinguished Service Professor in 1984, He served as Chairman of the de- partment from 1994 through 1997. He is currently a Professor Emeritus of Elect al ond Computer Engineering at UTK. He isthe founder of the Image & Patter Analysis Laboratory and the Robot- ies & Computer Vision Laboratory atthe University of Tennessee. He also found cd Perceptics Corporation in 1982 and was its president until 1992, The lat three years of this period were spent under a fulltime employment conteact with West inghouse Corporation, who acquired the company in 1989, Under his direction Perceptics became highly successful in image processing, computer vision, laser disk storage technologies In its initial ten years Perceptics introduced # se- rics of innovative products, including: The world’s first commercally-available computer vision system for automatically reading the license plate on moving ve- hile: a series of large-scale image processing and archiving systems used by the US. Navy at six different manufacturing sites throughout the country to inspect the rocket motors of missiles in the Trident Il Submarine Program: the market leading family of imaging boards for advanced Macintosh computers: and a line of trillion-byte laser disk products. He isa frequent consultant to industry and government inthe areas of pattern recognition, image processing, and machine earning. His academic honors for work in these fields include the 1977 UTK College of Engineering Faculty Achievement ‘Awardthe 1978 UTK Chancellor's Research Scholar Award: the 1980 Magnavox En- tineering Professor Award: and the 1980 M. E. Brooks Distinguished Professor ‘Awatd In 1981 he became an IBM Professor a the University of Tennesse and in 1984 he was named a Distinguished Service Professor there. He was awarded Di tinguished Alumnus Award by the University of Miami in 1985, the Phi Kappa Phi Scholar Award in 1986, and the University of Tennessee's Nathan W. Dougherty ‘Award for Excellence in Engineering in 1992. Honors fr industrial ascompishment include the 1987 IEEE Outstanding Engineer Award for Commercial Development in Tennessee: the 1988 Albert Rose National Award for Excellence in Commercial lenage Processing the 1589 B, Otto Whecley Award for Excllence in Technology Transter, the 1989 Coopers and Lybrand Entrepreneur of the Year Award; the 1992 IEEE Region 3 Outstanding Enginzer Award: and the 1993 Automated Imaging As- sociation National Award fr Technology Development. ir Gonzalez is author oF co-author of over 100 technical articles, two edited books. and five textbooks in the fields of pattern recognition, image processing, And robotics, His books are used in over 500 universities and research institutions throughout the world. He is listed inthe prestigious Marquis Who’ Who in Amer- ‘ea, Marquis Who's Who in Engineering, Marquis Who's Who in the World. and in 10 other national and intemational biographical citations. He s the co-holder of two US. Patents, and has been an associate editor of the [EEE Transactions on 4 About the Authors “Systems, Man and Cybernetic and the Intemasional Journal of Computer and In- Jovmation Sciences. He is 4 member of numerous professional and honorary soci- ‘ties, including Tau Beta Pi, Phi Kappa Phi, Eta Kappa Nu, and Sigma Xi, He is 8 Fellow of the IEEE. Richard E. Woods Richard E. Woods earned his BS, MS. and Pp.D. degrees in Flectrical Engineer- ing ftom the University of Tennessee, Knoxville. His professional experiences range from entrepreneurial to the more traditional academic, consulting, govemn- ‘meatal, and industrial pursuits Most recently, be founded MedData Interactive, a high technology company specializing in the development of handheld computer systems for medical applications, He was also a founder and Vice President of Per- ‘eptics Corporation, where he was responsible forthe development of many ofthe company’s quantitative image analysis and autonomous decision making products, rior to Perceptics and MedData, Dr. Woods was an Assistant Protessor of Elec ‘vical Engineering and Computer Science at the University of Tennessee and prior to that, computer applications engineer at Union Carbide Corporation. As a consul- tant he has been involved inthe development of a aumber of special-purpose digital processors for a variety of space and military agencies including NASA. the Ballistic Missle Systems Command, and the Oak Ridge National Laboratory. Dr. Woods has published numerous articles related to digital signal processing ‘and is co-author of Digital Image Processing, the leading text in the field. He is a member of several professional societies, including Tau Beta Pi, Phi Kappa Phi, and the IEEE, In 1986, he was recognized as a Distinguished Engineering Alum- rus of the University of Tennessee. Steven L. Eddins Steven L. Eddins is development manager of the image processing group at The ‘MathWorks, In. He fed the development of several versions of the company’s Image Processing Toolbox, His professional interests include building software tools that are based on the latest research in image processing algorithms, and that have a broad range of scientific and engineering applications. Prior to joining The MathWorks, Inc. in 1993, Dr, Eddins was on the faculty of the Electrical Engineering and Computer Science Department atthe University of Illinois Chicago. There he taught graduate and senior-level classes in digital image processing, computer vision, pattern recognition, and filter design, and he per- formed research in the area of image compression, De. Eddins holds a BE.E. (1986) and a Ph.D. (1990), both in cletrcal engineering from the Georgia Institute of Technology. He is a member ofthe IEEE. Preview Digital image processing is an area characterized by the need for extensive ex- perimental work to establish the viability of proposed solutions to a given problem. In this chapter we outline how a theoretical base and state-of-the-art software can be integrated into a prototyping environment whose objective is, {to provide a set of well-supported tools for the solution of a broad class of problems in digital image processing, HAR Background ‘An important characteristic underlying the design of image processing 5ys- temsis the sigfcant evel of esting and experimentation thst normaly ise 4uited before arriving at an acceptable solution. Ths characteristic implies that the ability to formulate approzches and quickly prototype candidate solu- tions generally plays a major role in reducing the cost and time required to ave at viable system implementation, tle has been Written in the way of instructional material to bridge the betseen theory and applation na wellsppored software enrronment the rain objective of this book sto integrate under one cover abroad base of the- retical concepts with the knowiedge required to implement those concepts ‘wing state-of the-art image processing software tools The theoretical underpin- rings ofthe material inthe following chapters are mainly from the leading text- book in the field: Digital mage Processing, by Gonzalez and Woods, published by Prentice Hall The software code and supporting tools are based on the lead- ing sottware package in the field: The MATLAB Image Processing Toolbor. cuson nin sueguen chapter we sometimes ee Digi Image Poe Woods asthe Gonss oots bok anda the nage Peng Faker UPES oper 1 2 Introduction from The MathWorks, Ine. (see Section 1.3), The material in the present book shares the same design, notation, and style of presentation as the Gonzalez- ‘Woods book, thus simplifying cross-referencing between the 1Wo. ‘The book is self-contained. To master its contents, the reader should have introductory preparation in digital image processing, either by having taken a formal course of study on the subject at the senior or first-year graduate level, oo by acquiring the necessary background in a program of self-study. It is as- sumed also that the reader has some familiarity with MATLAB, as well as rudimentary knowledge of the basics of computer programming, such as that acquired in a sophomore- or junior-level course on programming in a techni- cally oriented language. Because MATLAB is an array-oriented language. basic knowledge of matrix analysis also is helpful “The book is based on principles. It is organized and presented in a textbook format, not as a manual. Thus, basic ideas of both theory and software are ex- plained prior to the development of any new programming concepts. The ma- terial is illustrated and clarified further by numerous examples ranging from medicine and industrial inspection to remote sensing and astronomy. This ap- proach allows orderly progression from simple concepts to sophisticated im- plementation of image processing algorithms. However, readers already familiar with MATLAB, IPT, and image processing fundamentals can proceed directly to specific applications of interest, in which case the functions in the book ean be used as an extension of the family of IPT functions Al new func tions developed in the book are fully documented, and the code for each is included either ina chapter or in Appendix C. (Over 60 new functions are developed in the chapters that follow. These functions complement and extend by 35% the set of about 175 functions in IPT-In addition to addressing specifi applications, the new functions are clear examples of how to combine existing MATLAB and IPT functions with new code to develop prototypic solutions t0 a broad spectrum of problems in digi- tal image processing. The toolbox functions, as wel asthe functions developed in the book, run under most operating systems. Consult the book Web site (see Section 1.5) for a complete list. ‘4 What Is Digital Image Processing? ‘An image may be defined as a two-dimensional function f(y), where x and Jy are spaia! coordinates, and the amplitude of fat any pair of coordinates (G9) iscalled the intensity or gray level of the image at that point. When and the amplitude values of f are all finite discrete quantities, we call the image a digital image The fel of digital image processing refers to processing digital images by means ofa digital computer. Note that a digital image is com- posed ofa finite number of elements each of which has a particular location and value. These elements are referred to as picture elements, image elements pels, and pels. Pixel is the term most widely used to denote the elements of a digital image. We consider these definitions formally in Chapter 2. 1.2. What Is Digital Image Processing? Vision i the most advanced of our senses s its not surprising that images play the single most important role in human perception. However, ualike hi rnans, who are limited tothe visual band ofthe electromagnetic (EM) spec- trum, imaging machines cover almost the entire EM spectrum, ranging from zamma to radio waves They can operate also on images generated by sources that humans are not accustomed to associating with images. These include ul- trasound, electron mieroscopy, and computer-generated images. Thus, digital image processing encompasses a wide and varied feld of applications. ‘There is no general agreement among authors regarding where image pro- cessing stops and other related areas, such as image analysis and computer vie sion, start, Sometimes a distinction is made by defining image processing a8 8 >) and where the outputs of those commands are displayed. MATLAB defines the workspace as the set of variables that the user creates in a work session. The Workspace Browser shows these variables and some information about them, Double-clicking on a variable in the Wotk- space Browser launches the Array Editor, which can be used to obtain infor- ration and in some instances edit certain properties ofthe variable. ‘The Current Directory tab above the Workspace tab shows the contents of the current directory, whose path is shown in the Current Directory Window. For example, in the Windows operating system the path might be as follows: CAMATLAB\Work, indicating that directory “Work” is a suhdirectory of the main directory “MATLAB,” which is installed in drive C. Clicking on the arrow in the Current Directory Window shows a list of recently used paths CCicking on the button to the right of the window allows the user to change the current directory: 1 The MATLAB Working Environment 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, isto select Set Path from the File meau on the desktop, and then use the Set Path dialog box. I 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 toselect various options in addition to executing the commands. This isa use- ful feature when experimenting with various commands in a work session. 1.7.2 Using the MATLAB Editor to Create M-Files ‘The MATLAB editor is both a text editor specialized for creating M-files and 1 graphical MATLAB debugger. The editor can appear in a window by itself, ‘or it can be a subwindow in the desktop. M-fles are denoted by the extension as in pixeldup.n. The MATLAB editor window bas numerous pull-down ‘menus for tasks such as saving, viewing, and debugging files. Because it per forms some simple checks and also uses color to differentiate between various clements 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 edt Filenane at the prompt opens the M-fle #lenane.m in an editor window, ready for editing. As noted earli- r,the file must be in the current directory or ina directory in the search path. 1.3 Getting Help ‘The principal way to get help online is to use the MATLAB Help Browser, ‘opened as separate window either by clicking on the question mark symbol (2) on the desktop toolbar, or by typing helpbronser at the prompt in the Command Window. The Help Browser is a Web browser integrated into the MATLAB desktop that displays Hypertext Markup Language (HTML) doc rents. The Help Browser consists of two panes, the help navigator pane, used to find information, and the display pane, used to view the information Selfexplanatory tabs on the navigator pane are used to perform a search For example, help on a specific function is obtained by selecting the Search tab, slecting Function Name as the Search Type, and then typing inthe func- tion name in the Search for field. It is good practice to open the Help Browser "Use ofthe term online nhs bok refers formation such Rep es loa computer ‘sem ston tbe Intro, 9 Gapter 1 Introduction, at the beginning of a MATLAB session to have help readily available during code development or other MATLAB task. "Another way to obtain help for a specific function is by typing doc followed by the function name at the command prompt.For example, typing doc format displays documentation for the function called foraat in the display pane of the Help Browser, This command opens the browser if it is not already open, ‘M-funetions have two types of information that can be displayed by the user. The first is called the H7 line, which contains the function name and a ‘one-line description, The second isa block of explanation called the Help text block (these are discussed in detail in Section 2.10.1). Typing help at the prompt followed by a function name displays both the H1 line and the Help text for that function in the Command Window. Occasionally, this information ccan be more up to date than the information in the Help browser because itis extracted directly from the documentation of the M-function in question. Typ- ing Lookfor followed by a keyword displays all the H1 lines that contain that ‘keyword. This function is useful when looking for a particular topic without ‘knowing the names of applicable functions For example, typing Looktor edge at the prompt displays all the Hil lines containing that keyword. Because the Hil line contains the function name, it then becomes possible to look at specif- ic functions using the other help methods. Typing lookfor edge ~all at the prompt displays the Hi line of all functions that contain the word edge in ei- ther the HI line or the Help text block. Words that contain the characters edge also are detected. For example, the Hi line of a function containing the word polyedge in the Hi line or Help text would also be displayed. 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 lookfor. Its highly recommended that the reader be- ‘come familiar with all these methods for obtaining information because in the following chapters we often give only representative syntax forms for MAT- LAB and IPT functions This is necessary either because of space limitations ‘ro avoid deviating from a particular discussion more than is absolutely nec- ‘essary. In these cases we simply introduce the syntax required to execute the function in the form required at that point. By being comfortable with online search methods, the reader can then explore a function of interest in more de~ tall with little effort Finally, the MathWorks’ Web site mentioned in Section 1.3 contains 2 large database of help material, contributed functions, and other resources that should be utilized when the online documentation contains insufficient infor- mation about a desired topic. 4 Saving and Retrieving a Work Session ‘There are several ways to save and load an entire work session (the contents of the Workspace Browser) or selected workspace variables in MATLAB, The simplest i as follows To save the entire workspace, simply right-click on any blank space in the Workspace Browser window and select Save Workspace As from the menu that appears. This opens a directory window that allows naming the file and se- lecting any folder in the system in which to save it. Then simply click Save. To save a selected variable from the Workspace, select the variable with a left Click and then right-click on the highlighted area. Then select Save Selection ‘As from the menu that appears. This again opens a window from which a fold- er can be selected to save the variable. To select multiple variables, use shift lick or control-lick in the familiar manner, and then use the procedure just described for a single variable. All files are saved in double-precision, binary format withthe extension .mat. These saved files commonly are referred to as, -MATyfiles. For example, a session named, say, mywork_2003_02_10, would ap- pear as the MAT-fle mywork_2003_02_10.mat when saved. Similarly, a saved image called Final_inage (which is a single variable in the workspace) will appear when saved as final_image.mat. ‘To load saved workspaces andlor variables, left-click on the folder icon on the toolbar of the Workspace Browser window: This causes a window to open from which a folder containing the MAT-files of interest can be selected. Double-clicking on a selected MAT-file or selecting Open causes the contents of the file to be restored in the Workspace Browser window. Its possible to achieve the same results described in the preceding para- graphs by typing save and Load at the prompt, with the appropriate file names and path information. This approach is not as convenient, but itis used when formats other than those available in the menu method are required. As an exercise, the reader is encouraged to use the Help Browser to learn more about these two functions, EGE How References Are Organized in the Book All references in the book are listed in the Bibliography by author and date, as in Soille [2003]. Most of the background references for the theoretical content ‘of the book are from Gonzalez and Woods [2002]. In cases where this is not tive, the appropriate new references are identified at the point in the discus- sion where they are needed. References that are applicable to all chapters, such as MATLAB manuals and other general MATLAB references, are $0 identified in the Bibliography. Summary Inaddion ve inodton wo notation a be MATLAB ol, he terol in is chopter empha the mpoce ot compretestc peteying enon tent intesiona ging: poseang enon hewn chpcrve Sb oly te ouson needed wun uncon anna ftumenlpropamming concept tt fd twit he Bok The eal inChapten truth 2 apr decor scion ops tate athe mina Stat og reeing apts However soup he pes eovre ae ar, In he dno oe Chapel theamefae hee of enonstang how combing MATLAB snd TF uncon wih new soe ane sed ose brad pect of mae ecening robems un Preview As mentioned in the previous chapter, the power that MATLAB brings to dig ital image processing is an extensive set of functions for processing multidi- mensional arrays of which images (two-dimensional numerical arrays) are a special case, The Image Processing Toolbox (IPT) is a collection of functions that extend the capability of the MATLAB numeric computing environment. ‘These functions, and the expressiveness of the MATLAB language, make ‘many image-processing operations easy to write in a compact, clear manner, thus providing an ideal software prototyping environment for the solution of image processing problems In this chapter we introduce the basics of MA LAB notation, discuss a number of fundamental IPT properties and functions, and introduce programming concepts that further enhance the power of IPT. ‘Thus, the material in this chapter is the foundation for most of the material in the remainder of the book. WAY Digital Image Representation ‘An image may be defined as a two-dimensional function, f(x, y), where x and ‘are spatial (plane) coordinates, and the amplitude off at any pair of coordi nates (x,y) is called the intensity of the image at that point. The term gray level is used often to refer to the intensity of monocarome images. Color images are formed by a combination of individual 2-D images. For example, in the RGB color system, a color image consists of three (red, green, and biue) individual component images. For this reason, many of the techniques developed for monochrome images can be extended to color images by processing the three component images individually. Color image processing is treated in detail in Chapter 6. 21 Digital image Representation 13 ‘Aa image may be continuous with respect tothe x- and y-coordinates, and also in amplitude. Converting such an image to digital form requires thatthe coordinates as well as the amplitude, be digitized. Digitizing the coordinate vals i called sampling: digitizing the amplitude values is called quantization. ‘Thus, hen x, y,and the amplitude values of f are all finite, discrete quantities, wwe cal the image a digital image. 2.1.1 Coordinate Conventions ‘The result of sampling and quantization is a matrix of real numbers. We use two principal ways in this book to represent digital images. Assume that an image f(x, y) is sampled so that the resulting image has M rows and NV columns. We say that the image is of size M x N. The values of the coordi nates (x, y) are discrete quantities For notational clarity and convenience, we use integer values for these diserete coordinates. In many image processing ‘books, the image origin is defined to be at (x, y) = (0,0). The next coordinate values along the frst row of the image are (x, y) = (0,1). Itis important to keep in mind that the notation (0, 1) is used to signify the second sample along. the first row. It docs not mean that these are the actual values of physical co: ordinates when the image was sampled, Figure 2.1(2) shows this coordinate convention. Note that x ranges from 0to M ~ 1, and y from to N = 1, inin- leger increments "The coordinate convention used in the toolbox to denote arrays i different from the preceding paragraph in two minor ways. First, instead of using (x,y), the toolbox uses the notation (r, c) to indicate rows and columns. Note, how- cver, that the order of coordinates is the same as the order discussed in the previous paragraph, in the sense that the first element of a coordinate tuple, (2,6), refers to arow and the second to a column. The other difference is that the origin of the coordinate system is at (r,) = (1, 1);thus,r ranges from 1 to M, and c from 1 to N, in integer increments This coordinate convention is shown in Fig.2.1(b). o12 peel, -« fl eeea oe oe a FOUR 2.1 Cooidinate ‘conventions used (Gin many image processing books, and (b) inthe Image Processing Toolbox. L Ghopler 2m Fundamentals the rorms mate relangeably. How deep mind tenioal whereas stan ean have ‘yin dimension IPT documentation refers to the coordinates in Fig.2.1(b) as pivel coordi« rates. Less frequently, the toolbox also employs another coordinate conven tion called spatial coordinates, which uses.x to refer to columns and y to refers 10 rows. This is the opposite of our use of variables x and y. With very few ex ceptions, we do not use IPT's spatial coordinate convention in this book, but the reader will definitely encounter the terminology in IPT documentation. 2.4.2 Images as Matrices “The coordinate system in Fig. 2.1(a) and the preceding discussion lead to the following representation fora digitized image function: F(0.0) £(0,1) £O.N -1) Hay) 70,0) F.1) fun y) f(M = 1,0) f(M = 1,1) f(t = 1,.N ~ 1) ‘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 image and pixel are used throughout the rest of our discussions to denote a digital image and its elements. "A digital image ean be represented naturally as a MATLAB matrix: fran #2) fam] Jr t22) ~ F2,m) fe Lean sy) tn,2y = tH, mp where #(1, 1) = £(0,0) (note the use of a monospace font to denote MAT- LAB quantities), Clearly the two representations are identical, except for the shiftin origin. The notation #(p, q) denotes the element located in row p and column q, For example, #(6, 2) is the element inthe sixth row and second col ‘umn of the matrix f. Typically we use the letters i and N, respectively, to de- note the number of rows and columns in a matrix. A 1x¥ matrix is called a ‘row vector, whereas an Mx 1 matrix is called a column vector. A 1 %1 matrix is a scalar. Matrices in MATLAB are stored in variables with names such as A, a, G8, rreal_array, and so on. Variables must begin with a letier and contain only letters, numerals, and underscores. As noted in the previous paragraph, all MATLAB quantities in this book are written using monospace characters. We use conventional Roman, italic notation, such as f(x,y), for mathematical expressions GA Reading Images Images are read into the MATLAB environment using function inread, whose syntax is imread(‘filenane') 22m Reading Images 15 Recognized — Extensions ‘Tagged image File Format tif, tire Joint Photographic Experts Group “jp. -}peg Graphics Interchange Format! lait Windows Bitmap ap Portable Network Graphics pg X Window Dump wd "GUFiesuppenedy snceae,bat ot by amwrite Here, ilenane isa string containing the complete name of the image file (in- cluding any applicable extension). For example, the command line >> # = Anmoad( ‘chestxray. jog"); zeads the JPEG (Table 2.1) image chestxray into image array t. Note the use of single quotes ("} to delimit the string filename, The semicolon at the end ‘of command line is used by MATLAB for suppressing output. Ifa semicolon is not included, MATLAB displays the results of the operation(s) specified in that line, The prompt symbol (>>) designates the beginning of a command line, asit appears in the MATLAB Command Window (see Fig. 1.1) ‘When, asin the preceding command line, no path information is included in filename, imread reads the file from the current directory (see Section 1.7.1) and, if that fails it tries to find the file in the MATLAB search path (see Section 1.7.1), The simplest way to read an image from a specified directory is to include a full or relative path to that directory in Filename. For example, >> f = imread('D: \eyinages\chestxray. jg"); reads the image from a folder called nyinages on the O: drive, whereas >> f = imread( '. \nyinages\chestxray. Jpg"); reads the image trom the nyimages subdirectory of the current working di- rectory. The Current Directory Window on the MATLAB desktop toolbar Aisplays MATLAB's current working directory and provides a simple, man- ual way to change it. Table 2.1 lists some of the most popular image/graphics formats supporied by imread and imwrite (imurite is discussed in Section 2.4) Function size gives the row and column dimensions of an image: >> sizaity ans 1024 1024 TUBLE 2.1 Seme ofthe imagelsraphics formats supported by inread and soweste, starting with MATLAB 6S, Ecler versions support subset of, these formats See online help fora complete list of supported formats InWindows deco (6 Ghopter2 Fundamentals MATLAB ond IPT argent Mali foed wihin (ore brackets J “This function is particularly useful in programming when used in the following form to determine automatically the size of an image: >> [M,N] = size(t); ‘This syntax returns the number of rows (\) and columns (N) in the image. The whos function displays additional information about an array. For in- stance, the statement >> whos f gives Name size Bytes class t tozaxtoza 1048576 uints array Grand total 1s 1048576 elements using 1048676 bytes ‘The uint# entry shown refers to one of several MATLAB data classes dis- cussed in Section 2.5.A semicolon at the end of a whos line has no effect, so normally one is not used. Displaying Images Images are displayed on the MATLAB desktop using function ishow, which basic syntax: Anshow(t, 6) where ¢ is an image array, and G is the number of intensity levels used to dis- play it If is omitted, it defaults to 256 levels. Using the syntax. Anshow(f, {low high]) displays as black all values less than or equal to Low, and as white all values ‘greater than or equal to high. The values in between are displayed as interme- diate intensity values using the default number of levels Finally, the syntax Amshow(, (1) sets variable 2ow fo the minimum value of array f and high to its maximum value, This form of Amshow is useful for displaying images that have a low dy- namic range or that have positive and negative values. ‘Function pixval is used frequently to display ine intensity values of indl- vidual pixels interactively. This function displays a cursor overlaid on an image. As the cursor is moved over the image with the mouse, the coordi- nates of the cursor position and the corresponding intensity values are 23 m Displayingimages 17 shown on a display that appears below the figure window, When working with color images, he coordinates as well as the red, green, and blue compo- nents are displayed. Ifthe left button on the mouse is clicked and then held pressed, pixval displays the Euclidean distance between the initial and cur- rent cursor locations. ‘The syntax form of interest here is pixval “piiwal which shows the cursor on the last image displayed, Clicking the X button on the cursor window turns it off, M1 (a) The following statements read from disk an image called rose_612.tit, EXAMPLE extract basic information about the image, and display it using imshow: Image reading and displaying >> f= imread( ‘rose_512. tif"); >> whos t Name size Bytes class f 512x512 262144 uinte array Grand total is 262144 elements using 262144 bytes >> imshow() A semicolon at the end of an inshow line has no effect, so normally one is not used. Figure 2.2 shows what the output looks like on the screen. The figure umber appears on the top, left of the window. Note the various pull-down menus and utility buttons They are used for processes such as scaling, saving, tnd exporting the contents of the display window. In particular, the Kalit menu has functions for editing and formatting results before they are printed or saved to disk, Fioure 2.2 ‘Sereen capture shown how aa. ‘mage appears on the MATLAB, desktop, However, in most of the examples throughout this ‘book only the images themielves are shown, Note the figure number on the tep eft part ofthe window. 3. Gepter? 2 Fundamentals ‘tiguce “ne honed x figure window ‘nine to pure( ee gre namber Limieste b GURE23 (a) An sage. ha with low dynamic range, >) Result of scaling using inshon 1) Original adge courtesy of Se David ickens Dept Radiology & sdiologial Sciences. Vanderbilt Tniversity Medical If another image, g, is displayed using inshow, MATLAB replaces the image in the screen with the new image. To keep the frst image and output a second image, we use function f igure as follows: >> Figure, imshow(g) Using the statement >> imshow(f), figure, imshow(a) displays both images, Note that more than one command can be written on a line, long as different commands are properly delimited by commas or semi- ‘colons. As mentioned earlier, a semicolon is used whenever its desired to sup ‘press sereen outputs from a command line, (b) Suppose that we have just read an image hand find that using amshow(h) produces the image in Fig. 2.3(a). Its clear that this image has a low dynamic Tange, which ean be remedied for display purposes by using the statement >> inshow(h, 1) Figure 2.3(b) shows the result. The improvement is apparent. s AQ writing images Images are written to disk using function ameri basic syntax: Which has the following imurite(f, ‘Tilenane’) ‘With this syntax, the string contained in fAlenane must include a recognized file format extension (see Table 2.1). Alternatively, the desired format can be specified explicitly with a third input argument, For example, the following command writes # to a TIFF file named patient10_runt >> imirite(f, ‘patientio_runt', 'tif*) or, alternatively, >> Lmweite(#, ‘patienti_cunt.tit") 24 9 Writing Images 19 If filename contains no path information, then imurite saves the file in the current working directory. ‘The imwrite function can have other parameters, depending on the file for: rat selected. Most of the work in the following chapters deals either with SPEG or TIFF images, so we focus attention here on these two formats, ‘A more general imirte syntax applicable only to JPEG images is Amwrite(t, “filename.ipg’, ‘quality’, a) where q is an integer between 0 and 100 (the lower the number the higher the degradation due to JPEG compression}. 1 Figure 2.4(a) shows an image, f typical of sequences of images resulting {rom a given chemical process It is desired to transmit these images on a rou- tine basis to a central site for visual and/or automated inspection In order to reduce storage and transmission time, iti important thatthe images be com. pressed as much as possible while not degrading their visual sppearance beyond a reasonable level. In ths case “reasonable” means no perceptible false contouring Figures 2.4(b) through (f) show the results obtained by writ ing image to disk (in JPEG format) with q = 60,25, 18,8. and 0, respective Iy,For example, for q= 25 the applicable syntax is >> imrite(f, ‘bubbles2s.jpg", ‘quality’, 25) The image for q = 15 [Fig 2.4(d)] as false contouring that is barely visible, but this effect becomes quite pronounced for q = 5 and q 0. Thus, an acceptable solution with some margin for error isto compress the images with = 25. In order to get an idea of the compression achieved and to obtain other image file details, we can use function int info, which has the syntax intinfo filenane where filename is the complete file name of the image stored in disk. For example, >> imtinfo buboles2s. jp9 ‘outputs the following information (note that some fields contain no informa- tion in this case): Filename: “bubbles25. jpg" Filelloddate: 04-Jan-2003. 12:3 Fileaize: 19049 Format: jpg Formatversion: * width: 714 Height: 682 BitDepth: 8 colortype: ‘grayscale FormatSignaturs Comment: {} EXAMPLE22: Writing an image fonction intanfo. 20 Ghepler? 9 Fundamentals ab ed ef FIGURE 2.4 (2) Onginal image () through (9 Results of using 3p9 quality = 50,25, and 0, respectively. False contouring begins to be barely noticeable for {image (@)) but is quite visible fora=sand where F£Leaize isin bytes The muvnber of Lyles in the writ image is eon puted simply by multiplying tidth by Height by BitOepth and dividing the result by 8. The result is 48648, Dividing this by FileSize gives the compres sion ratio: (486948/13849) = 35.16. This compression ratio was achieved while maintaining image quality consistent with the requirements of the appli- 24 Writing Images 2 cation. In addition to the obvious advantages in storage space, this reduction allows the transmission of approximately 35 times the amount of uncom: pressed data per unit time. ‘The information fields displayed by iafinfo can he captured into a so- called structure variable that can be used for subsequent computations. Using the preceding image as an example, and assigning the name K to the structure variable, we use the syntax >> K = imfinfo(“bubbles25. jpg"); to store into variable K all the information generated by command infinfo. ‘The information generated by intinfo is appended to the structure variable by means of fields, separated from K by a dot. For example, the image height ‘and width are now stored in structure fields K, Hesght and K.Wicth ‘As an illustration, consider the following use of structure variable K to com- pute the compression ratio for bubb1es25. jpg: >» K = imfinfo‘bubbles25. jpg"); >> inage_bytes = K.Width*K.Height*K.BitDepth/6 >> compressed bytes = K.Filesize; >» compression_ratio = inage_bytos/comprossod_bytos compression ratio = 35.1612 Note that imfinfo was used in two different ways. The first was to wpe infinfo bubbies25. jpg at the prompt, which resulted in the information being displayed on the screen. The second was to type K = infinfo( bub. bles25. jpg"), which resulted in the information generated by intinto being stored in k. These two different ways of calling snfinfo are an example ‘of command-function duality, an important concept that is explained in more detail in the MATLAB online documentation, 2 has the form ‘A more general imurite syntax applicable only to taf im invrite(g, ‘filenane.tif', ‘compression’, ‘paraneter’, . resolution’, [colres’ rowres}) where "paranater’ can have one of the following principal values: ‘none indicates no compression; ‘packbits' indicates packbits compression (the default for nonbinary images); and ‘coitt’ indicates ccitt compression (the default for binary images). The 1 X 2 array [colras ronres] contains two in tegeis at give the coluinn resolusion and row resolution in duts-per-unit (Wie default values are (72°72). For example, if the image dimensions are in inches, colres is the number of dots (pixels) per inch (dpi) in the vertical direction, ‘and similarly for eowres in the horizontal direction, Specifying the resolution by a single scalar, res, is equivalent to writing [res res}. ‘tal cons ‘See Section perio), flowed by enw the pero 2 Ghepter 2 @ Fundamentals XAMPLE23: ‘Sing inerste sure 25 Erfeots of anging the dpi “ofution while eping the amet of pixels VA 450 x 450 age at 200 dpi wen 225 225 inches). ) The same 5 x 450 image, {at300 dpi oe = 15 “Sinches) Driginal image urtesy of Lisi 3) {@ Figure 2.5(a) is an 8-bit X-ray image of a circuit board generated during quality inspection. It is in jpg format, at 200 dpi. The image is of size 450 X 450 pixels so its dimensions are 2,25 X 2.25 inches. We want to store this image in-t1f formal, with no compression, under the name st.In addition, ‘we want to reduce the size of the image to 1.5 1.5 inches while keeping the pixel count at 450 x 450, The following statement yields the desired result >> inurite(t, ‘sf.tif", ‘compression’, ‘none’, ‘resolution’, ... 300"300]) The values ofthe vector (colres roures] were determined by multiplying 200 dpi by the ratio 2.25/1.5, which gives 300 dpi, Rather than do the compu tation manually, we could write >> res = round(200°2.25/1.5); >> imurite(#, 'sf.tif", ‘Compression’, ‘none’ , resolution’, res) ‘where function round rounds its argument to the nearest integer. It is impor- tant to note that the number of pixels was not changed by these commands. Only the scale of the image changed. The original 450 x 480 image at 200 dpi is of size 2.25 x 2.25 inches. The new 300-dpi image is identical, except that its 25 Data Classes 450 x 450 pixels are distributed over a 1.5 15-inch area. Processes such as this are useful for controlling the size of an image in a printed document with- cut sacrificing resolution, * Often, itis necessary to export images to disk the way they appear on the MATLAB desktop. This is especially true with plots, as shown in the next chapter. The contents ofa figure window can be exported to disk in two ways. The fist is t0 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 print command: print -fno ~d¥ileformat —rresno filenane where no refers to the figure number in the figure window of interest, filefornat refers to one of the file formats in Table 2.1, resno isthe resol tion in dpi, and #4.ename is the name we wish to assign the file. For example, to export the contents of the figure window in Fig.2.2 as a tit file at 300 dpi, and under the name hi_res_rose, we would type >> print 11 —dtift -r300 hi_res_rose ‘This command sends the file hi_res_rose. tit to the current directory. If we simply type print at the prompt, MATLAB prints (to the default printer) the contents of the lat figure window displayed. It is possible also to specify other options with print, such as a specific printing deviee Data Classes Although we work with integer coordinates the values of pixels themselves are ‘ot restricted to be integers in MATLAB Table 22 ists the various data clases’ supported by MATLAB and IPT for representing pixel values. The frst eight entries inthe table are referred to as numeric data classes. Te ninth entry the ¢har cass and, as Shown, the last entry is referred to asthe logical data clas. All numeric computations in MATLAB are done using double quantities, so this is also a frequent data class encountered in image processing applica tions. Class usnt6 also is encountered frequently especially when reading data from storage devices, as 8-bit images are the most common representa tions found in practice. These twa data classes, class logical, and, to & lesser degree, class vint 16, constitute the primary data classes on which we for in this book, Many IPT functions however, support all the data classes listed in Table 2.2, Data class double requires 8 bytes to represent a number, uint® and int8 require I byte each, uint16 and ant16 require 2bytes,and uint92 "MATLAB documeatation often wes he ems da cls rl dar pe tersnangea I his Dok, eres us fhe rm pe or mages a ted in print B 24 hapter? i Fundamentals TABLE 22 Data clases The first eight entries are relerred toa numeric classes; the ninth entry i the character class, and the last entry is of clas logical Name Desciion aoubie Dovbl-pregion, Goning-poin numbers i the approxinate range 10 to 10 (byte pe element vints Unsigned bit integers inthe range [0,255 (bye pr element). cintte Unsigned 16tit integers inthe range 0.6555] 2 bytes per sleet), vintse Unsigned 32-5 integers in the ange [0429957295] (4 yes peretemeat, ante Sgned i itgersin the range [12,127] ( byte pr clement ntt® Signed Ibi integers inthe range [327683767] bytes por Slement). ntsa Signed Stinger the range [~21S89685, 2147485647] (Gbvtes pe element. singte_ Single-preision Noting point number with ales the povinate range ~1D to 10" (per per lem). nar Character (2 byes pr element) Jogical —_Valneare Oo 1 (1 byte per elemen int92, and single, require 4 bytes each. The char data class holds characters in Unicode representation. A character string is merely a 1 xn array of char- acters. A Logical array contains only the values 0 and 1, with each element boeing stored in memory using one byte per element. Logical arrays are cre: cd by using funetion Logical (see Section 2.6.2) or by using relational opera- tors (Section 2.10.2). 2B image Types “The toolbox supports four types of images: + Intensity images * Binary images + Indexed images + RGB images Most monochrome image processing operations are carried out using binary ‘or intensity images, so our initial focus is on these two image types. Indexed and RGB color images are discussed in Chapter 6. 2.5.3 Intensity Images An intensity image is a data matrix whose values have been scaled to represent intensities. When the elements of an intensity image are of class uint8, or class usnt6, they have integer values in the range [0,255] and [0, 65535], re- spectively. Ifthe image is of class double, the values are floating-point num- bers Values of sealed, class double intensity images are in the range (0, 1] by ‘convention, 27 Converting between Data Classes and tmageTypes 25 126.2 Binary Images Binary images have a very specific meaning in MATLAB. A binary image is 2 logical array of Os and Is. "Thus an array of Os and 1s whose values are of Gata clas, a, uint®, is not considered a binary image in MATLAB, A fumeric aray is converted to binary using function Logical. Thus, if Ais 2 pumeric array consisting of Os and is, we create a logical array B using the statement 8 = logical(A) If Acontains elements other than Os and Is, use of the logical function con- ‘yerts all nonzero quantities to logical 1s and all entries with value 0 to logical (0 Using relational and logical operators (see Section 2.10.2) also creates logi calarrays ‘To test if an array is logical we use the islogical func islogical(c) If Cis logical array, this fonction returns a 1. Otherwise it returns a0. Logical arrays can be converted to numeric arrays using the data class conversion functions discussed in Section 2.7.1. 26.3 A Note on Terminology Considerable care was taken in the previous two sections to clarify the use of the terms data class and image type. In general, we refer to an image as being a “data_class image_type image,” where data_class is one of the entries from Table 22, and image_type is one ofthe image types defined at the begin- sing ofthis section. Thus an image is characterized by both a class and a type. For instance, a statement discussing an “unit8 intensity image" is simply r= ferring to an intensity image whose pixels are of data class unit8, Some func- tions in the toolbox support all data classes, while others are very specific as to What constitutes a valid class. For example, the pixels in a binary image can only be of data class Logical, as mentioned earlier. Converting between Data Classes and Image Types Converting between data classes and image types is a frequent operation in IPT applications. When converting between data classes, it is important to keep in mind the value ranges for each data class detailed in Table 22. 27.1 Converting between Data Classes Converting between data classes is straightforward. The general syntax is B = data_class_nane(A) Whete data_class_nane is one of the names in the first column of Table 2.2 For example, suppose that A is an array of class usnt8. A double-precision sfioaicat oF stonican See Tbte29 fora Ti of oer fin tons sed om the isha 26 hope? 2 Fundamentals ‘Reon change 1338 decsred Seenon 323.con be “edo changing an pat image ro aspe Etre ABLE 23 unctions in IPT sor converting ‘ween image ‘asses and types. te Table 63 for ply specially »eolor images, is generated by the command 8 = doube (A). This conversion is used routinely throughout the book because MATLAB expects operands in nu merical computations 1 be double-precison, floating-point numbers. IFC isan aay of clas double i which al values are in the range [0,255] (but possibly containing fractional values), it can be converted to an uant@ array with the command D = uint@(C) Ian array of class double has any values outside the range [0,255] andi is converted to class uints in the manner just described, MATLAB converts to O al values that are less than 0, and converts to 255 all values that are greater than 255. Numbers in between are converted to integers by discarding their fractional parts Thus proper scaling ofa double array so that its elements are in the range [0,255] is necessary before converting ito wint8. As indicated in Section 2.62, converting any of the numeric data classes to Logical results in ‘n array with logical Is in locations where the input array had nonzero values, and logical Os in places where the input array contained Os. 23.2 Converting between Image Classes and Types ‘The toolbox provides specific functions (Table 2.3) that perform the scaling necessary to convert between image classes and types. Function im2uint8 de- tects the data class of the input and performs all the necessary scaling for the toolbox to recognize the data as valid image data. For example, consider the following 2 x 2 image f of class double, which could be the result of an inter- Performing the conversion >> g = inguinte(t) yields the result go 0 128 191255 Name ‘Converts Input co ‘Valid Input image Data Classes amuints wants Logical, vint8,uint16,and doubte | Amauinere ints Logical. vinta, uint16,and double at2gray double (in range 0.1]) double Sw2double double Logical, vint,uint 16, and double sn2bw 1093 int. uine16,and double 27 Converting between Data Clases and Inage Types {rom which we see that function in2uint6 sets to 0 all values in the input that are less than O, sets to 255 all values in the input that are greater than 1, and ‘multiplies all other values by 255. Rounding the results ofthe noultipliation to the nearest integer completes the conversion. Note that the rounding behavior cof inzuint® is different from the data-class conversion function uint@ dis- cussed in the previous section, which simply discards fractional parts. Converting an arbitrary array of class double to an array of class double scaled to the range [0, 1] can be accomplished by using function mat2gray ‘whose basic syntax is g = matagray(A, (Amin, Anax]) ‘where image g has values in the range O (black) to 1 (white). The specified pa- rameters Ain and Amax are such that values less than Amin in Abecome 0 in g, and values greater than Anax in A correspond to 1 in g. Writing >> g = matzgray(A}; sets the values of Amin and Anax to the actual minimum and maximum values in ‘A The input is assumed to be of class double. The output also is of class double. Function in2double converts an input to class double. If the input is of class uint@, uint16, or logical, function in2double converts it to class double with values in the range (0, 1]. Ifthe input is already of class double, in2double returns an array that is equal to the input. For example, if an array of class double results from computations that yield values outside the range (0,1} inputting this array into in2doubLe will have no effect. As mentioned in the preceding paragraph, a double array having arbitrary values can be con- verted to a double array with values in the range [0, 1) by using function at2gray. ‘As an illustration, consider the class wint8 image! >> h = uinte((25 80; 128 2001); Performing the conversion >> g = inadouble(h); yields the result oe 0.0980 0.4708 0.1961 a rae from which we infer that the conversion when the input is of class uint@ is done simply by dividing each value of the input array by 255. If the input is of lass uint 18 the division is by 65535, "Seaton 282 exp the we of square Bact end semicolons to pel a mae 28 Ghopler? a Fundamentals ENAMPL Converting between image clases and types Finally, we consider conversion between binary and intensity image types Function im2bu, which has the syntax @ = dngpa(t, T) produces a binary image, g, from an intensity image, f, by thresholding, The output binary image g has values of 0 for all pixels in the input image with intensity values less than threshold T, and 1 for all other pixels. The value specified for T has to be in the range [0,1], regardless of the class of the input. The output binary image is automatically declared as a logical array by imabw. If we write g = in2bw(f), IPT uses a default value of 0.5 for T.If the input is an uints image, im2bw divides all its pixels by 255 and then ap- plies either the default or a specified threshold. If the input is of class Uuint16, the division is by 65535. If the input is a double image, smdbw ap. plies either the default or a specified threshold directly. Ifthe input is a Logical array, the output is identical to the input. A logical (binary) array ‘can be converted to a numerical array by using any of the four functions in the first column of Table 2.3. {@ (a) We wish to convert the following double image o> f= 192534) to binary such that values 1 and 2 become 0 and the other two values become 1. First we convert it to the range [0, 1}: >> g = matzgray(t) a 0 0.3933 0.8867 1.0000 ‘Then we convert it to binary using a threshold, say, of value 0.6: >> gb = im2bw(g, 0.8) oo 27 Converting between Data Classes and Imag? Types ‘As mentioned in Section 2.5, we can generate a binary array directly using re~ {ational operators (Section 2.10.2). Thus we get the same result by writing pogo =f>2 ob oo 14 We could store in a variable (say, gbv) the fact that gb is a logical array by using the islogical function, as follows >» gbv = islogical(go) ove 1 (b) Suppose now that we want to convert gb to a numerical array of Os and {sof class double. This is done directly: >> gbd = in2double(gb) god = oo 14 If gb had been a numeric array of class uint8, applying im2double to it would have resulted in an array with values ° ° 9.0089 9.0039 because én2double would have divided all the elements by 255. This did not happen in the preceding conversion because an2double detected that the input was a Logical array, whose only possible values are O and 1. Ifthe input in fact had boon an uint8 numeric array and we wanted to convert it to class ‘double while keeping the 0 and 1 values, we would have converted the array by writing > abd abd = oo 14 jouble(gb) 29 0 Gepter2 Fundamentals Acanepore comptes th con ‘erranspose Wher naps an be el erchangeab ee Tie Finally. we point out that MATLAB supports nested statements so we could have started with image # and arrived atthe same result by using the one-line statement >> gbd = imgdouble(inzow(natzgray(t), 0.6)); co by using partial groupings of these functions. Of course, the entire process could have been done in this case with a simpler command: >> ghd = double(f > 2); again demonstrating the compactness of the MATLAB language. . Array Indexing MATLAB supports @ number of powerful indexing schemes that simplify array manipulation and improve the efficiency of programs. In this section we discuss and illustrate basic indexing in one and two dimensions (ie., vectors and matrices). More sophisticated techniques are introduced as needed in sub sequent discussions 28.1 Vector Indexing ‘As discussed in Section 2.12, an array of dimension 1 x N is called a row vec for. The elements of such a vector are accessed using one-dimensional index- ing Thus, v(1) is the first element of vector v, v(2) its second element, and so forth, The elements of vectors in MATLAB are enclosed by square brackets and are separated by spaces or by commas. For example, 13879) ia iets) 7 aa) > via) ans 3 A.row vector is converted to a column vector using the transpose operator (.") 28m Araylndexing 31 ‘To access blocks of elements, we use MATLAB's colon notation, For exam- ple, to access the first three elements of v we write pe v(133) ans = 1305 Similarly, we can access the second through the fourth elements >> v(2id) ans 305 7 or all the elements from, say; the third through the last element: >> v(3zend) s 7 9 where end signifies the last clement inthe vector. If is a vector, writing >) produces a column vector, whereas writing >> v(tiend) Produces a row vector. Indexing is not restricted to contiguous elements, For example, >> v(t:2:end) ans = 1 5 9 The notation 1 :2:end says to start at 1, count up by 2 and stop when the count reaches the last element. The steps can be negative: 32 Gupte? ‘Lingpace Here, the index count started at the last element, decreased by 2, and stopped when it reached the first element. Function Linspace, with syntax x= Linspace(a, D, n) generates a row vector x of n elements linearly spaced between and including ‘ and b. We use this function in several places in later chapters, 'A vector can even be used as an index into another vector. For example, we can pick the first, fourth, and fifth elements of v using the command >> v(t 451) ans = eo 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 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 o> A= (128; 456; 789] displays the 3 X 3 matrix 1203 og 7 8 8 ‘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 ‘ovo indices: one to establish a row location and the other for the correspond ing column, For example, to extract the element in the second rou, third col ‘uma, we write >> Al2, a) 28 m Array indexing 33 ‘The colon operator is used in matrix indexing to select a two-dimensional block of elements out of a matrix. For example, >> 03 = Al, 9) oe 3 6 9 Here, use of the colon by itsifis analogous to writing A(1:3,9), which simply picks the third column of the matrix. Similarly, we extract the Second row as follows: >> RR = A(2, 2) Ra 45 6 The following statement extracts the top two rows: b> T2 = A(1:2, 1:9) ary 123 45 6 ‘To create a matrix 8 equal to A but with its last column set 1 Os, we write >> B(:, 3) = 0 Be 10200 45 0 7a 0; Operations using end are carried out ina manner similar to the examples given in the previous section for vector indexing, The following examples illus trate this, >> Alend, end) 8 4 (hopter 2 Fundamentals >> Alond, end ~ 2) 7 >> A(2iend, end ans = 6 4 a7 Using vectors to index into a matrix provides a powerful approach for ele~ ‘ment selection. For example, >> B= ACL 9], (291) cea a9 The notation A({a 8], {¢ d1) picks out the elements in A with coordinates (row a, column ¢), (row a, column d), (row b, column ¢), and (row b, column, 6). Thus, when we let = A([1 3), (2 3]) we are selecting the following ele- ments in A: the element in row 1 column 2, the element in row 1 column 3, the element in row 3 column 2, and the element in row 3 column 3, More complex schemes can be implemented using matrix addressing. A particularly useful addressing approach using matrices for indexing is of the form A(D), where Dis a logical array, For example, if >> D = logtcal({t 00; 00 1; 000}) o og 0 0 1 0 0 0 then >> ACD) 1 Finally, we point out chat use of a single colon as an index into a matrix se- leets all the elements ofthe array (on a column-by-column basis) and arranges them in the form of a column vector. For example, with reference to matrix T2, 28 w Array indexing pov = T2(:) This use of the colon is helpful when, for example, we want to find the sum of all the elements of a matrix: >> s = sum(A(:)) 48 In general, sun(v) adds the values of all the elements of input vector v. If ‘matrix is input into sum [as in sum (A) the output isa row vector containing the sums of each individual column of the input array (this behavior is typical of many MATLAB functions encountered in later chapters). By using a sin- ale colon in the manner just illustrated, we are in reality implementing the command >> sum(sum(A)); because use of a single colon converts the matrix into a vector. Using the colon notation is actually a form of linear indexing into a matrix or higher-dimensional array. Infact, MATLAB stores each array as a column of values regardless of the actual dimensions This column consists of the array columns, appended end to end. For example, matrix Ais stored in MATLAB as ‘Accessing A with a single subscript indexes directly into this column, For exan- ple, A(2) accesses the third value in the column, the number 7;A(B) accesses the eighth value, 6, and so on. When we use the column notation, we are simply 36 Ghopter? @ Fundamentals EXAMPLE 25: Some simple image operations ab de FIGURE 2.6 Results obtained indexing (0) Original image. (0) Image flipped vertically (6) Cropped limage.(e) horizontal sean line theough the middle ofthe addressing all the elements, A(1 :end). Ths type of indexing isa basic staple in vectorizing loops for program optimization, as discussed in Section 2.10.4 2 The image in Fig. 2.6(a) is a 1024 X 1024 intensity image, , of class uints. “The image in Fig. 2.6(b) was Mipped vertically using the statement >> fp = flendz—121, 1)5 2.6(¢) is a section out of image (a), obtained using ‘The image shown in the command #(257:768, 257:768); Similarly, Fig. 2.6(d) shows a subsampled image obtained using the statement >> fs = #(1:2:0nd, 1:2:end); 001000 29m Some Important Standard Arrays Finally, Fig. 2.6(e) shows a horizontal scan line through the middle of Fig. 2.6(a), obtained using the command >> plot(f(512, :)) ‘The plot function is discussed in detail in Section 3.3.1, . 28.3 Selecting Array Dimensions Operations of the form operation(A, dim) where operation denotes an applicable MATLAB operation, A is an array, and dimisa scalar, are used frequently in this book. For example, suppose that isan array of size M x N.The command > 820(A, 1); gives the size of & 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 isin the horizontal direction, so the statement siz0(A,2) gives the number of columns in A. A singleton di- ‘mension is any dimension, din, for which size(A, dim) = 1. Using these con- cepts, we could have written the last command in Example 25 as >> plot(f(size(f, 1)/2, :)) MATLAB does not restrict the number of dimensions of an array, so being able to extract the components of an array in any dimension is an important feature, For the most part, we deal with 2-D arrays, but there are several in stances (as when working with color or multispectral images) when itis neces sary to be able to “stack” images along a third or higher dimension. We deal with this in Chapters 6, 11, and 12. Function ndins, with syntax 4 = ndins(A) tives the number of dimensions of array A. Function ndims never returns a Value less than 2 because even scalars are considered two dimensional, in the Sense that they are arrays of size 1 x 1. EX some Important Standard Arrays Often, ts useful ro be able to generate simple image untays (vty oul ideas and to test the syntax of functions during developmen. In this section we in ioduce seven array-generating funetions that are used in later chapters. Tt only one argument is included in any of the following functions, the result isa square array. a 38 Gopter 2 # Fundamentals + zeros Ml, N) generates an MXN matrix of Os of class double, + ones (Mt, N) generates an lx N matrix of Is of class double. + true(M, M) generates an MN Logical matrix of 1s, + false (lt, N) generates an MN Logical matrix of Os + magic(Mt) generates an Mx U “magic square.” This is a square array in which the sum along any row, column, or main diagonal, i the same, Magic squares are useful arrays for testing purposes because they are easy to generate and their numbers are integers. + rand(U, N) generates an MXN matrix whose entries are uniformly distrib- tuted random numbers in the interval [0,1] + randn(tt, N) generates an MN matrix whose numbers are normally dis- tributed (Le., Gaussian) random numbers with mean 0 and variance 1 For example, >> A= Stones(3, 3) Ae ices 55 5 5 5 5 >> magic(3) eae 305 7 49 2 >> B= rand(2, 4) Be 0.2311 0.4860 0.7621 0.0185 0.6088 0.8913 0.4565 0.8214 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- come evident shorty, MATLAB function programming is flexible and parti. ularly easy 0 learn, 2.10.1 M-Files So-called M-files in MATLAB can be scripts that simply execute a series of MATLAB statements, or they can be functions that can accept arguments and ‘can produce one or more outputs The focus of this section in on M-file func- tions These functions extend the capabilites of both MATLAB and IPT toad- dress specific, user-defined applications. 210 w Introduction to M-Funetion Programming 39 Miles are created using a text editor and are stored with @ name of the form filenane.n, such as average. and filter.n, The components of a function M-fie are + The function definition line + The HI line + Help text + The function body © Comments “The function definition line has the form function [outputs] = nane(inputs) For example, « function to compute the sum and product (two different out puts) of two images would have the form funetion (8, p] = sunprod(t, 9) where f, and g are the input images, s is the sum image, and p is the product, image. The name sunprod is arbitrarily defined, but the word funetion always appears on the left, in the form shown. Note that the output arguments are en- closed by square brackets and the inputs are enclosed by parentheses. Ifthe function has a single output argument, it is acceptable to list the argument with- cut brackets. If the function has no output, only the word function is used, ‘without brackets or equal sign. Function names must begin with a letter, and the remaining characters can be any combination of letters, numbers, and un erscores. No spaces are allowed. MATLAB distinguishes function names up 063 characters long. Adcitional characters are ignored, Functions can be called at the command prompt; for example, >> [s, p] = sumprodit, 9); or they can be used as elements of other functions, in which case they become subfunctions, As noted in the previous paragraph, ifthe output has a single ar ‘gument, itis acceptable to write it without the brackets as in sun(x) ; ‘The HI line isthe first text line. ts a single comment line that follows the fanction detinition line. There can be no blank lines or leading spaces between the HI line and the function definition line, An example of an Hl line is, "© SUWPROD Computes the sum and product of two inages. As indicated in Section 1.7.3, the Hi line is the first text that appears when a wer types >> help tunctson_nane Shel 40 hopes? Fundamentals ot at the MATLAB prompt. Also, as mentioned in that section, typing lookfor keyword displays all the H lines containing the string keyword. This line pro- Vides important summary information about the M-file, soit should be as de~ scriptive as possible. Help texts a text block that follows the H1 line, without any blank lines in between the two, Help text is used to provide comments and online help for the function, When a user types help function_nare at the prompt, MAT- LAB displays all comment lines that appear between the function definition line and the first noncomment (executable or blank) line. The help system ig nores any comment lines that appear after the Help text block. "The function body contains all the MATLAB code that performs computa- ‘ions and assigns values to output arguments. Several examples of MATLAB ccode are given later in this chapter. Alllines preceded by the symbol “¥" that are not the Hi line or Help text are considered function comment lines and are not considered part of the Help text block. Itis permissible to append comments to the end of a line of code, ‘Mefiles can be created and edited using any text editor and saved with the extension .m in a specified directory, typically in the MATLAB search path. ‘Another way to ereate or edit an Mofile is to use the edit function at the prompt. For example, >> edit sunprod ‘opens for editing the file sunprod.mif the file exists in a directory thats in the MATLAB path or in the current directory. Ifthe file cannot be found, MAT- LAB gives the user the option to create it. As noted in Section 1.7.2, the MATLAB editor window has aumerous pull-down menus for tasks such as saving, viewing, and debugging files. Because it performs some simple checks ‘and uses color to differentiate between various elements of code, this texted tor is recommended as the tool of choice for writing and editing M-functions. 2.10.2 Operators MATLAB operators are grouped into three main categories: ‘* Arithmetic operators that perform numeric computations + Relational operators that compare operands quantitatively + Logical operators that perform the functions AND, OR, and NOT These are discussed in the remainder ofthis section. Arithmetic Operators MATLAB has (wo different types of arithmetic operations. Matrix arithmetie ‘operations are defined by the rules of linear algebra. Array arithmetic opera tions are carried out element by element and can be used with multidimen- sional arrays. The period (dot) character (.) distinguishes array operations from matrix operations For example, A'S indicates matrix multiplication in the traditional sense, whereas A. *B indicates array multiplication, in the sense that the result is an array, the same size as A and B, in which each element is the 210 a Introduction to M-Function Programming 41 oduct of corresponding elements of A and B. In other words, if C = A.*8, fhen C(1, J) = A(T, J)*8(Z, J). Because matrix and array operations are the same for addition and subtraction, the character pairs .+ and .— ‘When writing an expression such as B = A, MATLAB makes a js equal to A, but does not actually copy the data into B unless the contents of ‘Achange later in the program. This is an important point because using dif- ferent variables to “store” the same information sometimes can enhance code clarity and readability. Thus the fact that MATLAB does not duplicate infor- tration unless itis absolutely necessary is worth remembering when writing MATLAB code. Table 2.4 lists the MATLAB arithmetic operators, where A MATLAB ‘Comments Operator fame Funetion and Examples + Amayandmatris—plus(A, 8) Br RATaoratA addition = Arrayand matrix minus(A, 8) a= BAB.AMa, subtraction ora-A, + Array multiplication tines(a, 8) "8,01, 3) PACT, J)*B(2, J) + Matrix multiplication mtanes(A, 8) A*8, standard matrix multiplication, or a*A, ‘multiplication ofa scalar times all elements of > [py pmax, pmin, pn] = improd(t, 9) "InMATLAS documentation it incstomay to We dppercate charactersin the He and in Help ext ses etry lini nasa urea Ths def ok oie heen Poem $6 Chapter? os Funclamentals EXAMPLE 28 [Logical operator. EXAMPLE 29 Epgical functions, TABLE 2.8 Logical functions 8 Consider the AND operation on the following numeric arrays: A > Be [1 20; 0.451; “23; 0111; 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 land 0s elsewhere. Note that all operations are done on pairs of corresponding elements of the arrays, as before. “The OR operator works in a similar manner, An OR expression is true i ther operand is a logical 1 or nonzero numerical quantity, or if they both are logical 1s or nonzero numbers; otherwise it is false. The NOT operator works with a single input. Logically ifthe operand is true, the NOT operator converts itto false, When using NOT with numeric data, any nonzero operand becomes 0, and any zero operand becomes 1 = MATLAB also supports the logical functions summarized in Table 28. The a1 and any functions are particularly useful in programming, 1 Consider the simple arrays A = [1 2 3; 4 § 6]andB = (0-1 1; 00 2) ‘Substituting these arrays into the functions in Table 2.8 yield the following results: >> xor(A, 8) Function Comments or (exclusive OR) ‘The xor funetin returns | only ifboth operands are logiealy different; otherwise xor returns 20. aun The ail function returns {if all the elements in @ vector are nonzero; otherwise al} returns a0.This | function operates columawise on matrices. | any The any function returns a1 if any of the elements ina vector is nonzero; atherwise any eturns a0. This function operates columnvise on mateces. 2.10 w Introduction to M-Function Programming 47 >> all(A) ans = 44 >> any(A) ans = 44 >> all(8) ans = OPO >> any(B) ot Note how functions a1.1 and any operate on columns of A and 8. For instance, the first two elements of the vector produced by a11(B) are 0 because each of the first two columns of 6 contains at least one 0; the last element is 1 be- ‘cause all elements in the last column of 8 are nonzero, 7 In addition to the functions listed in Table 2.8, MATLAB provides a number of other functions that test for the existence of specific conditions fr values and return logical results. Some of these functions are listed in Table 2.9. A few of them deal with terms and concepts discussed earlier in this chapter (for example, see function islogical in Section 2.6.2); others are used in subsequent discussions. Keep in mind that the functions listed in Table 2.9 return a logical 1 when the condition being tested is true; other. wise they return a logical 0. When the argument is an array, some of the functions in Table 2.9 yield an array the same size as the argument contain- ‘ng logical 1s in the locations that satisfy the test performed by the function, and logical 0s elsewhere. For example, if A= {1 2; 9 1/0], the function Astinite(A) returns the matrix [1 1; 1 0], where the 0 (false) entry indi cates that the last element of A is not finite Some Important Variables and Constants, ‘The entries in Table 2.10 are used extensively in MATLAB programming, For ‘example, eps typically is added to denominators in expressions to prevent ‘overflow in the event that a denominator becomes zero, 8 Gupter? m Fundamentals “ABLE 29 ‘ome functions hat return a .sgical Lora “geal 0 lepending on ‘ether the value rcondition it “bei arguments retrusor false. See online elp fora “omplet list, ABLE 2.10 Some important riables and stants senumerse(A) Function Descriptio scei1(c) Tre if isa cel array. Ascellste(s) ‘Te ifs isa coll array of strings sehae(s) “True it isa character string, ssenpty(A) ‘True ifAis the empty array, J Aeequal(A, 6) TrueifAand8 have identical elements and dimensions. isfiels(S, ‘nane") Tue if ‘name! isa fied ofsructure 8. istinste(a) “Truc in the locations of array A tat are finite. Asint ia “True in the locations of array A that are infinite, sstetter(A) ‘True inthe locations of A that are letters ofthe alphabet. islogical(a) ‘True if Aisa logical array. enenber(A, 8) True in locations where elements of Aare also in 8. ssnan(a) ‘True inthe locations ofA that are NaNs (sce Table 2.10 for definition of Ha. ‘True if Aisa numeric array. ssprino(A) “True in locations of A that ate prime numbers ssreal(a) “True if the elements of Ahave no imaginary parts sspace(A) “True at locations where the elements of Aare whitespace characters Assparse(A) ‘True if Aisa sparse matrix sestruct(s) ‘Tre isis structure. ‘Runetion ‘Value Returned ans ‘Most recent answer (variable). Ifno output variable is assigned to an expression, MATLAB automatically stores the result in ans. ops Floating-point relative accuracy. Tiss the distance between 1.0 and the next largest number representable using double-precsion floating point. A(0r}) Imaginary unit, asin 1 +24. NaNornan Stands for Nots-Number (¢2, 0/0). pi 3.14159265358979 realnex The largest floating-point number that your computer can represent reainin The smallest floating-point number that your computer can represent computer Your computer type. version MATLAB version string 210 m Introduction to M-Function Programming 49 Number Representation MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter ‘to specify a power-of-ten scale factor. Imaginary numbers use either io j as, suffix. Some examples of valid number representations are 3 99 0.0001 9.697298 1.60210e-20 6.02252022 ui 8.14159) 3e5i All numbers are stored internally using the long format specified by the Insti- tute of Electrical and Electronics Engineers (IEEE) floating-point standard. Floating-point numbers have a finite precision of roughly 16 significant deci smal digits and a finite range of approximately 10*8 to 10°, 210.3 Flow Control ‘The ability to control the flow of operations based on a set of predefined con- ditions is at the heart of all programming languages, In fact, conditional branching was one of two key developments that led to the formulation of general-purpose computers in the 1940s (the other development was the use ‘of memory to hold stored programs and data), MATLAB provides the eight {low control statements summarized in Table 2.11. Keep in mind the observa- tion made in the previous section that MATLAB treats a logical 1 or nonzero ‘number as true, and a logical or numeric 0 as false. Statement Deseiption ae ‘Af. together with e1¢0 and eise:f, executes « group of statements based on a specified logical condition. tor Executes a group of statements a fixed (specified) number of times while [Executes a group of statements an indefinite numberof times, based om a specified logical condition, break ‘Terminates execution of for or whste loop. continue Passes control tothe next iteration of a for ot white loop, skipping any remaining statements in the body of the loop. switen ‘riteh; together with case aul ut ernise,execues diferent _groUps of statements, depending on a specified value or string return ‘Causes execution to return tothe invoking function, try.-.caten Changes flow contol i an error i detected during execution, TABLE 2.11 Flow control statements 50 Ghapler 2. Fundamentals EXAMPLE 21 Conditional branching and introduction of functions error, engeh, and une, if, else, and elseif Conditional statement i has the syntax Af expression statenents end ‘The expression is evaluated and, if the evaluation yields true, MATLAB ex- cecutes one of more commands, denoted here as staiements, between the if ‘and end lines. If expression is false, MATLAB skips all the statements be- ‘ween the if and end lines and resumes execution at th line following the end line, When nesting 45, each 1¢ must be paired with a matching end. ‘The else and elseif statements further conditionalize the if staterment, "The general syntax is Af expression? ‘statenents? elseif expression? statements? else statementsd eng expression? is true, statements? are executed and control is transferred to the end statement. If expressiont evaluates to false, then expression? is evaluated, If this expression evaluates to true, then statements? are exe cuted and control is transferred to the end statement. Otherwise (ese) statenontsd are executed. Note that the else statement has no condition. The else and elseif statements can appear by themselves after an if state- ‘ment; they do not need to appear in pairs, as shown in the preceding general syntax. Iti acceptable to have multiple else4f statements. 8 Suppose that we want to write a function that computes the average inten- sity of an image, As discussed earlier, a two-dimensional array f can be con- verted to a column vector, v, by letting v = f(:). Therefore, we want our function to be able to work with both vector and image inputs. The program should produce an error if the input is not a one- or two-dimensional array. function av = average (A) SAVERAGE Computes the average value of an array. % AV = AVERAGE(A) computes the average value of input % array, A, which must be a 1-0 or 2-D array. % Check the validity of the input. (Keep an ming that % a 1-0 array ds a special case of a 2-0 array.) if ndims(a) > 2 errer('The dimensions of the input cannot exceed 2.") end 210 @ Introduction to M-Function Programming 51 ‘s compute the average | Neertuttatth Heneentaccays bien ote that he fput is converted t a LD aray by wsing (2) In general Noy it tees an a of ie oagntcinein feag tin acest ean anti atau are racoer wo ae ita wueher Ge coe Nayear ee TD aaece ayo obai the mmber ef ements in an ary deel Be feet museh whee oan een ay in Thus. if Ais an image, nune2 (A) gives its number of pixels. Using this function, the last executable line of the previous program becomes sum A(z} /numel (A) Finally, note that the error function terminates execution of the program and outputs the message contained within the parentheses (the quotes shown are required), = for [As indicated in Table 2.11, for loop executes a group of statements a speci- fied number of times. The syntax is for index = start:increnont:end statements ona Itis possible to nest two or more for loops, as follows: for indext = start?:incrementt :end statements? for index2 statonents2 end additional loop1 statements end tart2:increment2:end For example, the following loop executes 11 times: count = 0; fork = 020.41 count = count + 1; ong 52. hope? Fundamentals EXAMPLE 2.11 Using 2 for loop townte multiple images to file See the help page for ymax forms appli ‘eto the fenton If the loop increment is omitted, itis taken to be I. Loop increments also can be negative, as in k = 0:~1:~10. Note that no semicolon is necessary at the end ofa for line. MATLAB automatically suppresses printing the values of a loop index. As discussed in detail in Section 2.10.4, considerable gains in program execution speed can be achieved by replacing for loops with so-called vectorized code whenever possible. 1 Example 2.2 compared several images using different JPEG quality val- ues. Here, we show how to write those files to disk using a for loop. Suppose that we have an image, f,and we want to write it toa series of JPEG files with quality factors ranging from 0 to 100 in increments of 5, Further, suppose that We want to write the JPEG files with filenames of the form series_xxx. jpg, where xx is the quality factor. We can accomplish this using the following for loop: for_q = 0:5:100 filename = sprintf (‘series $36. jp9', 4) imirite(f, filenane, ‘quality’, 4); end Function sprint, whose syntax inthis case is 8 = sprintf (‘characterstandcharacters2', q) writes formatted data as a string, s. In this syntax form, characterst and characters? are chatacter strings, and 4nd denotes a decimal number (speci- fied by g) with n digits. In this example, characterst is series_,the value of nis, characters? is . jpg, and q has the values specified in the loop. m1 while A while loop executes a group of statements for as long as the expression controlling the loop is true. The syntax is while expression statements end As in the case of for, whize loops can be nested: while expression? ‘statements? while expression2 ‘statemente2 end) additional loop! statements end 210 w Introduction to M-Function Programming, For example, the following nested white loops terminate when both a and bhave been reduced to 0: Note that to control the foops we usedl MATLAB's convention of treating a sumerical value ina logical context as true when it s nonzero and as false when it is0.In other words, while a and white b evaluate to true as long as a and bare nonzero ‘Asin the case of for loops, considerable gains in program execution speed can be achieved by replacing while loops with vectorized code (Section 2104) whenever possible break Asits name implies, break terminates the execution of a for or white loop. ‘When a break statement is encountered, execution continues with the next statement outside the loop. In nested loops, break exits only from the inner: ‘mast loop that contains it ‘continue The continue statement passes control to the next iteration of the for or hile loop in which it appears, skipping any remaining statements in the body of the loop. In nested loops, continue passes control to the next iteration of the loop enclosing it switch This isthe statement of choice for controlling the flow of an M-function based ‘on different types of inputs The syntax is ‘Switch switch exoression case case expression statenent(s) Case (case_expression!, case expression2, statement (s) otherwise statement (s) ena 54 Gapter? 1 Fundamentals EXAMPLE 212: Extracting @ subimege from a sven image. ‘The switeh construct executes groups of statements based on the value of a variable or expression. The keywords case and otherwise delineate the 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 expressions are included in the same case statement. As a simple example, suppose that we have an M-funetin that accepts an image f and conversitto specified class, call t nenoLass. Only three image classes are acceptable for the conversion: wint8, uint 16, nnd double. The following code fragment per forms the desired conversion and outputs an error if the class of the input {mage is not one ofthe acceptable clases: switch neclass case ‘uints Q = anguinta(t); case "vint16" 9 = imguintt6(t); case ‘double 9 = imcouble(t); otherwise error('Unknown or improper image class.) ond ‘The switch construct is used extensively throughout the book, '% in this example we write an Mcfunction (based on for loops) to extract a rectangular subimage from an image. Although, as shown in the next section, ‘xe could do the extraction using a single MATLAB statement, we use the pre- sent example later to compare the speed between loops and vectorized cod. ‘The inputs to the function are an image, the size (number of rows and columns) of the subimage we want to extract, andthe coordinates ofthe top, leit comer of the subimage. Keep in mind thatthe image origin in MATLAB isat(1,1),as discussed in Section 2.11 function s = subin(f, m,n, rx, ey) SSUBIM Extracts a subimage, s, fron a given image, f. % The subimage is of size m-by-n, and the coordinates % of its top, left corner are (rx, cy) 5 = zeros(m, n); rouhigh = rk +m = 45 colhigh = cy + n= 41} xcount = 0; Tor r= ex:rownagn xcount = xcount + 1; youu = OF "Unlitete Clangnge etn conse, MATLAB evi ten doe not fal rough” Tost eni8ch cexeeates onthe fist matting ene subsequent ateing aes do not execute Therefore, ea sak imens arent we 2.10 a Introduction to M-Function Programming, for ¢ = cyscolnigh yeount = yeount + 1; s(xcount, yeount) end ond (ry )5 In the following section we give a significantly more efficient implementation of this code, As an exercise, the reader should implement the preceding pro- gram using while instead of for loops = 210A Code Optimization {As discussed in some detail in Section 13, MATLAB is a programming fan- tage specifically designed for array operations. Taking advantage of this fact whenever possible can result insignificant increases in computational speed. Tn this section we discuss two important approaches for MATLAB code op mization: vectorizing loops and prealocating arrays. Vectorizing Loops Vectorizing simply means converting for and while loops to equivalent vec~ 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 improve code readability. Although multidimensional vectorization can be dif- ficult to formulate at times, the forms of vectorization used in image process- ing generally are straightforward. ‘We begin with a simple example, Suppose that we want to generate a 1-D function of the form f(x) = A sin(x/2m) forx = .M ~ 1.A for loop to implement this computation is, 1,2) for x= 1: % Array indices in MATLAB cannot be 0. F(x) = ARSiN( (x = 1)/(2*94)); one However, this code can be made considerably more efficient by vectorizing that is, by taking advantage of MATLAB indexing, as follows: x= 0m ~ 1; t= atsin(x/ (2*p4)); As this simple example illustrates, 1-D indexing generally is a simple process. When the functions to be evaluated have two variables, optimized indexing is slightly more subtle. MATLAB provides a direct way to implement 2-D function evaluations via function neshgri, which has the syntax. Ic, A} = meshgrigic, r) Paina 38 56 Captor? Fundamentals This function transforms the domain specified by row vectors © and e into ar- rays C and R that can be used for the evaluation of functions of two variables and 3-D surface plots (note that columns are listed first in both the input and ‘output of meshgrid), ‘The rows of output array C are copies of the vector ¢, and the columns of the output array R are copies of the vector e. For example, suppose that we ‘want to form a 2-D function whose elements are the sum of the squares of the values of coordinate variables x and y for x=0, 1, 2andy=0, 1. The vee: {or ris formed from the row components of the coordinates: r= (0 1 2].Sim- ilarly, ¢ is formed from the column component of the coordinates: ¢ = {0 1] (keep in mind that both r and c are row vectors here). Substituting these two vectors into meshgrid results inthe following arrays >> [C, RJ= meshgrid(c, r) ° onset 04 o4 R 0 0 14 22 ‘The function in which we are interested is implemented as >> he RD + 0.2 which gives the following result h 01 12 45 ‘Note that the dimensions of h are Length(r) x Length (¢).Also note, for ex- ample, that (1,1) = R(1,1)°2 + (1, 4)~2. Thus, MATLAB automatically 100k care of indexing h. Ths is a potential source for confusion when Qs are in volved in the coordinates because of the repeated warnings in this book and in ‘manuals that MATLAB asays cannot have 0 indices. AS this simple iustra- tion shows, when forming h, MATLAB used the contents of ® and ¢ for com pputations. The indices of h, R, and ©, started at 1. The power of this indexing scheme is demonstrated in the following example. 210 Iniroduction to M-Function rogramuning 57 Bn this example we write an M-funtion to compare the implementation of | EXAMPLE2t% thefoowing two-dimensional image function wing For lopeand vectorzaton: Aaron ‘he comparatina fay) vectorization, and invuducion ot timing functions tic and toc. tetning “Te function inputs are A, wy, M and N.The desired outputs are the im- "wien te and the time i takes to implement the function with For loops tothe time it takes to implement i using vectorization. The solution isas follows , N= 1, We also introduce the Asin(ugx + ay) advantages of for x = 0,1,2,...,M ~ Land y = 0,1,2, the timing ages generated by both methods (they should be identical), and the ratio of function [rt, f, 9) = twodsin(A, uo, vO, M,N) STHOOSIN Corpares for loops vs. vectorization % The comparison is based on inplenenting the function S £04 y) = ASin(uOK + voy) for x= 0, 1, 2,0.) M1 and Sy ='0, 1, 2,-0.5 N— 1. The inputs to the function are % WM and'N and'the ‘constants in the function, % First implement using for loops. tic % Start timing. for c= 42M WO = uor(r — 1); for c= 1:N Woy = vor(e = 1); f(r, ¢) = A*sin(uox + voy); end ond t= too; % End timing. asf ‘Now seplement using vectorization. Call the image g ‘ tic 4 Start timing. 0. 0 R] = meshgrid(c, r); Atsin(uO*R + ORC); toc; % End timing. % Compute the ratio of the two tines. rt t1/(t2 + eps); % Use eps in case t2 is close to 0. ‘Running this function at the MATLAB prompt, >> (ety fy g] = twodsin(s, 1/(4*pip, 1/(4*pi), $12, 512);

You might also like