The

Computer Vision

Workshop

The Computer Vision Workshop

Copyright © 2020 Packt Publishing

All rights reserved. No part of this course 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 course to ensure the accuracy of the information presented. However, the information contained in this course is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this course.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this course by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Authors: Hafsa Asad, Vishwesh Ravi Shrimali, and Nikhil Singh

Reviewers: Richmond Alake, Sayantani Basu, John Wesley Doyle, Tim Hoolihan, Harshil Jain, Amar Kumar, Shiva Prasad, Geetank Raipura, Shovon Sengupta, and Sanchit Singh

Managing Editors: Anush Kumar Mehalavarunan, Bhavesh Bangera, and Rutuja Yerunkar

Acquisitions Editors: Manuraj Nair, Royluis Rodrigues, Kunal Sawant, Anindya Sil, and Archie Vankar

Production Editor: Shantanu Zagade

Editorial Board: Megan Carlisle, Samuel Christa, Mahesh Dhyani, Heather Gopsill, Manasa Kumar, Alex Mazonowicz, Monesh Mirpuri, Bridget Neale, Dominic Pereira, Shiny Poojary, Abhishek Rane, Brendan Rodrigues, Erol Staveley, Ankita Thakur, Nitesh Thakur, and Jonathan Wray

First published: July 2020

Production reference: 1240720

ISBN: 978-1-80020-177-4

Published by Packt Publishing Ltd.

Livery Place, 35 Livery Street

Birmingham B3 2PB, UK

Table of Contents

Preface   i

1. Basics of Image Processing   1

Introduction   2

NumPy Arrays   4

Exercise 1.01: Creating NumPy Arrays   5

Pixels in Images   11

Pixel Location – Image Coordinate System   12

Image Properties   14

Size of the Image 14

Color Spaces and Channels 16

Pixel Values   19

Introduction to OpenCV   25

Images in OpenCV   25

Important OpenCV Functions   27

Exercise 1.02: Reading, Processing, and Writing an Image   30

Using Matplotlib to Display Images   35

Accessing and Manipulating Pixels   38

Exercise 1.03: Creating a Water Effect   39

Activity 1.01: Mirror Effect with a Twist   46

Summary   50

2. Common Operations When Working with Images   53

Introduction   54

Geometric Transformations   55

Image Translation   56

Exercise 2.01: Translation Using NumPy   58

Image Rotation   61

Finding the Rotation Matrix 62

Finding the Size of the Output Image 64

Image Resizing   67

Affine Transformation   73

Exercise 2.02: Working with Affine Transformation   75

Perspective Transformation   79

Exercise 2.03: Perspective Transformation   80

Image Arithmetic   84

Image Addition   84

Exercise 2.04: Performing Image Addition   85

Image Multiplication   92

Exercise 2.05: Image Multiplication   92

Binary Images   96

Exercise 2.06: Converting an Image into a Binary Image   98

Bitwise Operations on Images   102

Exercise 2.07: Chess Pieces   103

Masking   109

Activity 2.01: Masking Using Binary Images   111

Summary   114

3. Working with Histograms   117

Introduction   118

Introduction to Matplotlib   119

Displaying Images with Matplotlib   120

Plotting Histograms with Matplotlib   121

Exercise 3.01: Plotting a Sample Image and Its Histogram with 256 bins   122

Exercise 3.02: Plotting a Sample Image and Its Histogram with 10 bins   125

Histograms with OpenCV   131

User-Selected ROI   136

Exercise 3.03: Creating a Mask Image Using a User-Selected ROI   137

A Comparison of Some Sample Histograms   141

What Is Histogram Equalization?   142

Exercise 3.04: Histogram Equalization of a Grayscale Image   142

Contrast Limited Adaptive Histogram Equalization (CLAHE)   148

Exercise 3.05: Application of CLAHE on a Grayscale Image   152

Activity 3.01: Enhancing Images Using Histogram Equalization and CLAHE   159

Exercise 3.06: Histogram Equalization in the BGR Color Space   161

The Histogram Equalization of Color Images Using the HSV and LAB Color Spaces   165

Exercise 3.07: Histogram Equalization in the HSV Color Space   166

Exercise 3.08: Histogram Equalization in the LAB Color Space   170

Activity 3.02: Image Enhancement in a User-Defined ROI   176

Summary   178

4. Working with contours   181

Introduction   182

Contours – Basic Detection and Plotting   182

Exercise 4.01: Detecting Shapes and Displaying Them on BGR Images   188

Exercise 4.02: Detecting Shapes and Displaying Them on Black and White Images   195

Exercise 4.03: Displaying Different Contours with Different Colors and Thicknesses   197

Drawing a Bounding Box around a Contour   200

Area of a Contour   201

Difference between Contour Detection and Edge Detection   202

Hierarchy   204

Exercise 4.04: Detecting a Bolt and a Nut   210

Exercise 4.05: Detecting a Basketball Net in an Image   216

Contour Matching   224

Exercise 4.06: Detecting Fruits in an Image   228

Exercise 4.07: Identifying Bananas from the Image of Fruits   232

Exercise 4.08: Detecting an Upright Banana from the Image of Fruits   239

Activity 4.01: Identifying a Character on a Mirrored Document   240

Summary   243

5. Face Processing in Image and Video   245

Introduction   246

Introduction to Haar Cascades   247

Using Haar Cascades for Face Detection   250

Exercise 5.01: Face Detection Using Haar Cascades   256

Detecting Parts of the Face   262

Exercise 5.02: Eye Detection Using Cascades   264

Clubbing Cascades for Multiple Object Detection   270

Activity 5.01: Eye Detection Using Multiple Cascades   271

Activity 5.02: Smile Detection Using Haar Cascades   274

GrabCut Technique   276

Exercise 5.03: Human Body Segmentation Using GrabCut with Rectangular Mask   281

Exercise 5.04: Human Body Segmentation Using Mask and ROI   287

Activity 5.03: Skin Segmentation Using GrabCut   298

Activity 5.04: Emoji Filter   301

Summary   305

6. Object Tracking   307

Introduction   308

Naïve Tracker   309

Exercise 6.01: Object Tracking Using Basic Image Processing   311

Non-Deep Learning-Based Object Trackers   317

Kalman Filter – Predict and Update   317

Meanshift – Density Seeking Filter   319

CAMshift – Continuously Adaptive Meanshift   322

The OpenCV Object Tracking API   325

Object Tracker Summary   328

Exercise 6.02: Object Tracking Using the Median Flow and MIL Trackers   330

Installing Dlib   338

Object Tracking Using Dlib   339

Exercise 6.03: Object Tracking Using Dlib   342

Activity 6.01: Implementing Autofocus Using Object Tracking   346

Summary   350

7. Object Detection and Face Recognition   353

Introduction   354

Face Recognition   355

Face Recognition Using Eigenfaces   357

Principal Component Analysis   357

Eigenfaces   358

Exercise 7.01: Facial Recognition Using Eigenfaces   362

Limitations of the Eigenface Method   370

Fisherface   371

Exercise 7.02: Facial Recognition Using the Fisherface Method   376

Local Binary Patterns Histograms   383

Exercise 7.03: Facial Recognition Using the LBPH Method   388

Object Detection   395

Single Shot Detector   395

MobileNet   399

Exercise 7.04: Object Detection Using MobileNet SSD   400

Object Detection Using the LBPH Method   405

Exercise 7.05: Object Detection Using the LBPH Method   406

Haar Cascades   413

Exercise 7.06: Object Detection Using Haar-Based Features   414

Activity 7.01: Object Detection in a Video Using MobileNet SSD   418

Activity 7.02: Real-Time Facial Recognition Using LBPH   421

Summary   423

8. OpenVINO with OpenCV   425

Introduction   426

Exploring the OpenVINO Toolkit   427

Components of the OpenVINO Toolkit   428

Installing OpenVINO for Ubuntu   430

OpenVINO as a Backend in OpenCV   434

The Need for Pre-Trained Models   436

OpenVINO Model Zoo   437

Exercise 8.01: Downloading the Pedestrian and Vehicle Detection Model   438

Model Specifications   442

Image Transforms Using OpenCV   445

Exercise 8.02: Image Preprocessing Using OpenCV   446

Model Conversion Using Model Optimizer   449

Introduction to OpenVINO's Inference Engine   452

Exercise 8.03: Vehicle and Pedestrian Detection   453

Activity 8.01: Face Detection Using OpenVINO and OpenCV   459

Summary   463

Appendix   465

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset