Image Operators Image Operators Image Processing in Python JasonM.Kinser CRCPress Taylor&FrancisGroup 6000BrokenSoundParkwayNW,Suite300 BocaRaton,FL33487-2742 ©2019byTaylor&FrancisGroup,LLC CRCPressisanimprintofTaylor&FrancisGroup,anInformabusiness NoclaimtooriginalU.S.Governmentworks Printedonacid-freepaper InternationalStandardBookNumber-13:978-1-4987-9618-7(Hardback) Thisbookcontainsinformationobtainedfromauthenticandhighlyregardedsources.Reasonableeffortshavebeenmade topublishreliabledataandinformation,buttheauthorandpublishercannotassumeresponsibilityforthevalidityofall materialsortheconsequencesoftheiruse.Theauthorsandpublishershaveattemptedtotracethecopyrightholdersofall materialreproducedinthispublicationandapologizetocopyrightholdersifpermissiontopublishinthisformhasnotbeen obtained.Ifanycopyrightmaterialhasnotbeenacknowledgedpleasewriteandletusknowsowemayrectifyinanyfuture reprint. ExceptaspermittedunderU.S.CopyrightLaw,nopartofthisbookmaybereprinted,reproduced,transmitted,orutilizedin anyformbyanyelectronic,mechanical,orothermeans,nowknownorhereafterinvented,includingphotocopying,micro- filming,andrecording,orinanyinformationstorageorretrievalsystem,withoutwrittenpermissionfromthepublishers. Forpermissiontophotocopyorusematerialelectronicallyfromthiswork,pleaseaccesswww.copyright.com(http://www. copyright.com/)orcontacttheCopyrightClearanceCenter,Inc.(CCC),222RosewoodDrive,Danvers,MA01923,978- 750-8400.CCCisanot-for-profitorganizationthatprovideslicensesandregistrationforavarietyofusers.Fororganizations thathavebeengrantedaphotocopylicensebytheCCC,aseparatesystemofpaymenthasbeenarranged. TrademarkNotice:Productorcorporatenamesmaybetrademarksorregisteredtrademarks,andareusedonlyforidentifi- cationandexplanationwithoutintenttoinfringe. LibraryofCongressCataloging-in-PublicationData Names:Kinser,JasonM.,1962-author. Title:Imageoperators:imageprocessinginPython/JasonM.Kinser. Description:Firstedition.|BocaRaton,FL:CRCPress/Taylor&FrancisGroup,[2019]| “ACRCtitle,partoftheTaylor&Francisimprint,amemberoftheTaylor&FrancisGroup, theacademicdivisionofT&FInformaplc.”|Includesbibliographicalreferencesandindex. Identifiers:LCCN2018017140(print)|LCCN2018017764(ebook)|ISBN9780429451188(eBook)| ISBN9780429835940(AdobePDF)|ISBN9780429835933(ePUB)| ISBN9780429835926(Mobipocket)|ISBN9781498796187(hardback:acid-freepaper) Subjects:LCSH:Imageprocessing—Digitaltechniques.|Python(Computerprogramlanguage) Classification:LCCTA1637(ebook)|LCCTA1637.K482019(print)|DDC006.6—dc23 LCrecordavailableathttps://lccn.loc.gov/2018017140 VisittheTaylor&FrancisWebsiteat http://www.taylorandfrancis.com andtheCRCPressWebsiteat http://www.crcpress.com Thisbookisdedicatedtomybeautifulwife–SueEllen. Contents PythonCodes...................................................................................................................................xv Preface.............................................................................................................................................xxi SoftwareandData.........................................................................................................................xxiii Author............................................................................................................................................xxv PART I Image Operators Chapter1 Introduction.............................................................................................................3 1.1 ScriptinginPython.........................................................................................3 1.2 Installation......................................................................................................4 1.2.1 ExampleCodes..................................................................................4 1.2.2 EstablishingaWorkSpace.................................................................4 1.2.3 TheSpyderInterface..........................................................................5 1.2.4 IntentoftheText................................................................................5 Chapter2 OperatorNomenclature...........................................................................................7 2.1 ImageNotation...............................................................................................7 2.2 Operators.........................................................................................................8 2.2.1 CreationOperators.............................................................................8 2.2.2 ChannelOperators.............................................................................9 2.2.3 InformationalOperators...................................................................12 2.2.4 IntensityOperators...........................................................................14 2.2.5 GeometricOperators........................................................................16 2.2.6 TransformationOperators................................................................16 2.2.7 ExpansionOperators........................................................................17 2.3 CombinationsandReducedNotation...........................................................18 2.4 Summary.......................................................................................................19 Chapter3 ScriptinginPython................................................................................................21 3.1 BasicPythonSkills.......................................................................................21 3.1.1 Variables...........................................................................................21 3.1.2 Strings..............................................................................................22 3.1.3 TypeConversionswithStrings........................................................23 3.2 Tuples,List,Dictionaries,andSets..............................................................23 3.2.1 Tuple................................................................................................23 3.2.2 Slicing..............................................................................................23 3.2.3 Lists..................................................................................................25 3.2.4 Dictionaries......................................................................................25 3.2.5 Sets...................................................................................................26 3.3 FlowControl.................................................................................................26 3.3.1 TheifCommand..............................................................................27 3.3.2 ThewhileCommand........................................................................28 vii viii Contents 3.3.3 BreakandContinue..........................................................................29 3.3.4 TheForLoop...................................................................................29 3.3.5 ThemapandlambdaFunctions.......................................................31 3.3.6 ImageOperatorsandControl...........................................................31 3.4 InputandOutput...........................................................................................32 3.4.1 ReadingandWritingTextFiles.......................................................32 3.4.2 PicklingFiles...................................................................................32 3.5 DefiningFunctions........................................................................................33 3.5.1 FunctionComponents......................................................................33 3.5.2 Returns.............................................................................................34 3.5.3 DefaultArguments...........................................................................35 3.5.4 FunctionHelp...................................................................................35 3.6 Modules........................................................................................................36 3.7 Errors............................................................................................................38 3.8 NumPy..........................................................................................................39 3.8.1 CreatingArrays................................................................................39 3.8.1.1 ZerosandOnes................................................................39 3.8.1.2 Random............................................................................40 3.8.1.3 GeometricShapes............................................................41 3.8.1.4 ConversionofNumericalData........................................41 3.8.2 ManipulatingArrays........................................................................42 3.8.2.1 DisplayOption.................................................................42 3.8.2.2 ConvertingArrays...........................................................42 3.8.2.3 SimpleMath....................................................................42 3.8.2.4 MultiplyingVectors.........................................................43 3.8.2.5 MultiplyingMatrices.......................................................44 3.8.2.6 ArrayFunctions...............................................................44 3.8.2.7 Decisions.........................................................................47 3.8.2.8 AdvancedSlicing.............................................................48 3.8.2.9 UniversalFunctions.........................................................48 3.8.2.10 Sorting.............................................................................49 3.8.3 Indices..............................................................................................51 3.9 SciPy.............................................................................................................52 3.9.1 LoadingandSavingImages.............................................................53 3.9.2 Examplesfromndimage..................................................................54 3.9.2.1 RotationandShift............................................................54 3.9.2.2 CenterofMass.................................................................55 3.10 Summary.......................................................................................................56 Chapter4 DigitalImages.......................................................................................................59 4.1 ImagesinPython..........................................................................................59 4.2 Resolution.....................................................................................................59 4.2.1 IntensityResolution.........................................................................59 4.2.2 SpatialResolution............................................................................61 4.3 DigitalFormats.............................................................................................63 4.3.1 Bitmaps............................................................................................63 4.3.2 JPEG................................................................................................63 4.3.3 GIF...................................................................................................63 4.3.4 TIFF.................................................................................................64 Contents ix 4.3.5 PNG..................................................................................................65 4.3.6 OtherCompressions.........................................................................65 4.4 Summary.......................................................................................................65 Chapter5 Color......................................................................................................................67 5.1 TheRGBColorModel.................................................................................67 5.2 TheHSVColorModel..................................................................................69 5.3 TheYUVFamily..........................................................................................72 5.4 CIEL*a*b*...................................................................................................73 5.5 ImprovementsinRecognition.......................................................................74 5.6 Summary.......................................................................................................77 PART II Image Space Manipulations Chapter6 GeometricTransformations...................................................................................81 6.1 Selections......................................................................................................81 6.2 LinearTranslation.........................................................................................83 6.2.1 SimpleShifting................................................................................83 6.2.2 NonIntegerShifts.............................................................................84 6.3 Scaling..........................................................................................................85 6.4 Rotation.........................................................................................................87 6.5 DilationandErosion.....................................................................................88 6.6 CoordinateMapping.....................................................................................90 6.7 PolarTransformations...................................................................................90 6.7.1 Theory..............................................................................................91 6.7.2 PythonImplementation....................................................................92 6.7.3 Example...........................................................................................94 6.8 PincushionandBarrelTransformations ......................................................95 6.9 OtherTransformations..................................................................................96 6.9.1 GenericTransformations..................................................................97 6.9.2 AffineTransformation......................................................................98 6.10 Summary.......................................................................................................99 Chapter7 ImageMorphing..................................................................................................101 7.1 Warp............................................................................................................101 7.1.1 MarkingFiducialPoints.................................................................101 7.1.2 ImageDancer.................................................................................101 7.1.3 DelaunayTessellation....................................................................103 7.1.4 ApplyingtheWarp.........................................................................104 7.2 AverageFace...............................................................................................106 7.3 ImageMorphing.........................................................................................107 Chapter8 PrincipleComponentAnalysis............................................................................111 8.1 ThePurposeofPCA...................................................................................111 8.2 CovarianceMatrix......................................................................................111 8.3 Eigenvectors................................................................................................112 8.4 PCA.............................................................................................................113