Mastering OpenCV 4 with Python

Book description

Create advanced applications with Python and OpenCV, exploring the potential of facial recognition, machine learning, deep learning, web computing and augmented reality.

Key Features

  • Develop your computer vision skills by mastering algorithms in Open Source Computer Vision 4 (OpenCV 4)and Python
  • Apply machine learning and deep learning techniques with TensorFlow, Keras, and PyTorch
  • Discover the modern design patterns you should avoid when developing efficient computer vision applications

Book Description

OpenCV is considered to be one of the best open source computer vision and machine learning software libraries. It helps developers build complete projects in relation to image processing, motion detection, or image segmentation, among many others. OpenCV for Python enables you to run computer vision algorithms smoothly in real time, combining the best of the OpenCV C++ API and the Python language.

In this book, you'll get started by setting up OpenCV and delving into the key concepts of computer vision. You'll then proceed to study more advanced concepts and discover the full potential of OpenCV. The book will also introduce you to the creation of advanced applications using Python and OpenCV, enabling you to develop applications that include facial recognition, target tracking, or augmented reality. Next, you'll learn machine learning techniques and concepts, understand how to apply them in real-world examples, and also explore their benefits, including real-time data production and faster data processing. You'll also discover how to translate the functionality provided by OpenCV into optimized application code projects using Python bindings. Toward the concluding chapters, you'll explore the application of artificial intelligence and deep learning techniques using the popular Python libraries TensorFlow, and Keras.

By the end of this book, you'll be able to develop advanced computer vision applications to meet your customers' demands.

What you will learn

  • Handle files and images, and explore various image processing techniques
  • Explore image transformations, including translation, resizing, and cropping
  • Gain insights into building histograms
  • Brush up on contour detection, filtering, and drawing
  • Work with Augmented Reality to build marker-based and markerless applications
  • Work with the main machine learning algorithms in OpenCV
  • Explore the deep learning Python libraries and OpenCV deep learning capabilities
  • Create computer vision and deep learning web applications

Who this book is for

This book is designed for computer vision developers, engineers, and researchers who want to develop modern computer vision applications. Basic experience of OpenCV and Python programming is a must.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering OpenCV 4 with Python
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. 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. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Section 1: Introduction to OpenCV 4 and Python
  7. Setting Up OpenCV
    1. Technical requirements
      1. Code testing specifications
      2. Hardware specifications
    2. Understanding Python
      1. Introducing OpenCV
      2. Contextualizing the reader
    3. A theoretical introduction to the OpenCV library
      1. OpenCV modules
      2. OpenCV users
      3. OpenCV applications
      4. Why citing OpenCV in your research work
    4. Installing OpenCV, Python, and other packages
      1. Installing Python, OpenCV, and other packages globally
      2. Installing Python
        1. Installing Python on Linux
        2. Installing Python on Windows
        3. Installing OpenCV
        4. Installing OpenCV on Linux
        5. Installing OpenCV on Windows
      3. Testing the installation
    5. Installing Python, OpenCV, and other packages with virtualenv
    6. Python IDEs to create virtual environments with virtualenv
    7. Anaconda/Miniconda distributions and conda package–and environment-management system
    8. Packages for scientific computing, data science, machine learning, deep learning, and computer vision
    9. Jupyter Notebook
      1. Trying Jupiter Notebook online
      2. Installing the Jupyter Notebook
      3. Installing Jupyter using Anaconda
      4. Installing Jupyter with pip
    10. The OpenCV and Python project structure
    11. Our first Python and OpenCV project
    12. Summary
    13. Questions
    14. Further reading
  8. Image Basics in OpenCV
    1. Technical requirements
    2. A theoretical introduction to image basics
      1. Main problems in image processing
      2. Image-processing steps
      3. Images formulation
    3. Concepts of pixels, colors, channels, images, and color spaces
      1. File extensions
    4. The coordinate system in OpenCV
    5. Accessing and manipulating pixels in OpenCV
      1. Accessing and manipulating pixels in OpenCV with BGR images
      2. Accessing and manipulating pixels in OpenCV with grayscale images
    6. BGR order in OpenCV
    7. Summary
    8. Questions
    9. Further reading
  9. Handling Files and Images
    1. Technical requirements
    2. An introduction to handling files and images
      1. sys.argv
      2. Argparse – command-line option and argument parsing
    3. Reading and writing images
      1. Reading images in OpenCV
      2. Reading and writing images in OpenCV
    4. Reading camera frames and video files
      1. Reading camera frames
      2. Accessing some properties of the capture object
      3. Saving camera frames
      4. Reading a video file
      5. Reading from an IP camera
    5. Writing a video file
      1. Calculating frames per second
      2. Considerations for writing a video file
    6. Playing with video capture properties
      1. Getting all the properties from the video capture object
      2. Using the properties – playing a video backwards
    7. Summary
    8. Questions
    9. Further reading
  10. Constructing Basic Shapes in OpenCV
    1. Technical requirements
    2. A theoretical introduction to drawing in OpenCV
    3. Drawing shapes
      1. Basic shapes – lines, rectangles, and circles
        1. Drawing lines
        2. Drawing rectangles
        3. Drawing circles
      2. Understanding advanced shapes
        1. Drawing a clip line
        2. Drawing arrows
        3. Drawing ellipses
        4. Drawing polygons
      3. Shift parameter in drawing functions
      4. lineType parameter in drawing functions
    4. Writing text
      1. Drawing text
      2. Using all OpenCV text fonts
      3. More functions related to text
    5. Dynamic drawing with mouse events
      1. Drawing dynamic shapes
      2. Drawing both text and shapes
      3. Event handling with Matplotlib
    6. Advanced drawing
    7. Summary
    8. Questions
    9. Further reading
  11. Section 2: Image Processing in OpenCV
  12. Image Processing Techniques
    1. Technical requirements
    2. Splitting and merging channels in OpenCV
    3. Geometric transformations of images
      1. Scaling an image
      2. Translating an image
      3. Rotating an image
      4. Affine transformation of an image
      5. Perspective transformation of an image
      6. Cropping an image
    4. Image filtering
      1. Applying arbitrary kernels
      2. Smoothing images
        1. Averaging filter
        2. Gaussian filtering
        3. Median filtering
        4. Bilateral filtering
      3. Sharpening images
      4. Common kernels in image processing
      5. Creating cartoonized images
    5. Arithmetic with images
      1. Saturation arithmetic
      2. Image addition and subtraction
      3. Image blending
      4. Bitwise operations
    6. Morphological transformations
      1. Dilation operation
      2. Erosion operation
      3. Opening operation
      4. Closing operation
      5. Morphological gradient operation
      6. Top hat operation
      7. Black hat operation
      8. Structuring element
      9. Applying morphological transformations to images
    7. Color spaces
      1. Showing color spaces
      2. Skin segmentation in different color spaces
    8. Color maps
      1. Color maps in OpenCV
      2. Custom color maps
      3. Showing the legend for the custom color map
    9. Summary
    10. Questions
    11. Further reading
  13. Constructing and Building Histograms
    1. Technical requirements
    2. A theoretical introduction to histograms
      1. Histogram terminology
    3. Grayscale histograms
      1. Grayscale histograms without a mask
      2. Grayscale histograms with a mask
    4. Color histograms
    5. Custom visualizations of histograms
    6. Comparing OpenCV, NumPy, and Matplotlib histograms
    7. Histogram equalization
      1. Grayscale histogram equalization
      2. Color histogram equalization
    8. Contrast Limited Adaptive Histogram Equalization
    9. Comparing CLAHE and histogram equalization
    10. Histogram comparison
    11. Summary
    12. Questions
    13. Further reading
  14. Thresholding Techniques
    1. Technical requirements
      1. Installing scikit-image
      2. Installing SciPy
    2. Introducing thresholding techniques
    3. Simple thresholding
      1. Thresholding types
      2. Simple thresholding applied to a real image
    4. Adaptive thresholding
    5. Otsu's thresholding algorithm
    6. The triangle binarization algorithm
    7. Thresholding color images
    8. Thresholding algorithms using scikit-image
      1. Introducing thresholding with scikit-image
      2. Trying out more thresholding techniques with scikit-image
    9. Summary
    10. Questions
    11. Further reading
  15. Contour Detection, Filtering, and Drawing
    1. Technical requirements
    2. An introduction to contours
    3. Compressing contours
    4. Image moments
      1. Some object features based on moments
      2. Hu moment invariants
      3. Zernike moments
    5. More functionality related to contours
    6. Filtering contours
    7. Recognizing contours
    8. Matching contours
    9. Summary
    10. Questions
    11. Further reading
  16. Augmented Reality
    1. Technical requirements
    2. An introduction to augmented reality
    3. Markerless-based augmented reality
      1. Feature detection
      2. Feature matching
      3. Feature matching and homography computation to find objects
    4. Marker-based augmented reality
      1. Creating markers and dictionaries
      2. Detecting markers
      3. Camera calibration
      4. Camera pose estimation
      5. Camera pose estimation and basic augmentation
      6. Camera pose estimation and more advanced augmentation
    5. Snapchat-based augmented reality
      1. Snapchat-based augmented reality OpenCV moustache overlay
      2. Snapchat-based augmented reality OpenCV glasses overlay
    6. QR code detection
    7. Summary
    8. Questions
    9. Further reading
  17. Section 3: Machine Learning and Deep Learning in OpenCV
  18. Machine Learning with OpenCV
    1. Technical requirements
    2. An introduction to machine learning
      1. Supervised machine learning
      2. Unsupervised machine learning
      3. Semi-supervised machine learning
    3. k-means clustering
      1. Understanding k-means clustering
      2. Color quantization using k-means clustering
    4. k-nearest neighbor
      1. Understanding k-nearest neighbors
      2. Recognizing handwritten digits using k-nearest neighbor
    5. Support vector machine
      1. Understanding SVM
      2. Handwritten digit recognition using SVM
    6. Summary
    7. Questions
    8. Further reading
  19. Face Detection, Tracking, and Recognition
    1. Technical requirements
      1. Installing dlib
      2. Installing the face_recognition package
      3. Installing the cvlib package
    2. Face processing introduction
    3. Face detection
      1. Face detection with OpenCV
      2. Face detection with dlib
      3. Face detection with face_recognition
      4. Face detection with cvlib
    4. Detecting facial landmarks
      1. Detecting facial landmarks with OpenCV
      2. Detecting facial landmarks with dlib
      3. Detecting facial landmarks with face_recognition
    5. Face tracking
      1. Face tracking with the dlib DCF-based tracker
      2. Object tracking with the dlib DCF-based tracker
    6. Face recognition
      1. Face recognition with OpenCV
      2. Face recognition with dlib
      3. Face recognition with face_recognition
    7. Summary
    8. Questions
    9. Further reading
  20. Introduction to Deep Learning
    1. Technical requirements
      1. Installing TensorFlow
      2. Installing Keras
    2. Deep learning overview for computer vision tasks
      1. Deep learning characteristics
      2. Deep learning explosion
      3. Deep learning for image classification
      4. Deep learning for object detection
    3. Deep learning in OpenCV
      1. Understanding cv2.dnn.blobFromImage()
      2. Complete examples using the OpenCV DNN face detector
      3. OpenCV deep learning classification
        1. AlexNet for image classification
        2. GoogLeNet for image classification
        3. ResNet for image classification
        4. SqueezeNet for image classification
      4. OpenCV deep learning object detection
        1. MobileNet-SSD for object detection
        2. YOLO for object detection
    4. The TensorFlow library
      1. Introduction example to TensorFlow
      2. Linear regression in TensorFlow
      3. Handwritten digits recognition using TensorFlow
    5. The Keras library
      1. Linear regression in Keras
      2. Handwritten digit recognition in Keras
    6. Summary
    7. Questions
    8. Further reading
  21. Section 4: Mobile and Web Computer Vision
  22. Mobile and Web Computer Vision with Python and OpenCV
    1. Technical requirements
      1. Installing the packages
    2. Introduction to Python web frameworks
    3. Introduction to Flask
    4. Web computer vision applications using OpenCV and Flask
      1. A minimal example to introduce OpenCV and Flask
      2. Minimal face API using OpenCV
      3. Deep learning cat detection API using OpenCV
    5. Deep learning API using Keras and Flask
      1. Keras applications
      2. Deep learning REST API using Keras Applications
    6. Deploying a Flask application to the cloud
    7. Summary
    8. Questions
    9. Further reading
  23. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
  24. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering OpenCV 4 with Python
  • Author(s): Alberto Fernandez Villan
  • Release date: March 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789344912