Raspberry Pi ImageProcessingProgrammingWith NumPy, SciPy, Matplotlib,and OpenCVSecond EditionAshwin PajankarRaspberry Pi Image Processing Programming Ashwin Pajankar Nashik, Maharashtra, India ISBN-13 (pbk): 978-1-4842-8269-4 ISBN-13 (electronic): 978-1-4842-8270-0 https://doi.org/10.1007/978-1-4842-8270-0 Copyright 2022 by Ashwin Pajankar This work is subject to copyright. All rights are reserved by the publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made.
The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Celestin Suresh John Development Editor: James Markham Coordinating Editor: Aditee Mirashi Copyeditor: April Rondeau Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, email orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science+Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (github.com/apress) For more detailed information, please visit http://www.apress.com/source- code. Printed on acid-free paper This book is dedicated to the memory ofProf. Govindarajulu Regeti(July 9, 1945 to March 18, 2021)Popularly known to everyone as RGR, Prof.
Govindarajuluobtained a Bachelor of Technology degree in electricaland electronic engineering from JNTU Kakinada. He alsoearned a masters degree and Ph.D. from IIT Kanpur.Prof. Govindarajulu was an early faculty member of IIITHyderabad and played a significant role in making IIITHyderabad the top-class institution that it is today. He wasby far the most loved and cheered for faculty member ofthe institute. He was full of energy to teach and full of old-fashioned charm.
There is no doubt he cared for every studentas an individual, taking care to know about and to guidethem. He has taught, guided, and mentored many batches ofstudents at IIIT Hyderabad (including the author of this book).Table of Contents About the Author xi About the Technical Reviewer xiii Acknowledgments xv Introduction xvii Chapter 1: Introduction to Single-Board Computers and Raspberry Pi 1 Single-Board Computers (SBCs) 1 Differences Between SBCs and Regular Computers 3 System on Chips (SoCs) 3 SoC versus Regular CPU 4 History of SBCs 4 Popular SBC Families 5 The Raspberry Pi 5 Raspberry Pi Setup 10 Prepare the microSD Card 12 Booting Up Raspberry Pi 20 Accessing Raspberry Pi Remotely 20 Configuring Raspberry Pi 25 The Raspberry Pi OS 27 The configtxt File 27 Updating the RPi 28 Summary30 v Table of ConTenTs Chapter 2: Introduction to Python and Digital Image Processing 31 A Brief History of the Python Programming Language 32 Features of Python 34 Python 3 37 Python 2 and Python 3 on Raspberry Pi OS 38 Running a Python Program and Python Modes 38 IDEs for Python 40 IDLE 40 Geany 43 Thonny IDE 45 Introduction to Digital Image Processing 46 Signal Processing 46 Image Processing 47 Using Raspberry Pi and Python for Digital Image Processing (DIP) 49 Exercise 49 Summary50 Chapter 3: Getting Started 51 Image Sources 51 Using a Webcam 54 guvcview 55 fswebcam 56 The Pi Camera Module 57 raspistill 61 Python 3 for Digital Image Processing 61 Working with Images 62 Summary67 vi Table of ConTenTs Chapter 4: Basic Operations on Images 69 Image Module 69 Image Channels 70 Colorspace Conversion 71 Image Blending 72 Resizing an Image 75 Rotating an Image 76 Crop and Paste Operations 77 Copying and Saving Images to a File 79 Knowing the Value of a Particular Pixel 79 Mandelbrot Set 79 Julia Set 81 Noise and Gradients 84 ImageChops Module 85 ImageOps 89 Summary90 Chapter 5: Advanced Operations on Images 91 ImageFilter Module 91 The ImageEnhance Module 105 Color Quantization 108 Histograms and Equalization 110 Histogram Equalization 111 Summary112 vii Table of ConTenTs Chapter 6: Introduction to the Scientific Python Ecosystem 113 The Scientific Python Ecosystem 114 Simple Examples 115 Matplotlib 118 Image Channels 123 Conversion Between PIL Image Objects and NumPy ndarrays 126 Summary127 Chapter 7: Transformations and Measurements 129 Transformations 129 Measurements 139 Summary143 Chapter 8: Filters 145 Kernels, Convolution, and Correlation 146 Low-Pass Filters 150 Blurring 150 Noise Reduction 156 High-Pass Filters 161 Fourier Filters 165 Summary167 Chapter 9: Morphology, Thresholding, and Segmentation 169 Distance Transforms 169 Morphology and Morphological Operations 171 Structuring Element172 Binary Morphological Operations 172 Grayscale Morphological Operations 178 Thresholding and Segmentation 182 Summary188 viii Table of ConTenTs Chapter 10: Video Processing 189 Introduction to OpenCV Library 189 Converting the Colorspace of Image 194 Separating Color Channels in Live Webcam Stream 196 More Operations on Live Webcam Stream 198 Geometric Transformation 198 Convolution 200 Correlation 204 Filtering 206 Morphological Operations216 Summary220 Conclusion 220 Appendix 223 pgmagick Image Processing 223 Connecting a Display 225 Using a VGA Display225 Booting Up After Connecting a Display 226 Connecting to Ethernet/ Wired Network 234 Remote Desktop with VNC 235 Index 241 ix About the AuthorAshwin Pajankar earned a master of technology in computer science engineering from IIIT Hyderabad and has over 25 years of experience in the area of programming. He started his journey in programming and electronics at the tender age of seven with an MS-DOS computer and BASIC programming language. He is now proficient in Assembly programming, C, C++, Java, shell scripting, JavaScript, Go Programming, HTML, and Python. His other technical expertise includes single-board computers such as Raspberry Pi and Banana Pro, microcontroller boards such as Arduino, and embedded boards such as BBC Micro Bit. He has extensively worked on domains such as software/product testing, software automation, databases, data analytics and visualization, computer vision, and web development.
Next page