OpenCV 3 Computer Vision with Python Cookbook - Aleksei Spizhevoi - E-Book

OpenCV 3 Computer Vision with Python Cookbook E-Book

Aleksei Spizhevoi

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

OpenCV 3 is a native cross-platform library for computer vision, machine learning, and image processing. OpenCV's convenient high-level APIs hide very powerful internals designed for computational efficiency that can take advantage of multicore and GPU processing. This book will help you tackle increasingly challenging computer vision problems by providing a number of recipes that you can use to improve your applications.

In this book, you will learn how to process an image by manipulating pixels and analyze an image using histograms. Then, we'll show you how to apply image filters to enhance image content and exploit the image geometry in order to relay different views of a pictured scene. We’ll explore techniques to achieve camera calibration and perform a multiple-view analysis.

Later, you’ll work on reconstructing a 3D scene from images, converting low-level pixel information to high-level concepts for applications such as object detection and recognition. You’ll also discover how to process video from files or cameras and how to detect and track moving objects. Finally, you'll get acquainted with recent approaches in deep learning and neural networks.

By the end of the book, you’ll be able to apply your skills in OpenCV to create computer vision applications in various domains.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 249

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



OpenCV 3 Computer Vision with Python Cookbook
Leverage the power of OpenCV 3 and Python to build computer vision applications
Alexey Spizhevoy
Aleksandr Rybnikov
BIRMINGHAM - MUMBAI

OpenCV 3 Computer Vision with Python Cookbook

Copyright © 2018 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 authors, 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: Richa TripathiAcquisition Editor: Alok DhuriContent Development Editor: Rohit Kumar SinghTechnical Editor: Ketan KambleCopy Editor:Safis EditingProject Coordinator: Vaidehi SawantProofreader: Safis EditingIndexer: Tejal Daruwale SoniGraphics: Jason MonteiroProduction Coordinator: Aparna Bhagat

First published: March 2018

Production reference: 1200318

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78847-444-3

www.packtpub.com

mapt.io

Mapt is an online digital library that gives you full access to over 5,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

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

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.PacktPub.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.PacktPub.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.

Contributors

About the authors

Alexey Spizhevoy has over 7 years of experience in computer vision R&D. He has worked for 5 years at Itseez, the main OpenCV contributor, before it was acquired by Intel. He has contributed to video stabilization and photo stitching modules into OpenCV library. He has successfully participated in numerous Computer Vision projects in such areas as 3D reconstruction, video conferencing, object detection and tracking, semantic segmentation, driving assistance, and others. He holds a master's degree in computer science, and he is currently pursuing PhD.

Aleksandr Rybnikov has over 5 years of experience in C++ programming, including 3 years in the Computer Vision (CV) domain. He worked at Itseez, a company that supported and developed OpenCV, and then at Intel. He enriched OpenCV's dnn module by adding support of another two Deep Learning (DL) frameworks and many features, along with improving the existing functionality. As an engineer, he participated in CV and DL projects such as iris recognition, object detection, semantic segmentation, 6-DOF pose estimation, and digital hologram reconstruction. He has a master's degree in physics.

Without the great teachers I had, this book wouldn't have been possible. So I want to express gratitude to my colleagues who guided and advised me on my path in Computer Vision. Writing a book, especially when you want to make it useful and practical, isn't simple in any way and takes a lot of time. Many thanks and love to my wife, Daria Tikhonova, who has supported me while working on the book.

About the reviewer

Joseph Howse lives in a Canadian fishing village with four cats; the cats like fish, but they prefer chicken.

Joseph provides computer vision expertise through his company, Nummist Media. He is a Packt author whose books include OpenCV for Secret Agents, OpenCV 3 Blueprints, Android Application Programming with OpenCV 3, iOS Application Development with OpenCV 3, Learning OpenCV 3 Computer Vision with Python, and Python Game Programming by Example.

I wish to congratulate this book's authors, who have contributed so much code and wisdom to the OpenCV community. I am also thankful to the whole team at Packt for our continued collaboration. As always, I dedicate my work to Sam, Jan, Bob, Bunny, and the cats.

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

Title Page

Copyright and Credits

OpenCV 3 Computer Vision with Python Cookbook

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the authors

About the reviewer

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Sections

Getting ready

How to do it...

How it works...

There's more...

See also

Get in touch

Reviews

I/O and GUI

Introduction

Reading images from files

Getting ready

How to do it...

How it works...

Simple image transformations—resizing and flipping

Getting ready

How to do it...

How it works...

Saving images using lossy and lossless compression

Getting ready

How to do it...

How it works...

Showing images in an OpenCV window

Getting ready

How to do it...

How it works...

Working with UI elements, such as buttons and trackbars, in an OpenCV window

Getting ready

How to do it...

How it works...

Drawing 2D primitives—markers, lines, ellipses, rectangles, and text

Getting ready

How to do it...

How it works...

Handling user input from a keyboard

Getting ready

How to do it...

How it works...

Making your app interactive through handling user input from a mouse

Getting ready

How to do it...

How it works...

Capturing and showing frames from a camera

Getting ready

How to do it...

How it works...

Playing frame stream from video

Getting ready

How to do it...

How it works...

Obtaining a frame stream properties

Getting ready

How to do it...

How it works...

Writing a frame stream into video

Getting ready

How to do it...

How it works...

Jumping between frames in video files

Getting ready

How to do it...

How it works...

Matrices, Colors, and Filters

Introduction

Manipulating matrices-creating, filling, accessing elements, and ROIs

Getting ready

How to do it...

How it works...

Converting between different data types and scaling values

Getting ready

How to do it...

How it works...

Non-image data persistence using NumPy

Getting ready

How to do it...

How it works...

Manipulating image channels

Getting ready

How to do it...

How it works...

Converting images from one color space to another

Getting ready

How to do it...

How it works...

Gamma correction and per-element math

Getting ready

How to do it...

How it works...

Mean/variance image normalization

Getting ready

How to do it...

How it works...

Computing image histograms

Getting ready

How to do it...

How it works...

Equalizing image histograms

Getting ready

How to do it...

How it works...

Removing noise using Gaussian, median, and bilateral filters

Getting ready

How to do it...

How it works...

Computing gradients using Sobel operator

Getting ready

How to do it...

How it works...

Creating and applying your own filter

Getting ready

How to do it...

How it works...

Processing images with real-valued Gabor filters

Getting ready

How to do it...

How it works...

Going from the spatial domain to the frequency domain (and back) using the discrete Fourier transform

Getting ready

How to do it...

How it works...

Manipulating image frequencies for image filtration

Getting ready

How to do it...

How it works...

Processing images with different thresholds

Getting ready

How to do it...

How it works...

Morphological operators

Getting ready

How to do it...

How it works...

Image masks and binary operations

Getting ready

How to do it...

How it works...

Contours and Segmentation

Introduction

Binarization of grayscale images using the Otsu algorithm

Getting ready

How to do it...

How it works...

Finding external and internal contours in a binary image

Getting ready

How to do it...

How it works...

Extracting connected components from a binary image

Getting ready

How to do it...

How it works...

Fitting lines and circles into two-dimensional point sets

Getting ready

How to do it...

How it works...

Calculating image moments

Getting ready

How to do it...

How it works...

Working with curves - approximation, length, and area

Getting ready

How to do it...

How it works...

Checking whether a point is within a contour

Getting ready

How to do it...

How it works...

Computing distance maps

Getting ready

How to do it...

How it works...

Image segmentation using the k-means algorithm

Getting ready

How to do it...

How it works...

Image segmentation using segment seeds - the watershed algorithm

Getting ready

How to do it...

How it works...

Object Detection and Machine Learning

Introduction

Obtaining an object mask using the GrabCut algorithm

Getting ready

How to do it...

How it works...

Finding edges using the Canny algorithm

Getting ready

How to do it...

How it works...

Detecting lines and circles using the Hough transform

Getting ready

How to do it...

How it works...

Finding objects via template matching

Getting ready

How to do it...

How it works...

The medial flow tracker

Getting ready

How to do it...

How it works...

Tracking objects using different algorithms via the tracking API

Getting ready

How to do it...

How it works...

Computing the dense optical flow between two frames

Getting ready

How to do it...

How it works...

Detecting chessboard and circle grid patterns

Getting ready

How to do it...

How it works...

A simple pedestrian detector using the SVM model

Getting ready

How to do it...

How it works...

Optical character recognition using different machine learning models

Getting ready

How to do it...

How it works...

Detecting faces using Haar/LBP cascades

Getting ready

How to do it...

How it works...

Detecting AruCo patterns for AR applications

Getting ready

How to do it...

How it works...

Detecting text in natural scenes

Getting ready

How to do it...

How it works...

QR code detector

Getting ready

How to do it...

How it works...

Deep Learning

Introduction

Representing images as tensors/blobs

Getting ready

How to do it...

How it works...

Loading deep learning models from Caffe, Torch, and TensorFlow formats

Getting ready

How to do it...

How it works...

Getting input and output tensors' shapes for all layers

Getting ready

How to do it...

How it works...

Preprocessing images and inference in convolutional networks

Getting ready

How to do it...

How it works...

Measuring inference time and contributions to it from each layer

Getting ready

How to do it...

How it works...

Classifying images with GoogleNet/Inception and ResNet models

Getting ready

How to do it...

How it works...

Detecting objects with the Single Shot Detection (SSD) model

Getting ready

How to do it...

How it works...

Segmenting a scene using the Fully Convolutional Network (FCN) model

Getting ready

How to do it...

How it works...

Face detection using Single Shot Detection (SSD) and the ResNet model

Getting ready

How to do it...

How it works...

Age and gender prediction

Getting ready

How to do it...

How it works...

Linear Algebra

Introduction

The orthogonal Procrustes problem

Getting ready

How to do it...

How it works...

Rank-constrained matrix approximation

Getting ready

How to do it...

How it works...

Principal component analysis

Getting ready

How to do it...

How it works...

Solving systems of linear equations (including under- and over-determined)

Getting ready

How to do it...

How it works...

Solving polynomial equations

Getting ready

How to do it...

How it works...

Linear programming with the simplex method

Getting ready

How to do it...

How it works...

Detectors and Descriptors

Introduction

Finding corners in an image - Harris and FAST

Getting ready

How to do it...

How it works...

Selecting good corners in an image for tracking

Getting ready

How to do it...

How it works...

Drawing keypoints, descriptors, and matches

Getting ready

How to do it...

How it works...

Detecting scale invariant keypoints

Getting ready

How to do it...

How it works...

Computing descriptors for image keypoints - SURF, BRIEF, ORB

Getting ready

How to do it...

How it works...

Matching techniques for finding correspondences between descriptors

Getting ready

How to do it...

How it works...

Finding reliable matches - cross-check and ratio test

Getting ready

How to do it...

How it works...

Model-based filtering of matches - RANSAC

Getting ready

How to do it...

How it works...

BoW model for constructing global image descriptors

Getting ready

How to do it...

How it works...

Image and Video Processing

Introduction

Warping an image using affine and perspective transformations

Getting ready

How to do it

How it works

Remapping an image using arbitrary transformation

Getting ready

How to do it

How it works

Tracking keypoints between frames using the Lucas-Kanade algorithm

Getting ready

How to do it

How it works

Background subtraction

Getting ready

How to do it

How it works

Stitching many images into panorama

Getting ready

How to do it

How it works

Denoising a photo using non-local means algorithms

Getting ready

How to do it

How it works

Constructing an HDR image

Getting ready

How to do it

How it works

Removing defects from a photo with image inpainting

Getting ready

How to do it

How it works

Multiple View Geometry

Introduction

Pinhole camera model calibration

Getting ready

How to do it

How it works

Fisheye camera model calibration

Getting ready

How to do it

How it works

Stereo rig calibration - estimation of extrinsics

Getting ready

How to do it

How it works

Distorting and undistorting points

Getting ready

How to do it

How it works

Removing lens distortion effects from an image

Getting ready

How to do it

How it works

Restoring a 3D point from two observations through triangulation

Getting ready

How to do it

How it works

Finding a relative camera-object pose through the PnP algorithm

Getting ready

How to do it

How it works

Aligning two views through stereo rectification

Getting ready

How to do it

How it works

Epipolar geometry - computing fundamental and essential matrices

Getting ready

How to do it

How it works

Essential matrix decomposition into rotation and translation

Getting ready

How to do it

How it works

Estimating disparity maps for stereo images

Getting ready

How to do it

How it works

Special case 2-view geometry - estimating homography transformation

Getting ready

How to do it

How it works

Planar scene - decomposing homography into rotation and translation

Getting ready

How to do it

How it works

Rotational camera case - estimating camera rotation from homography

Getting ready

How to do it

How it works

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

Computer Vision is a broad topic comprising a lot of different areas. If you want to start using Computer Vision algorithms in your projects, it may be ambiguous where the entry point is. Even if you're an experienced Computer Vision engineer, undoubtedly there are some technologies that you would want to explore in depth or get familiar with. In both cases, a practical approach works best. Only through applying methods to real problems, tuning existing methods to meet your requirements, and playing with samples can you fully understand the possibilities and limitations of any Computer Vision algorithm. This book is specifically designed to get your hands dirty with solving real computer vision tasks. Recipes in this book use OpenCV—the most popular, functionally rich, and widely used open source Computer Vision library. This book progresses from the simplest samples to the most complicated ones, so you will be able to find some useful and information which is easy to understand.

Who this book is for

This book is for developers who have basic knowledge of Python. If you are aware of the basics of OpenCV and are ready to build computer vision systems that are smarter, faster, more complex, and more practical than the competition, then this book is for you.

What this book covers

Chapter 1, I/O and GUI, teaches the basic operations with images and video: loading, saving and displaying.

Chapter 2, Matrices, Colors, and Filters, covers operations to manipulate with matrices: accessing regions of an image, channels, and pixels. Conversions between various color spaces and usage of filters are also described.

Chapter 3, Contours and Segmentation, shows how to create image masks, find contours, and segment images.

Chapter 4, Object Detection and Machine Learning, describes ways of detecting and tracking different types of objects, from specially constructed (QR codes and ArUCo markers) to ones that can be met in natural scenes.

Chapter 5, Deep Learning, outlines new functionality in OpenCV connected with Deep Neural Nets. It provides examples of loading Deep Learning models and applying them to Computer Vision tasks.

Chapter 6, Linear Algebra, dives into useful mathematical methods for solving linear algebra problems and provides examples of applying these methods in Computer Vision.

Chapter 7, Detectors and Descriptors, contains information about how to work with image feature descriptors: how to compute them with different methods, how to display them, and how to match them for object detection and tracking purposes.

Chapter 8, Image and Video Processing, shows readers how to work with image sequences and get results based on correlations among the sequence.

Chapter 9, Multiple View Geometry, describes how to use cameras to retrieve information about 3D geometry of the scene.

To get the most out of this book

All the required information to get started with the respective recipes is mentioned in the recipes.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/OpenCV-3-Computer-Vision-with-Python-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/OpenCV3ComputerVisionwithPythonCookbook_ColorImages.pdf.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it...

This section contains the steps required to follow the recipe.

How it works...

This section usually consists of a detailed explanation of what happened in the previous section.

There's more...

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

I/O and GUI

In this chapter, we will cover the following recipes:

Reading images from file

Simple image transformations—resizing and flipping

Saving images using lossy and lossless compression

Showing images in an OpenCV window

Working with UI elements, such as buttons and trackbars, in an OpenCV window

Drawing 2D primitives—markers, lines, ellipses, rectangles, and text

Handling user input from a keyboard

Making your app interactive through handling user input from a mouse

Capturing and showing frames from a camera

Playing frame stream from video

Obtaining a frame stream properties

Writing a frame stream into video

Jumping between frames in video files

Introduction

Computer vision algorithms consume and produce data—they usually take images as an input and generate features of the input, such as contours, points or regions of interest, bounding boxes for objects, or another images. So dealing with the input and output of graphical information is an essential part of any computer vision algorithm. This means not only reading and saving images, but also displaying them with additional information about their features.

In this chapter, we will cover basic OpenCV functionality related to I/O functions. From the recipes, you will learn how to obtain images from different sources (either filesystem or camera), display them, and save images and videos. Also, the chapter covers the topic of working with the OpenCV UI system; for instance, in creating windows and trackbars.

Reading images from files

In this recipe, we will learn how to read images from files. OpenCV supports reading images in different formats, such as PNG, JPEG, and TIFF. Let's write a program that takes the path to an image as its first parameter, reads the image, and prints its shape and size.

Getting ready

You need to have OpenCV 3.x installed with Python API support.

How it works...

The loaded image is represented as a NumPy array. The same representation is used in OpenCV for matrices. NumPy arrays have such properties as shape, which is an image's size and number of color channels, and dtype, which is the underlying data type (for example, uint8 or float32). Note that OpenCV loads images in BGR, not RGB, format.

The shape tuple in this case should be interpreted as follows: image height, image width, color channels count.

The cv.imread function also supports optional flags, where users can specify whether conversion to uint8 type should be performed, and whether the image is grayscale or color.

Having run the code with the default parameters, you should see the following output:

read ../data/Lena.png

shape: (512, 512, 3)

dtype: uint8

read ../data/Lena.png as grayscale

shape: (512, 512)

dtype: uint8

Simple image transformations—resizing and flipping

Now we're able to load an image, it's time to do some simple image processing. The operations we're going to review—resize and flip—are basic and usually used as preliminary steps of complex computer vision algorithms.

Getting ready

You need to have OpenCV 3.x installed with Python API support.