Learning OpenCV 3 Computer Vision With Python - Second Edition - Sample Chapter
Learning OpenCV 3 Computer Vision With Python - Second Edition - Sample Chapter
Second Edition
Learning OpenCV 3 Computer Vision with Python,
Second Edition, takes you through building a
theoretical foundation for image processing and video
analysis and progress to the concepts of classification
through machine learning, acquiring the technical
know-how that will allow you to create and use object
detectors and classifiers, and even track objects in
movies or video camera feeds.
Finally, this journey ends at the world of artificial neural
networks, along with the development of a hand-written
digit recognition application.
P U B L I S H I N G
Joe Minichino
Joseph Howse
$ 44.99 US
28.99 UK
Second Edition
ee
Sa
m
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Joe Minichino
Joseph Howse
Joseph Howse lives in Canada. During the winters, he grows his beard, while
his four cats grow their thick coats of fur. He loves combing his cats every day and
sometimes, his cats also pull his beard.
He has been writing for Packt Publishing since 2012. His books include OpenCV for
Secret Agents, OpenCV Blueprints, Android Application Programming with OpenCV 3,
OpenCV Computer Vision with Python, and Python Game Programming by Example.
When he is not writing books or grooming his cats, he provides consulting,
training, and software development services through his company,
Nummist Media (http://nummist.com).
Preface
OpenCV 3 is a state-of-the-art computer vision library that is used for a variety
of image and video processing operations. Some of the more spectacular and
futuristic features, such as face recognition or object tracking, are easily achievable
with OpenCV 3. Learning the basic concepts behind computer vision algorithms,
models, and OpenCV's API will enable the development of all sorts of real-world
applications, including security and surveillance tools.
Starting with basic image processing operations, this book will take you through
a journey that explores advanced computer vision concepts. Computer vision is a
rapidly evolving science whose applications in the real world are exploding, so this
book will appeal to computer vision novices as well as experts of the subject who
want to learn about the brand new OpenCV 3.0.0.
Preface
Setting Up OpenCV
You picked up this book so you may already have an idea of what OpenCV is.
Maybe, you heard of Sci-Fi-sounding features, such as face detection, and got
intrigued. If this is the case, you've made the perfect choice. OpenCV stands for
Open Source Computer Vision. It is a free computer vision library that allows you
to manipulate images and videos to accomplish a variety of tasks from displaying
the feed of a webcam to potentially teaching a robot to recognize real-life objects.
In this book, you will learn to leverage the immense potential of OpenCV with the
Python programming language. Python is an elegant language with a relatively
shallow learning curve and very powerful features. This chapter is a quick guide to
setting up Python 2.7, OpenCV, and other related libraries. After setup, we also look
at OpenCV's Python sample scripts and documentation.
If you wish to skip the installation process and jump right into action,
you can download the virtual machine (VM) I've made available at
http://techfort.github.io/pycv/.
This file is compatible with VirtualBox, a free-to-use virtualization
application that lets you build and run VMs. The VM I've built is
based on Ubuntu Linux 14.04 and has all the necessary software
installed so that you can start coding right away.
This VM requires at least 2 GB of RAM to run smoothly, so make sure
that you allocate at least 2 (but, ideally, more than 4) GB of RAM to
the VM, which means that your host machine will need at least 6 GB
of RAM to sustain it.
[1]
Setting Up OpenCV
For this book's purposes, OpenNI and SensorKinect can be considered optional.
They are used throughout Chapter 4, Depth Estimation and Segmentation, but are
not used in the other chapters or appendices.
This book focuses on OpenCV 3, the new major release of the
OpenCV library. All additional information about OpenCV is
available at http://opencv.org, and its documentation is
available at http://docs.opencv.org/master.
Installation on Windows
Windows does not come with Python preinstalled. However, installation wizards are
available for precompiled Python, NumPy, SciPy, and OpenCV. Alternatively, we
can build from a source. OpenCV's build system uses CMake for configuration and
either Visual Studio or MinGW for compilation.
If we want support for depth cameras, including Kinect, we should first install
OpenNI and SensorKinect, which are available as precompiled binaries with
installation wizards. Then, we must build OpenCV from a source.
The precompiled version of OpenCV does not offer support
for depth cameras.
[2]
Chapter 1
On Windows, OpenCV 2 offers better support for 32-bit Python than 64-bit Python;
however, with the majority of computers sold today being 64-bit systems, our
instructions will refer to 64-bit. All installers have 32-bit versions available from the
same site as the 64-bit.
Some of the following steps refer to editing the system's PATH variable. This task can
be done in the Environment Variables window of Control Panel.
1. On Windows Vista / Windows 7 / Windows 8, click on the Start menu and
launch Control Panel. Now, navigate to System and Security | System |
Advanced system settings. Click on the Environment Variables button.
2. On Windows XP, click on the Start menu and navigate to Control Panel |
System. Select the Advanced tab. Click on the Environment Variables
button.
3. Now, under System variables, select Path and click on the Edit button.
4. Make changes as directed.
5. To apply the changes, click on all the OK buttons (until we are back in the
main window of Control Panel).
6. Then, log out and log back in (alternatively, reboot).
downloaded, run it and remember to add the path to the Anaconda installation to
your PATH variable following the preceding procedure.
[3]
Setting Up OpenCV
Here are the steps to set up Python7, NumPy, SciPy, and OpenCV:
1. Download and install the 32-bit Python 2.7.9 from https://www.python.
org/ftp/python/2.7.9/python-2.7.9.amd64.msi.
2. Download and install NumPy 1.6.2 from http://www.lfd.uci.
edu/~gohlke/pythonlibs/#numpyhttp://sourceforge.net/projects/
numpy/files/NumPy/1.6.2/numpy-1.6.2-win32-superpackpython2.7.exe/download (note that installing NumPy on Windows 64-bit
is a bit tricky due to the lack of a 64-bit Fortran compiler on Windows, which
NumPy depends on. The binary at the preceding link is unofficial).
3. Download and install SciPy 11.0 from http://www.lfd.uci.edu/~gohlke/
pythonlibs/#scipyhttp://sourceforge.net/projects/scipy/files/
scipy/0.11.0/scipy-0.11.0win32-superpack-python2.7.exe/download
[4]
Chapter 1
Let's assume that we have already installed 32-bit Python 2.7, NumPy, and SciPy
either from binaries (as described previously) or from a source. Now, we can
proceed with installing compilers and CMake, optionally installing OpenNI and
SensorKinect, and then building OpenCV from the source:
1. Download and install CMake 3.1.2 from http://www.cmake.org/files/
v3.1/cmake-3.1.2-win32-x86.exe. When running the installer, select
either Add CMake to the system PATH for all users or Add CMake to
the system PATH for current user. Don't worry about the fact that a 64-bit
version of CMake is not available CMake is only a configuration tool and
does not perform any compilations itself. Instead, on Windows, it creates
project files that can be opened with Visual Studio.
2. Download and install Microsoft Visual Studio 2013 (the Desktop edition if
you are working on Windows 7) from https://www.visualstudio.com/
products/free-developer-offers-vs.aspx?slcid=0x409&type=web
or MinGW.
Note that you will need to sign in with your Microsoft account and if you
don't have one, you can create one on the spot. Install the software and
reboot after installation is complete.
For MinGW, get the installer from http://sourceforge.net/projects/
mingw/files/Installer/mingw-get-setup.exe/download and http://
sourceforge.net/projects/mingw/files/OldFiles/mingw-get-inst/
mingw-get-inst-20120426/mingw-get-inst-20120426.exe/download.
When running the installer, make sure that the destination path does not
contain spaces and that the optional C++ compiler is included. Edit the
system's PATH variable and append ;C:\MinGW\bin (assuming that MinGW
is installed to the default location). Reboot the system.
3. Optionally, download and install OpenNI 1.5.4.0 from the links provided in
the GitHub homepage of OpenNI at https://github.com/OpenNI/OpenNI.
4. You can download and install SensorKinect 0.93 from https://github.com/
avin2/SensorKinect/blob/unstable/Bin/SensorKinect093-Bin-Win32v5.1.2.1.msi?raw=true (32-bit). Alternatively, for 64-bit Python, download
the setup from https://github.com/avin2/SensorKinect/blob/
unstable/Bin/SensorKinect093-Bin-Win64-v5.1.2.1.msi?raw=true
(64-bit). Note that this repository has been inactive for more than three years.
5. Download the self-extracting ZIP of OpenCV 3.0.0 from https://github.
com/Itseez/opencv. Run the self-extracting ZIP, and when prompted, enter
any destination folder, which we will refer to as <unzip_destination>. A
subfolder, <unzip_destination>\opencv, is then created.
[5]
Setting Up OpenCV
6. Open Command Prompt and make another folder where our build will go
using this command:
> mkdir<build_folder>
7. Now, we are ready to configure our build. To understand all the options,
we can read the code in <unzip_destination>\opencv\CMakeLists.txt.
However, for this book's purposes, we only need to use the options that will
give us a release build with Python bindings, and optionally, depth camera
support via OpenNI and SensorKinect.
8. Open CMake (cmake-gui) and specify the location of the source code of
OpenCV and the folder where you would like to build the library. Click on
Configure. Select the project to be generated. In this case, select Visual Studio
12 (which corresponds to Visual Studio 2013). After CMake has finished
configuring the project, it will output a list of build options. If you see a red
background, it means that your project may need to be reconfigured: CMake
might report that it has failed to find some dependencies. Many of OpenCV's
dependencies are optional, so do not be too concerned yet.
If the build fails to complete or you run into problems later,
try installing missing dependencies (often available as prebuilt
binaries), and then rebuild OpenCV from this step.
You have the option of selecting/deselecting build options
(according to the libraries you have installed on your machine) and
click on Configure again, until you get a clear background (white).
9. At the end of this process, you can click on Generate, which will create
an OpenCV.sln file in the folder you've chosen for the build. You can then
navigate to <build_folder>/OpenCV.sln and open the file with Visual
Studio 2013, and proceed with building the project, ALL_BUILD. You will
need to build both the Debug and Release versions of OpenCV, so go ahead
and build the library in the Debug mode, then select Release and rebuild it
(F7 is the key to launch the build).
10. At this stage, you will have a bin folder in the OpenCV build directory,
which will contain all the generated .dll files that will allow you to include
OpenCV in your projects.
Alternatively, for MinGW, run the following command:
> cmake -D:CMAKE_BUILD_TYPE=RELEASE -D:WITH_OPENNI=ON -G
"MinGWMakefiles" <unzip_destination>\opencv
[6]
Chapter 1
Installing on OS X
Some versions of Mac used to come with a version of Python 2.7 preinstalled that
were customized by Apple for a system's internal needs. However, this has changed
and the standard version of OS X ships with a standard installation of Python. On
python.org, you can also find a universal binary that is compatible with both the
new Intel systems and the legacy PowerPC.
You can obtain this installer at https://www.python.org/
downloads/release/python-279/ (refer to the Mac OS X 32-bit
PPC or the Mac OS X 64-bit Intel links). Installing Python from the
downloaded .dmg file will simply overwrite your current system
installation of Python.
For Mac, there are several possible approaches for obtaining standard Python 2.7,
NumPy, SciPy, and OpenCV. All approaches ultimately require OpenCV to be
compiled from a source using Xcode Developer Tools. However, depending on the
approach, this task is automated for us in various ways by third-party tools. We will
look at these kinds of approaches using MacPorts or Homebrew. These tools can
potentially do everything that CMake can, plus they help us resolve dependencies
and separate our development libraries from system libraries.
[7]
Setting Up OpenCV
Before proceeding, let's make sure that the Xcode Developer Tools are properly
set up:
1. Download and install Xcode from the Mac App Store or
https://developer.apple.com/xcode/downloads/. During
installation, if there is an option to install Command Line Tools, select it.
2. Open Xcode and accept the license agreement.
3. A final step is necessary if the installer does not give us the option to install
Command Line Tools. Navigate to Xcode | Preferences | Downloads,
and click on the Install button next to Command Line Tools. Wait for the
installation to finish and quit Xcode.
Alternatively, you can install Xcode command-line tools by running the following
command (in the terminal):
$ xcode-select install
http://www.macports.org/install.php.
[8]
Chapter 1
2. If you want support for the Kinect depth camera, you need to tell MacPorts
where to download the custom Portfiles that I have written. To do so, edit
/opt/local/etc/macports/sources.conf (assuming that MacPorts
is installed to the default location). Just above the line, rsync://rsync.
macports.org/release/ports/ [default], add the following line:
http://nummist.com/opencv/ports.tar.gz
Save the file. Now, MacPorts knows that it has to search for Portfiles in my
online repository first, and then the default online repository.
3. Open the terminal and run the following command to update MacPorts:
$ sudo port selfupdate
[9]
Setting Up OpenCV
Note the triple slashes and save the file. Now, MacPorts knows that it has
to search for Portfiles in <local_repository> first, and then, its default
online repository.
3. Open the terminal and update MacPorts to ensure that we have the latest
Portfiles from the default repository:
$ sudo port selfupdate
[ 10 ]
Chapter 1
7. From now on, we can treat our custom opencv file just like any other
MacPorts package. For example, we can install it as follows:
$ sudo port install opencv +python27 +openni_sensorkinect
Note that our local repository's Portfile takes precedence over the default
repository's Portfile because of the order in which they are listed in /opt/
local/etc/macports/sources.conf.
[ 11 ]
Setting Up OpenCV
Chapter 1
8. To install OpenCV on a 64-bit system (all new Mac hardware since late 2006),
run the following command:
$ brew install opencv
The package definitions are actually scripts in the Ruby programming language. Tips
on editing them can be found on the Homebrew Wiki page at https://github.com/
mxcl/homebrew/wiki/Formula-Cookbook. A script may specify Make or CMake
configuration flags, among other things.
To see which CMake configuration flags are relevant to OpenCV, we need to look
at its source code. Download the source code archive from https://github.com/
Itseez/opencv/archive/3.0.0.zip, unzip it to any location, and read
<unzip_destination>/OpenCV-2.4.3/CMakeLists.txt.
After making edits to the Ruby script, save it.
The customized package can be treated as normal. For example, it can be installed
as follows:
$ brew install opencv
[ 13 ]
Setting Up OpenCV
Ubuntu comes with Python 2.7 preinstalled. The standard Ubuntu repository
contains OpenCV 2.4.9 packages without support for depth cameras. At the time
of writing this, OpenCV 3 is not yet available through the Ubuntu repositories, so
we will have to build it from source. Fortunately, the vast majority of Unix-like and
Linux systems come with all the necessary software to build a project from scratch
already installed. When built from source, OpenCV can support depth cameras
via OpenNI and SensorKinect, which are available as precompiled binaries with
installation scripts.
Equivalently, we could have used Ubuntu Software Center, which is the apt package
manager's graphical frontend.
After the installation terminates, you might want to look at OpenCV's Python
samples in <opencv_folder>/opencv/samples/python and <script_folder>/
opencv/samples/python2.
[ 14 ]
Chapter 1
Linux Mint 17
On Debian Linux and its derivatives, the apt package manager works the same as on
Ubuntu, though the available packages may differ.
On Gentoo Linux and its derivatives, the Portage package manager is similar to
MacPorts (as described previously), though the available packages may differ.
On FreeBSD derivatives, the process of installation is again similar to MacPorts;
in fact, MacPorts derives from the ports installation system adopted on FreeBSD.
Consult the remarkable FreeBSD Handbook at https://www.freebsd.org/doc/
handbook/ for an overview of the software installation process.
On other Unix-like systems, the package manager and available packages may
differ. Consult your package manager's documentation and search for packages with
opencv in their names. Remember that OpenCV and its Python bindings might be
split into multiple packages.
Also, look for any installation notes published by the system provider, the repository
maintainer, or the community. Since OpenCV uses camera drivers and media codecs,
getting all of its functionality to work can be tricky on systems with poor multimedia
support. Under some circumstances, system packages might need to be reconfigured
or reinstalled for compatibility.
If packages are available for OpenCV, check their version number. OpenCV 3 or
higher is recommended for this book's purposes. Also, check whether the packages
offer Python bindings and depth camera support via OpenNI and SensorKinect.
Finally, check whether anyone in the developer community has reported success or
failure in using the packages.
If, instead, we want to do a custom build of OpenCV from source, it might be helpful
to refer to the installation script for Ubuntu (as discussed previously) and adapt it to
the package manager and packages that are present on another system.
[ 15 ]
Setting Up OpenCV
So, if you've followed the standard procedure and created a build directory in your
OpenCV download folder, you should run the following command:
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=Release -DOPENCV_EXTRA_MODULES_PATH=<opencv_
contrib>/modules -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
Running samples
Running a few sample scripts is a good way to test whether OpenCV is correctly set
up. The samples are included in OpenCV's source code archive.
On Windows, we should have already downloaded and unzipped OpenCV's
self-extracting ZIP. Find the samples in <unzip_destination>/opencv/samples.
On Unix-like systems, including Mac, download the source code archive from
https://github.com/Itseez/opencv/archive/3.0.0.zip and unzip it to
any location (if we have not already done so). Find the samples in <unzip_
destination>/OpenCV-3.0.0/samples.
[ 16 ]
Chapter 1
a screensaver.
Some steps in the OpenCV installation might have failed or been missed.
Go back and review the steps.
[ 17 ]
Setting Up OpenCV
If you write code on airplanes or other places without Internet access, you will
definitely want to keep offline copies of the documentation.
If the documentation does not seem to answer your questions, try talking to the
OpenCV community. Here are some sites where you will find helpful people:
Joe Minichino's website for this book (author of this book): http://
techfort.github.io/pycv/
Joe Howse's website for this book (author of the first edition of this book):
http://nummist.com/opencv/
Lastly, if you are an advanced user who wants to try new features, bug fixes, and
sample scripts from the latest (unstable) OpenCV source code, have a look at the
project's repository at https://github.com/Itseez/opencv/.
[ 18 ]
Chapter 1
Summary
By now, we should have an OpenCV installation that can do everything we need for
the project described in this book. Depending on which approach we took, we might
also have a set of tools and scripts that are usable to reconfigure and rebuild OpenCV
for our future needs.
We know where to find OpenCV's Python samples. These samples covered a
different range of functionalities outside this book's scope, but they are useful as
additional learning aids.
In the next chapter, we will familiarize ourselves with the most basic functions of
the OpenCV API, namely, displaying images, videos, capturing videos through a
webcam, and handling basic keyboard and mouse inputs.
[ 19 ]
www.PacktPub.com
Stay Connected: