34,79 €
Computer vision is found everywhere in modern technology. OpenCV for Python enables us to run computer vision algorithms in real time. With the advent of powerful machines, we have more processing power to work with. Using this technology, we can seamlessly integrate our computer vision applications into the cloud. Focusing on OpenCV 3.x and Python 3.6, this book will walk you through all the building blocks needed to build amazing computer vision applications with ease.
We start off by manipulating images using simple filtering and geometric transformations. We then discuss affine and projective transformations and see how we can use them to apply cool advanced manipulations to your photos like resizing them while keeping the content intact or smoothly removing undesired elements. We will then cover techniques of object tracking, body part recognition, and object recognition using advanced techniques of machine learning such as artificial neural network. 3D reconstruction and augmented reality techniques are also included. The book covers popular OpenCV libraries with the help of examples.
This book is a practical tutorial that covers various examples at different levels, teaching you about the different functions of OpenCV and their actual implementation. By the end of this book, you will have acquired the skills to use OpenCV and Python to develop real-world computer vision applications.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 217
Veröffentlichungsjahr: 2018
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: Aaron LazarAcquisition Editor: Chaitanya NairContent Development Editor: Rohit Kumar SinghTechnical Editor: Ketan KambleCopy Editor:Safis EditingProject Coordinator: Vaidehi SawantProofreader: Safis EditingIndexer: Rekha NairGraphics Coordinator: Jason MonteiroProduction Coordinator: Shraddha Falebhai
First published: September 2015 Second edition: January 2018
Production reference: 1150118
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78839-690-5
www.packtpub.com
Gabriel Garrido is a multifaceted and versatile software engineer with more than 7 years of experience in developing web applications for companies such as Telefonica, Trivago, and Base7Booking. He has a degree in computer science from the University of Granada, Spain.
He is passionate about coding, focusing on its quality and spending hours working on personal projects based on technologies such as computer vision, artificial intelligence, and augmented reality. Taking part in hackathons is one of his hobbies. He has won a couple of prizes for implementing beta software for a Google Cardboard hackathon and another for a travel assistant at a TNOOZ hackathon.
Prateek Joshi is an artificial intelligence researcher, an author of eight published books, and a TEDx speaker. He has been featured in Forbes 30 Under 30, CNBC, TechCrunch, Silicon Valley Business Journal, and many more publications. He is the founder of Pluto AI, a venture-funded Silicon Valley start-up building an intelligence platform for water facilities. He has been an invited speaker at technology and entrepreneurship conferences including TEDx, Global Big Data Conference, Machine Learning Developers Conference, Sensors Expo, and more. His tech blog has more than 1.6 million page views from over 200 countries, and he has more than 7,400 followers. He graduated from the University of Southern California with a master's degree specializing in Artificial Intelligence. He has previously worked at NVIDIA and Microsoft Research. You can learn more about him on his personal website.
Naren Yellavula, better known as Naren Arya in the developer community, is a Python and Go developer from Bangalore, India. He works as software engineer level two for Citrix R&D India, and as a full stack developer, he develops single-page applications and microservices. He loves the UNIX philosophy and blogs about programming. He has also been a speaker at various developer conferences, such as PyCon India 2015 and 2017. When he's not staring at his computer screen, he enjoys reading books.
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.
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.
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
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.
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
Get in touch
Reviews
Applying Geometric Transformations to Images
Installing OpenCV-Python
Windows
macOS X
Linux (for Ubuntu)
Virtual environments
Troubleshooting
OpenCV documentation
Reading, displaying, and saving images
What just happened?
Loading and saving an image
Changing image format
Image color spaces
Converting color spaces
What just happened?
Splitting image channels
Merging image channels
Image translation
What just happened?
Image rotation
What just happened?
Image scaling
What just happened?
Affine transformations
What just happened?
Projective transformations
What just happened?
Image warping
Summary
Detecting Edges and Applying Image Filters
2D convolution
Blurring
Size of the kernel versus blurriness
Motion blur
Under the hood
Sharpening
Understanding the pattern
Embossing
Edge detection
Erosion and dilation
Afterthought
Creating a vignette filter
What's happening underneath?
How do we move the focus around?
Enhancing the contrast in an image
How do we handle color images?
Summary
Cartoonizing an Image
Accessing the webcam
Under the hood
Extending capture options
Keyboard inputs
Interacting with the application
Mouse inputs
What's happening underneath?
Interacting with a live video stream
How did we do it?
Cartoonizing an image
Deconstructing the code
Summary
Detecting and Tracking Different Body Parts
Using Haar cascades to detect things
What are integral images?
Detecting and tracking faces
Understanding it better
Fun with faces
Under the hood
Removing the alpha channel from the overlay image
Detecting eyes
Afterthought
Fun with eyes
Positioning the sunglasses
Detecting ears
Detecting a mouth
It's time for a moustache
Detecting pupils
Deconstructing the code
Summary
Extracting Features from an Image
Why do we care about keypoints?
What are keypoints?
Detecting the corners
Good features to track
Scale-invariant feature transform (SIFT)
Speeded-up robust features (SURF)
Features from accelerated segment test (FAST)
Binary robust independent elementary features (BRIEF)
Oriented FAST and Rotated BRIEF (ORB)
Summary
Seam Carving
Why do we care about seam carving?
How does it work?
How do we define interesting?
How do we compute the seams?
Can we expand an image?
Can we remove an object completely?
How did we do it?
Summary
Detecting Shapes and Segmenting an Image
Contour analysis and shape matching
Approximating a contour
Identifying a pizza with a slice taken out
How to censor a shape?
What is image segmentation?
How does it work?
Watershed algorithm
Summary
Object Tracking
Frame differencing
Colorspace based tracking
Building an interactive object tracker
Feature-based tracking
Background subtraction
Summary
Object Recognition
Object detection versus object recognition
What is a dense feature detector?
What is a visual dictionary?
What is supervised and unsupervised learning?
What are support vector machines?
What if we cannot separate the data with simple straight lines?
How do we actually implement this?
What happened inside the code?
How did we build the trainer?
Summary
Augmented Reality
What is the premise of augmented reality?
What does an augmented reality system look like?
Geometric transformations for augmented reality
What is pose estimation?
How to track planar objects
What happened inside the code?
How to augment our reality
Mapping coordinates from 3D to 2D
How to overlay 3D objects on a video
Let's look at the code
Let's add some movements
Summary
Machine Learning by an Artificial Neural Network
Machine learning (ML) versus artificial neural network (ANN)
How does ANN work?
How to define multi-layer perceptrons (MLP)
How to implement an ANN-MLP classifier?
Evaluate a trained network
Classifying images
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
Computer vision is found everywhere in modern technology. OpenCV for Python enables us to run computer vision algorithms in real time. With the advent of powerful machines, we are getting more processing power to work with. Using this technology, we can seamlessly integrate our computer vision applications into the cloud. Web developers can develop complex applications without having to reinvent the wheel. This book is a practical tutorial that covers various examples at different levels, teaching you about the different functions of OpenCV and their actual implementations.
This book is intended for Python developers who are new to OpenCV and want to develop computer vision applications with OpenCV and Python. This book is also useful for generic software developers who want to deploy computer vision applications on the cloud. It would be helpful to have some familiarity with basic mathematical concepts such as vectors and matrices.
Chapter 1, Applying Geometric Transformations to Images, explains how to apply geometric transformations to images. In this chapter, we will discuss affine and projective transformations and see how we can use them to apply cool geometric effects to photos. The chapter will begin with the procedure of installing OpenCV-Python on multiple platforms, such as Mac OS X, Linux, and Windows. You will also learn how to manipulate an image in various ways, such as resizing and changing color spaces.
Chapter 2, Detecting Edges and Applying Image Filters, shows how to use fundamental image-processing operators and how we can use them to build bigger projects. We will discuss why we need edge detection and how it can be used in various different ways in computer vision applications. We will discuss image filtering and how we can use it to apply various visual effects to photos.
Chapter 3, Cartoonizing an Image, shows how to cartoonize a given image using image filters and other transformations. We will see how to use the webcam to capture a live video stream. We will discuss how to build a real-time application, where we extract information from each frame in the stream and display the result.
Chapter 4, Detecting and Tracking Different Body Parts, shows how to detect and track faces in a live video stream. We will discuss the face detection pipeline and see how we can use it to detect and track different parts of the face, such as eyes, ears, mouth, and nose.
Chapter 5, Extracting Features from an Image, is about detecting the salient points (called keypoints) in an image. We will discuss why these salient points are important and how we can use them to understand the image's content. We will talk about the different techniques that can be used to detect salient points and extract features from an image.
Chapter 6, Seam Carving, shows how to do content-aware image resizing. We will discuss how to detect interesting parts of an image and see how we can resize a given image without deteriorating those interesting parts.
Chapter 8, Detecting Shapes andSegmenting an Image, shows how to perform image segmentation. We will discuss how to partition a given image into its constituent parts in the best possible way. You will also learn how to separate the foreground from the background in an image.
Chapter 8, Object Tracking, shows you how to track different objects in a live video stream. At the end of this chapter, you will be able to track any object in a live video stream that is captured through the webcam.
Chapter 9, Object Recognition, shows how to build an object recognition system. We will discuss how to use this knowledge to build a visual search engine.
Chapter 10, Augmented Reality, shows how to build an augmented reality application. By the end of this chapter, you will be able to build a fun augmented reality project using the webcam.
Chapter 11, Machine Learning by Artificial Neural Network, shows how to build advanced image classifiers and object recognition using the latest OpenCV implementations. By the end of this chapter, you will be able to understand how neural networks work and how to apply them to machine learning to build advance images tools.
You'll need the following software:
OpenCV 3.1 or higher
NumPy 1.13 or higher
SciPy 1.0 or higher
scikit-learn 0.19 or higher
pickleshare 0.7 or higher
The hardware specifications requirement is any computer with at least 8 GB DDR3 RAM.
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 athttps://github.com/PacktPublishing/OpenCV-3-x-with-Python-By-Example. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/OpenCV3xwithPythonByExample_ColorImages.pdf.
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.
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.
In this chapter, we are going to learn how to apply cool geometric effects to images. Before we get started, we need to install OpenCV-Python. We will explain how to compile and install the necessary libraries to follow every example in this book.
By the end of this chapter, you will know:
How to install OpenCV-Python
How to read, display, and save images
How to convert to multiple color spaces
How to apply geometric transformations such as translation, rotation, and scaling
How to use affine and projective transformations to apply funny geometric effects to photos
In this section, we explain how to install OpenCV 3.X with Python 2.7 on multiple platforms. If you desire it, OpenCV 3.X also supports the use of Python 3.X and it will be fully compatible with the examples in this book. Linux is recommended as the examples in this book were tested on that OS.
In order to get OpenCV-Python up and running, we need to perform the following steps:
Install Python
: Make sure you have Python 2.7.x installed on your machine. If you don't have it, you can install it from:
https://www.python.org/downloads/windows/
.
Install NumPy
: NumPy is a great package to do numerical computing in Python. It is very powerful and has a wide variety of functions. OpenCV-Python plays nicely with NumPy, and we will be using this package a lot during the course of this book. You can install the latest version from:
http://sourceforge.net/projects/numpy/files/NumPy/
.
We need to install all these packages in their default locations. Once we install Python and NumPy, we need to ensure that they're working fine. Open up the Python shell and type the following:
>>> import numpy
If the installation has gone well, this shouldn't throw up any errors. Once you confirm it, you can go ahead and download the latest OpenCV version from: http://opencv.org/downloads.html.
Once you finish downloading it, double-click to install it. We need to make a couple of changes, as follows:
Navigate to
opencv/build/python/2.7/
.
You will see a file named
cv2.pyd
. Copy this file to
C:/Python27/lib/site-packages
.
You're all set! Let's make sure that OpenCV is working. Open up the Python shell and type the following:
>>> import cv2
If you don't see any errors, then you are good to go! You are now ready to use OpenCV-Python.
To install OpenCV-Python, we will be using Homebrew. Homebrew is a great package manager for macOS X and it will come in handy when you are installing various libraries and utilities on macOS X. If you don't have Homebrew, you can install it by running the following command in your terminal:
$ ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)"
Even though OS X comes with inbuilt Python, we need to install Python using Homebrew to make our lives easier. This version is called brewed Python. Once you install Homebrew, the next step is to install brewed Python. Open up the terminal, and type the following:
$ brew install python
This will automatically install it as well. Pip is a package management tool to install packages in Python, and we will be using it to install other packages. Let's make sure the brewed Python is working correctly. Go to your terminal and type the following:
$ which python
You should see /usr/local/bin/python printed on the terminal. This means that we are using the brewed Python, and not the inbuilt system Python. Now that we have installed brewed Python, we can go ahead and add the repository, homebrew/science, which is where OpenCV is located. Open the terminal and run the following command:
$ brew tap homebrew/science
Make sure the NumPy package is installed. If not, run the following in your terminal:
$ pip install numpy
Now, we are ready to install OpenCV. Go ahead and run the following command from your terminal:
$ brew install opencv --with-tbb --with-opengl
OpenCV is now installed on your machine, and you can find it at /usr/local/Cellar/opencv/3.1.0/. You can't use it just yet. We need to tell Python where to find our OpenCV packages. Let's go ahead and do that by symlinking the OpenCV files. Run the following commands from your terminal (please, double check that you are actually using the right versions, as they might be slightly different):
$ cd /Library/Python/2.7/site-packages/
$ ln -s /usr/local/Cellar/opencv/3.1.0/lib/python2.7/site-packages/cv.py
cv.py
$ ln -s /usr/local/Cellar/opencv/3.1.0/lib/python2.7/site-packages/cv2.so
cv2.so
You're all set! Let's see if it's installed properly. Open up the Python shell and type the following:
> import cv2
If the installation went well, you will not see any error messages. You are now ready to use OpenCV in Python.
If you want to use OpenCV within a virtual environment, you could follow the instructions in the Virtual environments section, applying small changes to each of the commands for macOS X.
First, we need to install the OS requirements:
[compiler]
$ sudo apt-get install build-essential
[required]
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config
libavcodec-dev libavformat-dev libswscale-dev git
libgstreamer0.10-dev libv4l-dev
[optional]
$ sudo apt-get install python-dev python-numpy libtbb2
libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev
libdc1394-22-dev
Once the OS requirements are installed, we need to download and compile the latest version of OpenCV along with several supported flags to let us implement the following code samples. Here we are going to install Version 3.3.0:
$ mkdir ~/opencv
$ git clone -b 3.3.0
https://github.com/opencv/opencv.git
opencv
$ cd opencv
$ git clone
https://github.com/opencv/opencv_contrib.git
opencv_contrib
$ mkdir release
$ cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules -D BUILD_PYTHON_SUPPORT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D BUILD_EXAMPLES=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D BUILD_EXAMPLES=ON ../
$ make -j4 ; echo 'Running in 4 jobs'
$ sudo make install
If you are using Python 3, place -D + flags together, as you see in the following command:
cmake -DCMAKE_BUILD_TYPE=RELEASE....
If you are using virtual environments to keep your test environment completely separate from the rest of your OS, you could install a tool called virtualenvwrapper by following this tutorial: https://virtualenvwrapper.readthedocs.io/en/latest/.
To get OpenCV running on this virtualenv, we need to install the NumPy package:
$(virtual_env) pip install numpy
Following all the previous steps, just add the following three flags on compilation by cmake (pay attention that flag CMAKE_INSTALL_PREFIX is being redefined):
$(<env_name>) > cmake ...
-D CMAKE_INSTALL_PREFIX=~/.virtualenvs/<env_name> \
-D PYTHON_EXECUTABLE=~/.virtualenvs/<env_name>/bin/python
-D PYTHON_PACKAGES_PATH=~/.virtualenvs/<env_name>/lib/python<version>/site-packages ...
Let's make sure that it's installed correctly. Open up the Python shell and type the following:
> import cv2
If you don't see any errors, you are good to go.
If the cv2 library was not found, identify where the library was compiled. It should be located at /usr/local/lib/python2.7/site-packages/cv2.so. If that is the case, make sure your Python version matches the one package that has been stored, otherwise just move it into the according site-packages folder of Python, including same for virtualenvs.
During cmake command execution, try to join -DMAKE... and the rest of the -D lines. Moreover, if execution fails during the compiling process, some libraries might be missing from the OS initial requirements. Make sure you installed them all.
You can find an official tutorial about how to install the latest version of OpenCV on Linux at the following website: http://docs.opencv.org/trunk/d7/d9f/tutorial_linux_install.html.
If you are trying to compile using Python 3, and cv2.so is not installed, make sure you installed OS dependency Python 3 and NumPy.
