Kenneth Dawson-Howe 20 10 0 -10 -20 40 20 80 0 0 20 40 60 80 A Practical Introduction to ComPuter VIsIon wItH oPenCV A PRACTICAL INTRODUCTION TO COMPUTER VISION WITH OPENCV A PRACTICAL INTRODUCTION TO COMPUTER VISION WITH OPENCV KennethDawson-Howe TrinityCollegeDublin,Ireland Thiseditionfirstpublished2014 ©2014JohnWiley&SonsLtd Registeredoffice JohnWiley&SonsLtd,TheAtrium,SouthernGate,Chichester,WestSussex,PO198SQ,UnitedKingdom Fordetailsofourglobaleditorialoffices,forcustomerservicesandforinformationabouthowtoapplyfor permissiontoreusethecopyrightmaterialinthisbookpleaseseeourwebsiteatwww.wiley.com. TherightoftheauthortobeidentifiedastheauthorofthisworkhasbeenassertedinaccordancewiththeCopyright, DesignsandPatentsAct1988. Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted,inany formorbyanymeans,electronic,mechanical,photocopying,recordingorotherwise,exceptaspermittedbytheUK Copyright,DesignsandPatentsAct1988,withoutthepriorpermissionofthepublisher. Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappearsinprintmaynotbe availableinelectronicbooks. Designationsusedbycompaniestodistinguishtheirproductsareoftenclaimedastrademarks.Allbrandnamesand productnamesusedinthisbookaretradenames,servicemarks,trademarksorregisteredtrademarksoftheir respectiveowners.Thepublisherisnotassociatedwithanyproductorvendormentionedinthisbook. LimitofLiability/DisclaimerofWarranty:Whilethepublisherandauthorhaveusedtheirbesteffortsinpreparing thisbook,theymakenorepresentationsorwarrantieswithrespecttotheaccuracyorcompletenessofthecontentsof thisbookandspecificallydisclaimanyimpliedwarrantiesofmerchantabilityorfitnessforaparticularpurpose.Itis soldontheunderstandingthatthepublisherisnotengagedinrenderingprofessionalservicesandneitherthe publishernortheauthorshallbeliablefordamagesarisingherefrom.Ifprofessionaladviceorotherexpert assistanceisrequired,theservicesofacompetentprofessionalshouldbesought. LibraryofCongressCataloging-in-PublicationDataappliedfor. ISBN:9781118848456 Setin10/12ptTimesbyAptaraInc.,NewDelhi,India 1 2014 Iamgrateful to many peoplefortheirhelp andsupport during the writing ofthisbook. Thebiggest thanksmust goto mywife Jane,mychildren, WilliamandSusie,andmyparents, allofwhoseencouragement hasbeenunstinting. Imustexpressmythankstomystudentsfortheirinterestandenthusiasm inthis subject.Itisalwaysrefreshing tohearstudents discussinghowto solvevision problems intutorials andgreatto heartheirsolutions to problems which areoftendifferent (andsometimes better)than my own. Ithankmy colleagues(in particularArthur Hughes,Jeremy Jonesand Hilary McDonald)fortheirencouragement andsupport. Contents Preface xiii 1 Introduction 1 1.1 ADifficultProblem 1 1.2 TheHumanVisionSystem 2 1.3 PracticalApplicationsofComputerVision 3 1.4 TheFutureofComputerVision 5 1.5 MaterialinThisTextbook 6 1.6 GoingFurtherwithComputerVision 7 2 Images 9 2.1 Cameras 9 2.1.1 TheSimplePinholeCameraModel 9 2.2 Images 10 2.2.1 Sampling 11 2.2.2 Quantisation 11 2.3 ColourImages 13 2.3.1 Red–Green–Blue(RGB)Images 14 2.3.2 Cyan–Magenta–Yellow(CMY)Images 17 2.3.3 YUVImages 17 2.3.4 HueLuminanceSaturation(HLS)Images 18 2.3.5 OtherColourSpaces 20 2.3.6 SomeColourApplications 20 2.4 Noise 22 2.4.1 TypesofNoise 23 2.4.2 NoiseModels 25 2.4.3 NoiseGeneration 26 2.4.4 NoiseEvaluation 26 2.5 Smoothing 27 2.5.1 ImageAveraging 27 2.5.2 LocalAveragingandGaussianSmoothing 28 2.5.3 RotatingMask 30 2.5.4 MedianFilter 31 viii Contents 3 Histograms 35 3.1 1DHistograms 35 3.1.1 HistogramSmoothing 36 3.1.2 ColourHistograms 37 3.2 3DHistograms 39 3.3 Histogram/ImageEqualisation 40 3.4 HistogramComparison 41 3.5 Back-projection 43 3.6 k-meansClustering 44 4 BinaryVision 49 4.1 Thresholding 49 4.1.1 ThresholdingProblems 50 4.2 ThresholdDetectionMethods 51 4.2.1 BimodalHistogramAnalysis 52 4.2.2 OptimalThresholding 52 4.2.3 OtsuThresholding 54 4.3 VariationsonThresholding 56 4.3.1 AdaptiveThresholding 56 4.3.2 BandThresholding 57 4.3.3 Semi-thresholding 58 4.3.4 MultispectralThresholding 58 4.4 MathematicalMorphology 59 4.4.1 Dilation 60 4.4.2 Erosion 62 4.4.3 OpeningandClosing 63 4.4.4 Grey-scaleandColourMorphology 65 4.5 Connectivity 66 4.5.1 Connectedness:ParadoxesandSolutions 66 4.5.2 ConnectedComponentsAnalysis 67 5 GeometricTransformations 71 5.1 ProblemSpecificationandAlgorithm 71 5.2 AffineTransformations 73 5.2.1 KnownAffineTransformations 74 5.2.2 UnknownAffineTransformations 75 5.3 PerspectiveTransformations 76 5.4 SpecificationofMoreComplexTransformations 78 5.5 Interpolation 78 5.5.1 NearestNeighbourInterpolation 79 5.5.2 BilinearInterpolation 79 5.5.3 Bi-CubicInterpolation 80 5.6 ModellingandRemovingDistortionfromCameras 80 5.6.1 CameraDistortions 81 5.6.2 CameraCalibrationandRemovingDistortion 82