Digital Image Processing with C++ Digital Image Processing with C++: Implementing Reference Algorithms with the Clmg Library presents the theory of digital image processing and implementations of algorithms using a dedi- cated library. Processing a digital image means transforming its content (denoising, stylizing, etc.) or extracting information to solve a given problem (object recognition, measurement, mo- tion estimation, etc.). This book presents the mathematical theories underlying digital image processing as well as their practical implementation through examples of algorithms implement- ed in the C++ language using the free and easy-to-use CImg library. Chapters cover the field of digital image processing in a broad way and propose practical and functional implementations of each method theoretically described. The main topics covered include filtering in spatial and frequency domains, mathematical morphology, feature extraction and applications to segmentation, motion estimation, multispectral image processing and 3D visualization. Students or developers wishing to discover or specialize in this discipline and teachers and re- searchers hoping to quickly prototype new algorithms or develop courses will all find in this book material to discover image processing or deepen their knowledge in this field. David Tschumperlé is a permanent CNRS research scientist heading the IMAGE team at the GREYC Laboratory in Caen, France. He’s particularly interested in partial differential equations and variational methods for processing multi-valued images in a local or non-local way. He has authored more than 40 papers in journals or conferences and is the project leader of CImg and G’MIC, two open-source software/libraries. Christophe Tilmant is associate professor in computer science at Clermont-Auvergne Univer- sity. His research activities include image processing and artificial intelligence, where he has au- thored more than 30 papers. His teaching includes deep learning, image processing and network security. He participates or leads several French research programs. Vincent Barra is a full professor in computer science at Clermont-Auvergne University and associate director of the LIMOS Lab. He teaches artificial intelligence and image processing in engineering schools and master’s programs. His research activities focus on n-dimensional data analysis with methodological and application aspects in various fields. He has authored more than 90 papers in journals or conferences and participates or leads several French and European research programs. Taylor & Francis Taylor & Francis Group http://taylorandfrancis.com Digital Image Processing with C++ Implementing Reference Algorithms with the CImg Library David Tschumperlé Christophe Tilmant Vincent Barra First edition published 2023 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742 and by CRC Press 4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN © 2023 David Tschumperlé, Christophe Tilmant and Vincent Barra CRC Press is an imprint of Taylor & Francis Group, LLC Title of the original French edition, Le traitement numerique des images en C++. Implementation d’algorithmes avec la bibliotheque CIMG- published by Ellipses - Copyright 2021, Edition Marketing S. A. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publica- tion and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, trans- mitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750- 8400. For works that are not available on CCC please contact [email protected] Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. ISBN: 978-1-032-34752-3 (hbk) ISBN: 978-1-032-34753-0 (pbk) ISBN: 978-1-003-32369-3 (ebk) DOI: 10.1201/9781003323693 Typeset in Nimbus Roman by KnowledgeWorks Global Ltd. Publisher’s note: This book has been prepared from camera-ready copy provided by the author. Contents Preface ............................................. xi Preamble ........................................... xv I INTRODUCTION TO CImg 1 Introduction .......................................... 3 2 Getting Started with the CImg Library ................. 17 2.1 Objective: subdivide an image into blocks 17 2.2 Setup and first program 18 2.3 Computing the variations 19 2.4 Computing the block decomposition 24 2.5 Rendering of the decomposition 26 2.6 Interactive visualization 31 2.7 Final source code 36 II IMAGE PROCESSING USING CImg 3 Point Processing Transformations ...................... 43 3.1 Image operations 43 3.1.1 Mathematicaltransformations . . . . . . . . . . . . . . . . . . . . . . . . 43 3.1.2 Bitwisetransformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.1.3 Contrastenhancement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 vi Table of contents 3.2 Histogram operations 48 3.2.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.2.2 Histogramspecification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.2.3 Localhistogramspecification . . . . . . . . . . . . . . . . . . . . . . . . . 50 4 Mathematical Morphology ........................... 53 4.1 Binary images 54 4.1.1 Dilationanderosion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.1.2 Openingandclosing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.2 Gray-level images 58 4.3 Some applications 59 4.3.1 Kramer-Brucknerfilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.3.2 Alternatingsequentialfilters . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.3.3 Morphologicalgradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 4.3.4 Skeletonization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 5 Filtering ............................................. 69 5.1 Spatial filtering 69 5.1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.1.2 Low-passfilters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.1.3 High-passfilters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.1.4 Adaptivefilters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 5.1.5 Adaptivewindowfilters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.2 Recursive filtering 84 5.2.1 Optimaledgedetection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.2.2 Derichefilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 5.3 Frequency filtering 94 5.3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 5.3.2 TheFouriertransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 5.3.3 Frequencyfiltering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.3.4 ProcessingaMoiréimage . . . . . . . . . . . . . . . . . . . . . . . . . . 105 5.4 Diffusion filtering 110 5.4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 5.4.2 Physicalbasisofdiffusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 5.4.3 Lineardiffusionfilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Table of contents vii 5.4.4 Non-lineardiffusionfilterintwodimensions. . . . . . . . . . . . . . 114 5.4.5 Non-lineardiffusionfilteronavideosequence . . . . . . . . . . 117 6 Feature Extraction .................................. 121 6.1 Points of interest 121 6.1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.1.2 HarrisandStephensdetector . . . . . . . . . . . . . . . . . . . . . . . . 122 6.1.3 ShiandTomasialgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 6.1.4 Pointsofinterestwithsub-pixelaccuracy . . . . . . . . . . . . . . . 127 6.2 Hough transform 128 6.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 6.2.2 Linedetection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 6.2.3 Circleandellipsedetection . . . . . . . . . . . . . . . . . . . . . . . . . 134 6.3 Texture features 137 6.3.1 Texturespectrum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6.3.2 Tamuracoefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 6.3.3 Localbinarypattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 6.3.4 Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 7 Segmentation ...................................... 151 7.1 Edge-based approaches 151 7.1.1 Introductiontoimplicitactivecontours . . . . . . . . . . . . . . . . 151 7.1.2 Implicitrepresentationofacontour . . . . . . . . . . . . . . . . . . . 156 7.1.3 Evolutionequation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 7.1.4 Discretizationoftheevolutionequation . . . . . . . . . . . . . . . . 160 7.1.5 Geodesicmodelpropagationalgorithm . . . . . . . . . . . . . . . 161 7.2 Region-based approaches 163 7.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 7.2.2 Histogram-basedmethods . . . . . . . . . . . . . . . . . . . . . . . . . . 163 7.2.3 Thresholdingbyclustering . . . . . . . . . . . . . . . . . . . . . . . . . . 167 7.2.4 Transformationofregions . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 7.2.5 Super-pixelspartitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 viii Table of contents 8 Motion Estimation ................................... 183 8.1 Optical flow: dense motion estimation 183 8.1.1 Variationalmethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 8.1.2 LucasandKanadedifferentialmethod . . . . . . . . . . . . . . . . 189 8.1.3 Affineflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 8.2 Sparse estimation 195 8.2.1 Displacementfieldusingspatialcorrelation . . . . . . . . . . . . . 196 8.2.2 Displacementfieldusingphasecorrelation . . . . . . . . . . . . . 198 8.2.3 Kalmanfiltering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 9 Multispectral Approaches ........................... 209 9.1 Dimension reduction 209 9.1.1 Principalcomponentanalysis. . . . . . . . . . . . . . . . . . . . . . . . 210 9.1.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 9.2 Color imaging 213 9.2.1 Colorimetricspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 9.2.2 Medianfilteringincolorimaging . . . . . . . . . . . . . . . . . . . . . 218 9.2.3 Edgedetectionincolorimaging . . . . . . . . . . . . . . . . . . . . . 220 10 3D Visualisation .................................... 227 10.1 Structuring of 3D mesh objects 227 10.2 3D plot of a function z= f(x,y) 229 10.3 Creating complex 3D objects 233 10.3.1 Detailsonvertexstructuring . . . . . . . . . . . . . . . . . . . . . . . . . 233 10.3.2 Detailsonprimitivestructuring . . . . . . . . . . . . . . . . . . . . . . . 234 10.3.3 Detailsonmaterialstructuring . . . . . . . . . . . . . . . . . . . . . . . 235 10.3.4 Detailsonopacitystructuring . . . . . . . . . . . . . . . . . . . . . . . . 235 10.4 Visualization of a cardiac segmentation in MRI 236 10.4.1 Descriptionoftheinputdata . . . . . . . . . . . . . . . . . . . . . . . . 236 10.4.2 Extractionofthe3Dsurfaceoftheventricle . . . . . . . . . . . . 237 10.4.3 Adding3Dmotionvectors . . . . . . . . . . . . . . . . . . . . . . . . . . 238 10.4.4 Addingcuttingplanes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 10.4.5 Finalresult . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Table of contents ix 11 And So Many Other Things... ....................... 243 11.1 Compression by transform (JPEG) 243 11.1.1 Introduction-Compressionbytransform . . . . . . . . . . . . . . . 243 11.1.2 JPEGAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 11.1.3 Discretecosinetransformandquantization . . . . . . . . . . . . . 245 11.1.4 SimplifiedJPEGalgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 11.2 Tomographic reconstruction 252 11.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 11.2.2 Analyticaltomographicreconstruction . . . . . . . . . . . . . . . . 254 11.2.3 Algebraictomographicreconstruction . . . . . . . . . . . . . . . . 259 11.3 Stereovision 264 11.3.1 Epipolargeometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 11.3.2 Depthestimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 11.4 Interactive deformation using RBF 273 11.4.1 Goaloftheapplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 11.4.2 TheRBF interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 11.4.3 RBF forimagewarping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 11.4.4 Userinterfaceforkeypointmanagement. . . . . . . . . . . . . . . 277 List of CImg Codes ................................. 281 Bibliography ....................................... 285 Index .............................................. 289