Image Processing and Acquisition using Python Chapman & Hall/CRC The Python Series About the Series Python has been ranked as the most popular programming language, and it is widely used in education and industry. This book series will offer a wide range of books on Python for students and professionals. Titles in the series will help users learn the language at an introductory and advanced level, and explore its many applications in data science, AI, and machine learning. Series titles can also be supplemented with Jupyter notebooks. Image Processing and Acquisition using Python, Second Edition Ravishankar Chityala, Sridevi Pudipeddi For more information about this series please visit: https://www.crcpress.com/Chapman--HallCRC/book-series/PYTH Image Processing and Acquisition using Python Second Edition Ravishankar Chityala IonPath and University of California Santa Cruz Sridevi Pudipeddi University of California, Berkeley MATLAB(cid:13)R isatrademarkofTheMathWorks,Inc.andisusedwithpermission.TheMathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or discus- sionofMATLAB(cid:13)R softwareorrelatedproductsdoesnotconstituteendorsementorsponsorship by The MathWorks of a particular pedagogical approach or particular use of the MATLAB(cid:13)R software. Firsteditionpublished2021 byCRCPress 6000BrokenSoundParkwayNW,Suite300,BocaRaton,FL33487-2742 andbyCRCPress 2ParkSquare,MiltonPark,Abingdon,Oxon,OX144RN (cid:13)c 2021Taylor&FrancisGroup,LLC CRCPressisanimprintofTaylor&FrancisGroup,LLC Reasonableeffortshavebeenmadetopublishreliabledataandinformation,buttheauthorand publishercannotassumeresponsibilityforthevalidityofallmaterialsortheconsequencesoftheir use. The authors and publishers have attempted to trace the copyright holders of all material reproducedinthispublicationandapologizetocopyrightholdersifpermissiontopublishinthis form has not been obtained. If any copyright material has not been acknowledged please write andletusknowsowemayrectifyinanyfuturereprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, repro- duced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now knownorhereafterinvented,includingphotocopying,microfilming,andrecording,orinanyinfor- mationstorageorretrievalsystem,withoutwrittenpermissionfromthepublishers. Forpermissiontophotocopyorusematerialelectronicallyfromthiswork,accesswww.copyright .comorcontacttheCopyrightClearanceCenter,Inc.(CCC),222RosewoodDrive,Danvers,MA 01923,978-750-8400.ForworksthatarenotavailableonCCCpleasecontactmpkbookspermis- [email protected] Trademark notice: Product or corporate names may be trademarks or registered trademarks, andareusedonlyforidentificationandexplanationwithoutintenttoinfringe. LibraryofCongressCataloging-in-PublicationData Names:Chityala,Ravishankar,author.|Pudipeddi,Sridevi,author. Title:ImageprocessingandacquisitionusingPython/RavishankarChityala andSrideviPudipeddi. Description:Secondedition.|BocaRaton:Chapman&Hall/CRCPress,2020. |Series:Chapman&Hall/CRCthePythonseries|Includes bibliographicalreferencesandindex. Identifiers:LCCN2020015556|ISBN9780367198084(hardback)|ISBN 9780429243370(ebook) Subjects:LCSH:Imageprocessing.|Python(Computerprogramlanguage) Classification:LCCTA1637.C4862020|DDC006.6/63--dc23 LCrecordavailableathttps://lccn.loc.gov/2020015556 ISBN:9780367198084(hbk) ISBN:9780429243370(ebk) Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com To our parents Contents Foreword xvii Preface xix Preface to the First Edition xxi Introduction xxv Authors xxvii List of Symbols and Abbreviations xxix I Introduction to Images and Computing using Python 1 1 Introduction to Python 3 1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 What Is Python? . . . . . . . . . . . . . . . . . . . . . 3 1.3 Python Environments . . . . . . . . . . . . . . . . . . 5 1.3.1 Python Interpreter . . . . . . . . . . . . . . . . 5 1.3.2 Anaconda Python Distribution . . . . . . . . . 6 1.4 Running a Python Program . . . . . . . . . . . . . . . 6 1.5 Basic Python Statements and Data Types . . . . . . . 7 1.5.1 Data Structures . . . . . . . . . . . . . . . . . . 11 1.5.2 File Handling . . . . . . . . . . . . . . . . . . . 16 1.5.3 User-Defined Functions . . . . . . . . . . . . . . 18 1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 20 vii viii Contents 2 Computing using Python Modules 23 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 23 2.2 Python Modules . . . . . . . . . . . . . . . . . . . . . 23 2.2.1 Creating Modules . . . . . . . . . . . . . . . . . 24 2.2.2 Loading Modules . . . . . . . . . . . . . . . . . 24 2.3 Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.3.1 Numpy Array or Matrices? . . . . . . . . . . . . 30 2.4 Scipy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.5 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.6 Python Imaging Library . . . . . . . . . . . . . . . . . 32 2.7 Scikits . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.8 Python OpenCV Module . . . . . . . . . . . . . . . . 33 2.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 34 3 Image and Its Properties 37 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 37 3.2 Image and Its Properties . . . . . . . . . . . . . . . . . 38 3.2.1 Bit-Depth . . . . . . . . . . . . . . . . . . . . . 38 3.2.2 Pixel and Voxel . . . . . . . . . . . . . . . . . . 39 3.2.3 Image Histogram . . . . . . . . . . . . . . . . . 41 3.2.4 Window and Level . . . . . . . . . . . . . . . . 42 3.2.5 Connectivity: 4 or 8 Pixels . . . . . . . . . . . . 43 3.3 Image Types . . . . . . . . . . . . . . . . . . . . . . . 44 3.3.1 JPEG . . . . . . . . . . . . . . . . . . . . . . . 44 3.3.2 TIFF . . . . . . . . . . . . . . . . . . . . . . . . 45 3.3.3 DICOM . . . . . . . . . . . . . . . . . . . . . . 45 3.4 Data Structures for Image Analysis . . . . . . . . . . . 49 3.5 Reading, Writing and Displaying Images . . . . . . . . 49 3.5.1 Reading Images . . . . . . . . . . . . . . . . . . 49 3.5.2 Reading DICOM Images using pyDICOM . . . 51 3.5.3 Writing Images . . . . . . . . . . . . . . . . . . 52 3.5.4 Writing DICOM Images using pyDICOM . . . . 52 3.5.5 Displaying Images . . . . . . . . . . . . . . . . . 53 Contents ix 3.6 Programming Paradigm . . . . . . . . . . . . . . . . . 53 3.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 56 3.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 57 II Image Processing using Python 59 4 Spatial Filters 61 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 61 4.2 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . 62 4.2.1 Mean Filter . . . . . . . . . . . . . . . . . . . . 64 4.2.2 Median Filter . . . . . . . . . . . . . . . . . . . 68 4.2.3 Max Filter . . . . . . . . . . . . . . . . . . . . . 70 4.2.4 Min Filter . . . . . . . . . . . . . . . . . . . . . 72 4.3 Edge Detection using Derivatives . . . . . . . . . . . . 73 4.3.1 First Derivative Filters . . . . . . . . . . . . . . 74 4.3.1.1 Sobel Filter . . . . . . . . . . . . . . . 76 4.3.1.2 Prewitt Filter . . . . . . . . . . . . . . 78 4.3.1.3 Canny Filter . . . . . . . . . . . . . . 80 4.3.2 Second Derivative Filters . . . . . . . . . . . . . 82 4.3.2.1 Laplacian Filter . . . . . . . . . . . . . 83 4.3.2.2 Laplacian of Gaussian Filter . . . . . . 86 4.4 Shape Detecting Filter . . . . . . . . . . . . . . . . . . 89 4.4.1 Frangi Filter . . . . . . . . . . . . . . . . . . . . 89 4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 91 4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 92 5 Image Enhancement 95 5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 95 5.2 Pixel Transformation . . . . . . . . . . . . . . . . . . . 95 5.3 Image Inverse . . . . . . . . . . . . . . . . . . . . . . . 97 5.4 Power Law Transformation . . . . . . . . . . . . . . . 99 5.5 Log Transformation . . . . . . . . . . . . . . . . . . . 102 5.6 Histogram Equalization . . . . . . . . . . . . . . . . . 104