Learning OpenCV 4 Computer Vision with Python 3

Book description

Updated for OpenCV 4 and Python 3, this book covers the latest on depth cameras, 3D tracking, augmented reality, and deep neural networks, helping you solve real-world computer vision problems with practical code

Key Features

  • Build powerful computer vision applications in concise code with OpenCV 4 and Python 3
  • Learn the fundamental concepts of image processing, object classification, and 2D and 3D tracking
  • Train, use, and understand machine learning models such as Support Vector Machines (SVMs) and neural networks

Book Description

Computer vision is a rapidly evolving science, encompassing diverse applications and techniques. This book will not only help those who are getting started with computer vision but also experts in the domain. You'll be able to put theory into practice by building apps with OpenCV 4 and Python 3.

You'll start by understanding OpenCV 4 and how to set it up with Python 3 on various platforms. Next, you'll learn how to perform basic operations such as reading, writing, manipulating, and displaying still images, videos, and camera feeds. From taking you through image processing, video analysis, and depth estimation and segmentation, to helping you gain practice by building a GUI app, this book ensures you'll have opportunities for hands-on activities. Next, you'll tackle two popular challenges: face detection and face recognition. You'll also learn about object classification and machine learning concepts, which will enable you to create and use object detectors and classifiers, and even track objects in movies or video camera feed. Later, you'll develop your skills in 3D tracking and augmented reality. Finally, you'll cover ANNs and DNNs, learning how to develop apps for recognizing handwritten digits and classifying a person's gender and age.

By the end of this book, you'll have the skills you need to execute real-world computer vision projects.

What you will learn

  • Install and familiarize yourself with OpenCV 4's Python 3 bindings
  • Understand image processing and video analysis basics
  • Use a depth camera to distinguish foreground and background regions
  • Detect and identify objects, and track their motion in videos
  • Train and use your own models to match images and classify objects
  • Detect and recognize faces, and classify their gender and age
  • Build an augmented reality application to track an image in 3D
  • Work with machine learning models, including SVMs, artificial neural networks (ANNs), and deep neural networks (DNNs)

Who this book is for

If you are interested in learning computer vision, machine learning, and OpenCV in the context of practical real-world applications, then this book is for you. This OpenCV book will also be useful for anyone getting started with computer vision as well as experts who want to stay up-to-date with OpenCV 4 and Python 3. Although no prior knowledge of image processing, computer vision or machine learning is required, familiarity with basic Python programming is a must.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learning OpenCV 4 Computer Vision with Python 3 Third Edition
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Code in Action
      3. Download the color images
      4. Conventions used
    4. Get in touch
      1. Reviews
  7. Setting Up OpenCV
    1. Technical requirements
    2. What's new in OpenCV 4
    3. Choosing and using the right setup tools
      1. Installation on Windows
        1. Using a ready-made OpenCV package
        2. Building OpenCV from source
      2. Installation on macOS
        1. Using Homebrew with ready-made packages
        2. Using Homebrew with your own custom packages
      3. Installation on Debian, Ubuntu, Linux Mint, and similar systems
        1. Using a ready-made OpenCV package
        2. Building OpenCV from source
      4. Installation on other Unix-like systems
    4. Running samples
    5. Finding documentation, help, and updates
    6. Summary
  8. Handling Files, Cameras, and GUIs
    1. Technical requirements
    2. Basic I/O scripts
      1. Reading/writing an image file
      2. Converting between an image and raw bytes
      3. Accessing image data with numpy.array
      4. Reading/writing a video file
      5. Capturing camera frames
      6. Displaying an image in a window
      7. Displaying camera frames in a window
    3. Project Cameo (face tracking and image manipulation)
    4. Cameo – an object-oriented design
      1. Abstracting a video stream with managers.CaptureManager
      2. Abstracting a window and keyboard with managers.WindowManager
      3. Applying everything with cameo.Cameo
    5. Summary
  9. Processing Images with OpenCV
    1. Technical requirements
    2. Converting images between different color models
      1. Light is not paint
    3. Exploring the Fourier transform
      1. HPFs and LPFs
    4. Creating modules
    5. Edge detection
    6. Custom kernels – getting convoluted
    7. Modifying the application
    8. Edge detection with Canny
    9. Contour detection
      1. Bounding box, minimum area rectangle, and minimum enclosing circle
      2. Convex contours and the Douglas-Peucker algorithm
    10. Detecting lines, circles, and other shapes
      1. Detecting lines
      2. Detecting circles
      3. Detecting other shapes
    11. Summary
  10. Depth Estimation and Segmentation
    1. Technical requirements
    2. Creating modules
    3. Capturing frames from a depth camera
    4. Converting 10-bit images to 8-bit
    5. Creating a mask from a disparity map
    6. Modifying the application
    7. Depth estimation with a normal camera
    8. Foreground detection with the GrabCut algorithm
    9. Image segmentation with the Watershed algorithm
    10. Summary
  11. Detecting and Recognizing Faces
    1. Technical requirements
    2. Conceptualizing Haar cascades
    3. Getting Haar cascade data
    4. Using OpenCV to perform face detection
      1. Performing face detection on a still image
      2. Performing face detection on a video
      3. Performing face recognition
        1. Generating the data for face recognition
        2. Recognizing faces
        3. Loading the training data for face recognition
        4. Performing face recognition with Eigenfaces
        5. Performing face recognition with Fisherfaces
        6. Performing face recognition with LBPH
        7. Discarding results based on the confidence score
    5. Swapping faces in the infrared
      1. Modifying the application's loop
      2. Masking a copy operation
    6. Summary
  12. Retrieving Images and Searching Using Image Descriptors
    1. Technical requirements
    2. Understanding types of feature detection and matching
      1. Defining features
    3. Detecting Harris corners
    4. Detecting DoG features and extracting SIFT descriptors
      1. Anatomy of a keypoint
    5. Detecting Fast Hessian features and extracting SURF descriptors
    6. Using ORB with FAST features and BRIEF descriptors
      1. FAST
      2. BRIEF
      3. Brute-force matching
      4. Matching a logo in two images
    7. Filtering matches using K-Nearest Neighbors and the ratio test
    8. Matching with FLANN
    9. Performing homography with FLANN-based matches
    10. A sample application – tattoo forensics
      1. Saving image descriptors to file
      2. Scanning for matches
    11. Summary
  13. Building Custom Object Detectors
    1. Technical requirements
    2. Understanding HOG descriptors
      1. Visualizing HOG
      2. Using HOG to describe regions of an image
    3. Understanding NMS
    4. Understanding SVMs
    5. Detecting people with HOG descriptors
    6. Creating and training an object detector
      1. Understanding BoW
      2. Applying BoW to computer vision
        1. k-means clustering
    7. Detecting cars
      1. Combining an SVM with a sliding window
        1. Detecting a car in a scene
        2. Saving and loading a trained SVM
    8. Summary
  14. Tracking Objects
    1. Technical requirements
    2. Detecting moving objects with background subtraction
      1. Implementing a basic background subtractor
      2. Using a MOG background subtractor
      3. Using a KNN background subtractor
      4. Using GMG and other background subtractors
    3. Tracking colorful objects using MeanShift and CamShift
      1. Planning our MeanShift sample
      2. Calculating and back-projecting color histograms
        1. Understanding the parameters of cv2.calcHist
        2. Understanding the parameters of cv2.calcBackProject
      3. Implementing the MeanShift example
      4. Using CamShift
    4. Finding trends in motion using the Kalman filter
      1. Understanding the predict and update phases
      2. Tracking a mouse cursor
    5. Tracking pedestrians
      1. Planning the flow of the application
      2. Comparing the object-oriented and functional paradigms
      3. Implementing the Pedestrian class
      4. Implementing the main function
      5. Considering the next steps
    6. Summary
  15. Camera Models and Augmented Reality
    1. Technical requirements
    2. Understanding 3D image tracking and augmented reality
      1. Understanding camera and lens parameters
      2. Understanding cv2.solvePnPRansac
    3. Implementing the demo application
      1. Importing modules
      2. Performing grayscale conversion
      3. Performing 2D-to-3D spatial conversions
      4. Implementing the application class
        1. Initializing the tracker
        2. Implementing the main loop
        3. Tracking the image in 3D
        4. Initializing and applying the Kalman filter
        5. Drawing the tracking results and masking the tracked object
      5. Running and testing the application
    4. Improving the 3D tracking algorithm
    5. Summary
  16. Introduction to Neural Networks with OpenCV
    1. Technical requirements
    2. Understanding ANNs
      1. Understanding neurons and perceptrons
      2. Understanding the layers of a neural network
        1. Choosing the size of the input layer
        2. Choosing the size of the output layer
        3. Choosing the size of the hidden layer
    3. Training a basic ANN in OpenCV
    4. Training an ANN classifier in multiple epochs
    5. Recognizing handwritten digits with an ANN
      1. Understanding the MNIST database of handwritten digits
      2. Choosing training parameters for the MNIST database
      3. Implementing a module to train the ANN
      4. Implementing a minimal test module
      5. Implementing the main module
      6. Trying to improve the ANN's training
      7. Finding other potential applications
    6. Using DNNs from other frameworks in OpenCV
    7. Detecting and classifying objects with third-party DNNs
    8. Detecting and classifying faces with third-party DNNs
    9. Summary
  17. Appendix A: Bending Color Space with the Curves Filter
    1. Formulating a curve
    2. Caching and applying a curve
    3. Designing object-oriented curve filters
    4. Emulating photo films
      1. Emulating Kodak Portra
      2. Emulating Fuji Provia
      3. Emulating Fuji Velvia
      4. Emulating cross-processing
    5. Summary
  18. Other Book You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learning OpenCV 4 Computer Vision with Python 3
  • Author(s): Joseph Howse, Joe Minichino
  • Release date: February 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781789531619