Instant Download OpenCV Computer Vision Application Programming Cookbook 2nd Edition Robert Laganiere PDF All Chapters
Instant Download OpenCV Computer Vision Application Programming Cookbook 2nd Edition Robert Laganiere PDF All Chapters
https://ebookultra.com/download/learning-opencv-2nd-edition-computer-
vision-in-c-with-the-opencv-library-adrian-kaehler/
ebookultra.com
https://ebookultra.com/download/multiple-view-geometry-in-computer-
vision-2nd-edition-richard-hartley/
ebookultra.com
https://ebookultra.com/download/multiple-view-geometry-in-computer-
vision-2nd-edition-richard-hartley-2/
ebookultra.com
https://ebookultra.com/download/fundamentals-of-computer-and-
programming-2nd-edition-e-balagurusamy/
ebookultra.com
Template Matching Techniques in Computer Vision Roberto
Brunelli
https://ebookultra.com/download/template-matching-techniques-in-
computer-vision-roberto-brunelli/
ebookultra.com
https://ebookultra.com/download/computer-vision-a-modern-approach-
david-a-forsyth/
ebookultra.com
https://ebookultra.com/download/programming-concepts-in-c-2nd-edition-
robert-burns/
ebookultra.com
https://ebookultra.com/download/a-computer-science-tapestry-exploring-
programming-and-computer-science-with-c-2nd-edition-owen-l-astrachan/
ebookultra.com
https://ebookultra.com/download/maya-programming-with-python-
cookbook-1st-edition-herbez/
ebookultra.com
OpenCV Computer Vision Application Programming
Cookbook 2nd Edition Robert Laganiere Digital Instant
Download
Author(s): Robert Laganiere
ISBN(s): 9781782161486, 1782161481
File Details: PDF, 6.79 MB
Year: 2014
Language: english
OpenCV Computer Vision Application
Programming Cookbook Second Edition
Table of Contents
OpenCV Computer Vision Application Programming Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Playing with Images
Introduction
Installing the OpenCV library
Getting ready
How to do it…
How it works…
There’s more…
Using Qt for OpenCV developments
The OpenCV developer site
See also
Loading, displaying, and saving images
Getting ready
How to do it…
How it works…
There’s more…
Clicking on images
Drawing on images
Running the example with Qt
See also
Exploring the cv::Mat data structure
How to do it…
How it works…
There’s more…
The input and output arrays
The old IplImage structure
See also
Defining regions of interest
Getting ready
How to do it…
How it works…
There’s more…
Using image masks
See also
2. Manipulating Pixels
Introduction
Accessing pixel values
Getting ready
How to do it…
How it works…
There’s more…
The cv::Mat_ template class
See also
Scanning an image with pointers
Getting ready
How to do it…
How it works…
There’s more…
Other color reduction formulas
Having input and output arguments
Efficient scanning of continuous images
Low-level pointer arithmetics
See also
Scanning an image with iterators
Getting ready
How to do it…
How it works…
There’s more…
See also
Writing efficient image-scanning loops
How to do it…
How it works…
There’s more…
See also
Scanning an image with neighbor access
Getting ready
How to do it…
How it works…
There’s more…
See also
Performing simple image arithmetic
Getting ready
How to do it…
How it works…
There’s more…
Overloaded image operators
Splitting the image channels
Remapping an image
How to do it…
How it works…
See also
3. Processing Color Images with Classes
Introduction
Using the Strategy pattern in an algorithm design
Getting ready
How to do it…
How it works…
There’s more…
Computing the distance between two color vectors
Using OpenCV functions
The functor or function object
See also
Using a Controller design pattern to communicate with processing modules
Getting ready
How to do it…
How it works…
There’s more…
The Model-View-Controller architecture
Converting color representations
Getting ready
How to do it…
How it works…
See also
Representing colors with hue, saturation, and brightness
How to do it…
How it works…
There’s more…
Using colors for detection – skin tone detection
4. Counting the Pixels with Histograms
Introduction
Computing the image histogram
Getting started
How to do it…
How it works…
There’s more…
Computing histograms of color images
See also
Applying look-up tables to modify the image appearance
How to do it…
How it works…
There’s more…
Stretching a histogram to improve the image contrast
Applying a look-up table on color images
See also
Equalizing the image histogram
How to do it…
How it works…
Backprojecting a histogram to detect specific image content
How to do it…
How it works…
There’s more…
Backprojecting color histograms
See also
Using the mean shift algorithm to find an object
How to do it…
How it works…
See also
Retrieving similar images using the histogram comparison
How to do it…
How it works…
See also
Counting pixels with integral images
How to do it…
How it works…
There’s more…
Adaptive thresholding
Visual tracking using histograms
See also
5. Transforming Images with Morphological Operations
Introduction
Eroding and dilating images using morphological filters
Getting ready
How to do it…
How it works…
There’s more…
See also
Opening and closing images using morphological filters
How to do it…
How it works…
See also
Detecting edges and corners using morphological filters
Getting ready
How to do it…
How it works…
See also
Segmenting images using watersheds
How to do it…
How it works…
There’s more…
See also
Extracting distinctive regions using MSER
How to do it…
How it works…
See also
Extracting foreground objects with the GrabCut algorithm
How to do it…
How it works…
See also
6. Filtering the Images
Introduction
Filtering images using low-pass filters
How to do it…
How it works…
There’s more…
Downsampling an image
Interpolating pixel values
See also
Filtering images using a median filter
How to do it…
How it works…
Applying directional filters to detect edges
How to do it…
How it works…
There’s more…
Gradient operators
Gaussian derivatives
See also
Computing the Laplacian of an image
How to do it…
How it works…
There’s more…
Enhancing the contrast of an image using the Laplacian
Difference of Gaussians
See also
7. Extracting Lines, Contours, and Components
Introduction
Detecting image contours with the Canny operator
How to do it…
How it works…
See also
Detecting lines in images with the Hough transform
Getting ready
How to do it…
How it works…
There’s more…
Detecting circles
See also
Fitting a line to a set of points
How to do it…
How it works…
There’s more…
Extracting the components’ contours
How to do it…
How it works…
There’s more…
Computing components’ shape descriptors
How to do it…
How it works…
There’s more…
Quadrilateral detection
8. Detecting Interest Points
Introduction
Detecting corners in an image
How to do it…
How it works…
There’s more…
Good features to track
The feature detector’s common interface
See also
Detecting features quickly
How to do it…
How it works…
There’s more…
Adapted feature detection
Grid adapted feature detection
Pyramid adapted feature detection
See also
Detecting scale-invariant features
How to do it…
How it works…
There’s more…
The SIFT feature-detection algorithm
See also
Detecting FAST features at multiple scales
How to do it…
How it works…
There’s more…
The ORB feature-detection algorithm
See also
9. Describing and Matching Interest Points
Introduction
Matching local templates
How to do it…
How it works…
There’s more…
Template matching
See also
Describing local intensity patterns
How to do it…
How it works…
There’s more…
Cross-checking matches
The ratio test
Distance thresholding
See also
Describing keypoints with binary features
How to do it…
How it works…
There’s more…
FREAK
See also
10. Estimating Projective Relations in Images
Introduction
Image formation
Calibrating a camera
How to do it…
How it works…
There’s more…
Calibration with known intrinsic parameters
Using a grid of circles for calibration
See also
Computing the fundamental matrix of an image pair
Getting ready
How to do it…
How it works…
See also
Matching images using a random sample consensus
How to do it…
How it works…
There’s more…
Refining the fundamental matrix
Refining the matches
Computing a homography between two images
Getting ready
How to do it…
How it works…
There’s more…
Detecting planar targets in an image
See also
11. Processing Video Sequences
Introduction
Reading video sequences
How to do it…
How it works…
There’s more…
See also
Processing the video frames
How to do it…
How it works…
There’s more…
Processing a sequence of images
Using a frame processor class
See also
Writing video sequences
How to do it…
How it works…
There’s more…
The codec four-character code
See also
Tracking feature points in a video
How to do it…
How it works…
See also
Extracting the foreground objects in a video
How to do it…
How it works…
There’s more…
The Mixture of Gaussian method
See also
Index
OpenCV Computer Vision Application
Programming Cookbook Second Edition
OpenCV Computer Vision Application
Programming Cookbook Second Edition
Copyright © 2014 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 author, nor Packt Publishing, and its
dealers and distributors will be held liable for any damages caused or alleged to be 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.
First published: May 2011
Second edition: August 2014
Production reference: 1200814
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-148-6
www.packtpub.com
Cover image by Mathieu Paquette (<[email protected]>)
Credits
Author
Robert Laganière
Reviewers
Walter Lucetti
Anton Sukhinov
Tao Wu
Commissioning Editor
Usha Iyer
Acquisition Editors
Usha Iyer
Neha Nagwekar
Content Development Editor
Amey Varangaonkar
Technical Editors
Indrajit A. Das
Pooja Nair
Humera Shaikh
Copy Editors
Dipti Kapadia
Stuti Srivastava
Project Coordinator
Leena Purkait
Proofreaders
Simran Bhogal
Maria Gould
Ameesha Green
Paul Hindle
Indexer
Hemangini Bari
Graphics
Sheetal Aute
Disha Haria
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph
About the Author
Robert Laganière is a professor at the School of Electrical Engineering and Computer
Science of the University of Ottawa, Canada. He is also a faculty member of the VIVA
research lab and is the co-author of several scientific publications and patents in content-
based video analysis, visual surveillance, object recognition, and 3D reconstruction.
Robert authored OpenCV2 Computer Vision Application Programming Cookbook, Packt
Publishing, in 2011 and co-authored Object Oriented Software Development, McGraw
Hill, in 2001. He co-founded Visual Cortek in 2006, an Ottawa-based video analytics
startup that was later acquired by iWatchLife (www.iwatchlife.com) in 2009, where he
also assumes the role of Chief Scientist. Since 2011, he is also Chief Scientist at Cognivue
Corp, which is a leader in embedded vision solutions. He has a Bachelor of Electrical
Engineering degree from Ecole Polytechnique in Montreal (1987) and MSc and PhD
degrees from INRS-Telecommunications, Montreal (1996). You can visit his website at
www.laganiere.name.
I wish to thank all my students at the VIVA lab; I learn so much from them.
About the Reviewers
Walter Lucetti, known on the Internet as Myzhar, is an Italian computer engineer with a
specialization in Robotics and Robotics Perception. He received a Laurea degree in 2005
while studying at Research Center “E.Piaggio” in Pisa (Italy). He wrote a thesis on the 3D
mapping of the real world using a 2D Laser tilted using a servo motor, fusing 3D with
RGB data. During the writing of his thesis, he got introduced to OpenCV for the first time.
It was early 2004 and OpenCV was at its larval stage.
After the Laurea, he started working as a software developer for low-level embedded
systems and high-level desktop systems. He deeply improved his knowledge about
Computer Vision and Machine Learning as a researcher at Gustavo Stefanini Advanced
Robotics Research Center in La Spezia (Italy), which is a spinoff of PERCRO Laboratory
of the Scuola Superiore Sant’Anna of Pisa (Italy).
Now, he is working in the industry, developing firmware for embedded ARM systems and
intelligent algorithms for video surveillance systems.
He is also working on a personal project: MyzharBot. MyzharBot is a tracked ground
mobile robot that uses stereo vision to detect obstacles and analyze and explore the
environment.
You can find more information about Walter, his project, and a lot of tutorials on computer
vision at www.robot-home.it and http://myzharbot.robot-home.it.
Anton Sukhinov graduated from Moscow Institute of Physics and Technology. He has a
PhD degree in Physics and Mathematics.
Anton has experience in mathematical modeling, numerical simulation, image processing
and analysis, machine learning, and electronics.
During the reviewing of this book, Anton was a senior developer in CVisionLab, which is
a small Russian R&D firm dedicated to image processing, analysis, machine learning, and
related fields.
Currently, he works as a research scientist at Skolkovo Institute of Science and
Technology (Russia).
Tao Wu graduated from University of Maryland with a PhD degree in Electrical
Engineering in 2013. He received his MS and BE degrees in Electrical Engineering from
Tsinghua University, Beijing, in 2008 and 2005, respectively.
He has more than 7 years of experience in C/C++ and OpenCV. His research interests
include computer vision, pattern recognition, machine learning, and image and video
processing.
Currently, he is working for Microsoft.
www.PacktPub.com
Support files, eBooks, discount offers, and
more
You might want to visit www.PacktPub.com for support files and downloads related to
your book.
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.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt’s online digital
book library. Here, you can access, read and search across Packt’s entire library of books.
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view nine entirely free books. Simply use your login credentials for
immediate access.
Preface
OpenCV (Open source Computer Vision) is an open source library that contains more than
500 optimized algorithms for image and video analysis. Since its introduction in 1999, it
has been largely adopted as the primary development tool by the community of
researchers and developers in computer vision. OpenCV was originally developed at Intel
by a team led by Gary Bradski as an initiative to advance research in vision and promote
the development of rich vision-based, CPU-intensive applications. After a series of beta
releases, Version 1.0 was launched in 2006. A second major release occurred in 2009 with
the launch of OpenCV 2 that proposed important changes, especially the new C++
interface that we use in this book. In 2012, OpenCV reshaped itself as a nonprofit
foundation (http://opencv.org/) that relies on crowdfunding for its future development.
This book is a new edition of OpenCV Computer Vision Application Programming
Cookbook. All the programming recipes of the previous editions have been reviewed and
updated. We also have added new content to provide readers with even better coverage of
the essential functionalities of the library. This book covers many of the library’s features
and shows you how to use them to accomplish specific tasks. Our objective is not to
provide detailed coverage of every option offered by the OpenCV functions and classes,
but rather to give you the elements you need to build your applications from the ground
up. In this book, we also explore fundamental concepts in image analysis, and we describe
some of the important algorithms in computer vision.
This book is an opportunity for you to get introduced to the world of image and video
analysis. However, this is just the beginning. The good news is that OpenCV continues to
evolve and expand. Just consult the OpenCV online documentation at http://opencv.org/ to
stay updated on what the library can do for you. You can also visit the author’s website at
www.laganiere.name for updated information about this Cookbook.
What this book covers
Chapter 1, Playing with Images, introduces the OpenCV library and shows you how to
build simple applications that can read and display images. It also introduces the basic
OpenCV data structures.
Chapter 2, Manipulating Pixels, explains how an image can be read. It describes different
methods for scanning an image in order to perform an operation on each of its pixels.
Chapter 3, Processing Color Images with Classes, consists of recipes that present various
object-oriented design patterns that can help you build better computer vision applications.
It also discusses the concept of colors in images.
Chapter 4, Counting the Pixels with Histograms, shows you how to compute image
histograms and how they can be used to modify an image. Different applications based on
histograms are presented, and they achieve image segmentation, object detection, and
image retrieval.
Chapter 5, Transforming Images with Morphological Operations, explores the concept of
mathematical morphology. It presents different operators and informs you how they can be
used to detect edges, corners, and segments in images.
Chapter 6, Filtering the Images, teaches you the principle of frequency analysis and image
filtering. It shows how low-pass and high-pass filters can be applied to images and
presents the concept of derivative operators.
Chapter 7, Extracting Lines, Contours, and Components, focuses on the detection of
geometric image features. It explains how to extract contours, lines, and connected
components in an image.
Chapter 8, Detecting Interest Points, describes various feature-point detectors in images.
Chapter 9, Describing and Matching Interest Points, explains how descriptors of interest
points can be computed and used to match points between images.
Chapter 10, Estimating Projective Relations in Images, explores the projective relations
that exist between two images of the same scene. It also describes the process of camera
calibration and revisits the problem of matching feature points.
Chapter 11, Processing Video Sequences, provides you with a framework to read and write
a video sequence and process its frames. It also shows you how it is possible to track
feature points from frame to frame and how to extract the foreground objects moving in
front of a camera.
What you need for this book
This Cookbook is based on the C++ API of the OpenCV library. Therefore, it is assumed
that you have some experience with the C++ language. In order to run the examples
presented in the recipes and experiment with them, you need a good C++ development
environment. Microsoft Visual Studio and Qt are two popular choices.
Who this book is for
This Cookbook is appropriate for novice C++ programmers who want to learn how to use
the OpenCV library to build computer vision applications. It is also suitable for
professional software developers who wish to be introduced to the concepts of computer
vision programming. It can be used as a companion book for university-level computer
vision courses. It is an excellent reference for graduate students and researchers of image
processing and computer vision.
Conventions
In this book, you will find a number of styles of text that distinguish between different
kinds of information. Here are some examples of these styles, and an explanation of their
meaning.
Code words in text, folder names, filenames, file extensions, pathnames, dummy URLs,
and user input are shown as follows: “Very conveniently, this check is encapsulated inside
the create method of cv::Mat.”
A block of code is set as follows:
// use image with a Mat_ template
cv::Mat_<uchar> im2(image);
im2(50,100)= 0; // access to row 50 and column 100
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or may have disliked. Reader feedback is important for us to
develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and
mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or
contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help
you to get the most from your purchase.
Downloading the example code
You can download the example code files for all Packt books you have purchased from
your account at http://www.packtpub.com. If you purchased this book elsewhere, you can
visit http://www.packtpub.com/support and register to have the files e-mailed directly to
you.
Discovering Diverse Content Through
Random Scribd Documents
d’enfant. Quand l’auto s’arrêta et qu’il fallut descendre, ses jambes,
sans direction, cafouillaient. Elles se disputaient pour atteindre le
marchepied.
— Mme Olmey est toujours dans le bureau de la direction ?
Mais le portier répondait à de nouveaux arrivants qui
demandaient des chambres.
D’un geste déférent de son crayon, il faisait signe à Georges
d’attendre son tour.
Attendre son tour ?
Georges s’adressait déjà à un autre employé qui le regardait un
peu ahuri. Alors, sans attendre de réponse, le jeune homme entraîna
le banquier vers le bureau en question.
Un surveillant se tenait toujours devant la porte, ce qui rassura
Georges en lui indiquant que Béatrice n’avait pas déménagé.
Cet agent fit mine de leur demander pourquoi ils voulaient entrer.
Mais Georges lui répondit avec une volubilité telle, dans une
langue inconnue, que l’autre, impressionné, s’effaça.
Déjà Béatrice avait sauté au cou de M. Markeysen. Georges
profita de ce que Mme Olmey était occupée pour dire bonjour à
Laurence. Mais Béatrice ne l’oubliait pas. Elle lui tendit une petite
main, qui lui sembla fondante de tendresse.
— Asseyons-nous, dit Béatrice.
XVI
FIN
ACHEVÉ D’IMPRIMER
LE 10 JUILLET 1928
PAR LES
ÉTABLISSEMENTS BUSSON
117, RUE DES POISSONNIERS
PARIS
*** END OF THE PROJECT GUTENBERG EBOOK LE VOYAGE
IMPRÉVU ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com