Python Image Processing Cookbook Over 60 recipes to help you perform complex image processing and computer vision tasks with ease Sandipan Dey BIRMINGHAM - MUMBAI Python Image Processing Cookbook Copyright © 2020 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Pravin Dhandre Acquisition Editor: Devika Battike Content Development Editor: Athikho Sapuni Rishana Senior Editor: Sofi Rogers Technical Editor: Manikandan Kurup Copy Editor: Safis Editing Project Coordinator: Aishwarya Mohan Proofreader: Safis Editing Indexer: Pratik Shirodkar Production Designer: Joshua Misquitta First published: April 2020 Production reference: 1170420 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78953-714-7 www.packt.com Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Fully searchable for easy access to vital information Copy and paste, print, and bookmark content Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. I dedicate this book to my beloved parents and all the teachers in my life from all the schools: Ballygunge Government High School, Kolkata, India Jadavpur University, Kolkata, India University of Maryland, Baltimore County, USA MOOC platforms like Coursera and edX Contributors About the author Sandipan Dey is a data scientist with a wide range of interests, covering topics such as machine learning, deep learning, image processing, and computer vision. He has worked in numerous data science fields, working with recommender systems, predictive models for the events industry, sensor localization models, sentiment analysis, and device prognostics. He earned his master's degree in computer science from the University of Maryland, Baltimore County, and has published in a few IEEE data mining conferences and journals. He has earned certifications from 100+ MOOCs on data science, machine learning, deep learning, image processing, and related courses. He is a regular blogger (sandipanweb) and is a machine learning education enthusiast. About the reviewers Srinjoy Ganguly holds a master's in Artificial Intelligence from the University of Southampton, UK and has over 4 years of experience in AI. He completed his bachelor's in Electronics and Communications from GGSIPU, Delhi, and gained a thorough knowledge of signal processing algorithms and neural networks. He has actively pursued the theory and principles of machine learning, deep learning, computer vision, and reinforcement learning. His research interests include Bayesian machine learning, Bayesian deep learning, and quantum machine learning. He is also an active contributor to Julia language. He strongly believes that this book will be an extremely valuable asset into the hands of beginners in the field of AI and computer vision. Shankar Jha is a software engineer who has helped a lot of beginners get to grips with Python through his articles and talks. He writes about Python, Django, tackling climate change, and poverty alleviation. He lives by the philosophy of Richard Feynman and Swami Vivekananda. His latest endeavor is to help facilitate the use of environment- friendly products. He believes this book will give you enough hands-on experience to jump start your career in computer vision. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Image Manipulation and Transformation 8 Technical requirements 8 Transforming color space (RGB → Lab) 9 Getting ready 9 How to do it... 9 Converting RGB image into grayscale by setting the Lab space color channels to zero 10 Changing the brightness of the image by varying the luminosity channel 11 How it works... 11 There's more... 12 Applying affine transformation 12 Getting ready 13 How to do it... 13 How it works... 15 There's more... 16 Applying perspective transformation and homography 16 Getting ready 17 How to do it... 17 How it works... 20 There's more... 20 See also 20 Creating pencil sketches from images 21 Getting ready 21 How to do it... 21 How it works... 24 There's more... 24 See also 25 Creating cartoonish images 25 Getting ready 26 How to do it... 26 How it works... 27 There's more... 28 See also 28 Simulating light art/long exposure 29 Getting ready 29 How to do it... 30 How it works... 32 There's more... 32 Table of Contents Extended depth of field with mahotas 32 See also 34 Object detection using color in HSV 34 Getting ready 35 How to do it... 35 How it works... 36 See also 38 Chapter 2: Image Enhancement 39 Applying filters to denoise different types of noise in an image 40 Getting ready 40 How to do it... 40 How it works... 42 There's more... 44 Image denoising with a denoising autoencoder 44 Getting ready 44 How to do it... 45 How it works... 48 There's more... 51 See also 51 Image denoising with PCA/DFT/DWT 51 Getting ready 51 How to do it... 52 How it works... 53 There's more... 55 See also 55 Image denoising with anisotropic diffusion 55 Getting ready 55 How to do it... 56 How it works... 58 There's more... 59 See also 59 Improving image contrast with histogram equalization 59 Getting ready 60 How to do it... 60 How it works... 61 There's more... 63 Implementing histogram matching 63 Getting ready 64 How to do it... 64 How it works... 65 There's more... 67 See also 67 Performing gradient blending 68 Getting ready 69 [ ii ] Table of Contents How to do it... 69 How it works... 70 Edge detection with Canny, LoG/zero-crossing, and wavelets 70 Getting ready 71 How to do it... 71 Canny/hysteresis thresholding 72 LoG/zero-crossing 73 Wavelets 75 How it works... 77 There's more... 79 See also 79 Chapter 3: Image Restoration 80 Restoring an image with the Wiener filter 81 Getting ready 81 How to do it... 81 How it works... 84 See also 85 Restoring an image with the constrained least squares filter 86 Getting ready 86 How to do it... 86 How it works... 90 There's more... 91 See also 91 Image restoration with a Markov random field 92 Getting ready 92 How to do it... 93 How it works... 95 See also 96 Image inpainting 96 Getting ready 97 How to do it... 97 How it works... 98 There's more... 99 Image inpainting with convex optimization 100 See also 101 Image completion with inpainting using deep learning 102 Getting ready 102 How to do it... 102 There's more... 105 See also 106 Image restoration with dictionary learning 106 Getting ready 107 How to do it ... 107 There's more... 110 Online dictionary learning 110 [ iii ]