0% found this document useful (0 votes)
94 views

DigitalImageProcessingUsingMatlab Muya

pengcit matlab

Uploaded by

Neas Ginting
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
94 views

DigitalImageProcessingUsingMatlab Muya

pengcit matlab

Uploaded by

Neas Ginting
Copyright
© Attribution Non-Commercial (BY-NC)
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.

You might also like