OpenCV 3 Computer Vision Application Programming Cookbook: Recipes to make your applications see
()
About this ebook
Making your applications see has never been easier with OpenCV. With it, you can teach your robot how to follow your cat, write a program to correctly identify the members of One Direction, or even help you find the right colors for your redecoration.
OpenCV 3 Computer Vision Application Programming Cookbook Third Edition provides a complete introduction to the OpenCV library and explains how to build your first computer vision program. You will be presented with a variety of computer vision algorithms and exposed to important concepts in image and video analysis that will enable you to build your own computer vision applications.
This book helps you to get started with the library, and shows you how to install and deploy the OpenCV library to write effective computer vision applications following good programming practices. You will learn how to read and write images and manipulate their pixels. Different techniques for image enhancement and shape analysis will be presented. You will learn how to detect specific image features such as lines, circles or corners. You will be introduced to the concepts of mathematical morphology and image filtering.
The most recent methods for image matching and object recognition are described, and you’ll discover how to process video from files or cameras, as well as how to detect and track moving objects. Techniques to achieve camera calibration and perform multiple-view analysis will also be explained. Finally, you’ll also get acquainted with recent approaches in machine learning and object classification.
Related to OpenCV 3 Computer Vision Application Programming Cookbook
Related ebooks
OpenCV 3 Computer Vision Application Programming Cookbook - Third Edition Rating: 0 out of 5 stars0 ratingsOpenCV Computer Vision Application Programming Cookbook Second Edition Rating: 0 out of 5 stars0 ratingsThree.js Cookbook Rating: 0 out of 5 stars0 ratingsCinder Creative Coding Cookbook Rating: 0 out of 5 stars0 ratingsOpenGL 4.0 Shading Language Cookbook Rating: 0 out of 5 stars0 ratingsIBM SPSS Modeler Cookbook Rating: 0 out of 5 stars0 ratingsAway3D 3.6 Cookbook Rating: 0 out of 5 stars0 ratingsPHP 7: Real World Application Development Rating: 0 out of 5 stars0 ratingsOdoo Development Cookbook Rating: 0 out of 5 stars0 ratingsOpenGL 4 Shading Language Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsR: Recipes for Analysis, Visualization and Machine Learning Rating: 0 out of 5 stars0 ratingsOpenGL Development Cookbook Rating: 5 out of 5 stars5/5Dart Cookbook Rating: 0 out of 5 stars0 ratingsPractical Data Analysis Cookbook Rating: 0 out of 5 stars0 ratingsPHP 7 Programming Cookbook Rating: 0 out of 5 stars0 ratingsGephi Cookbook Rating: 0 out of 5 stars0 ratingsLua Game Development Cookbook Rating: 0 out of 5 stars0 ratingsData Visualization with D3 4.x Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsLess Web Development Cookbook Rating: 0 out of 5 stars0 ratingsHTML5 Data and Services Cookbook Rating: 5 out of 5 stars5/5QGIS Python Programming Cookbook Rating: 0 out of 5 stars0 ratingsBusiness Intelligence with MicroStrategy Cookbook Rating: 0 out of 5 stars0 ratingsJavaFX 1.2 Application Development Cookbook Rating: 0 out of 5 stars0 ratingsQGIS 2 Cookbook Rating: 5 out of 5 stars5/5IBM Cognos 8 Report Studio Cookbook: Over 80 great recipes for taking control of Cognos 8 Report Studio Rating: 0 out of 5 stars0 ratingsUnity 5.x Cookbook: More than 100 solutions to build amazing 2D and 3D games with Unity Rating: 3 out of 5 stars3/5Yii2 Application Development Cookbook - Third Edition Rating: 0 out of 5 stars0 ratings
Intelligence (AI) & Semantics For You
The Roadmap to AI Mastery: A Guide to Building and Scaling Projects Rating: 3 out of 5 stars3/5ChatGPT For Dummies Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 4 out of 5 stars4/5AI for Educators: AI for Educators Rating: 5 out of 5 stars5/5Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Artificial Intelligence: A Guide for Thinking Humans Rating: 4 out of 5 stars4/5Summary of Super-Intelligence From Nick Bostrom Rating: 4 out of 5 stars4/5ChatGPT For Fiction Writing: AI for Authors Rating: 5 out of 5 stars5/5Writing AI Prompts For Dummies Rating: 0 out of 5 stars0 ratingsChat-GPT Income Ideas: Pioneering Monetization Concepts Utilizing Conversational AI for Profitable Ventures Rating: 3 out of 5 stars3/5The Secrets of ChatGPT Prompt Engineering for Non-Developers Rating: 5 out of 5 stars5/5101 Midjourney Prompt Secrets Rating: 3 out of 5 stars3/5Nexus: A Brief History of Information Networks from the Stone Age to AI Rating: 4 out of 5 stars4/5Our Final Invention: Artificial Intelligence and the End of the Human Era Rating: 4 out of 5 stars4/5The AI-Driven Leader: Harnessing AI to Make Faster, Smarter Decisions Rating: 0 out of 5 stars0 ratingsKiller ChatGPT Prompts: Harness the Power of AI for Success and Profit Rating: 2 out of 5 stars2/5Midjourney Mastery - The Ultimate Handbook of Prompts Rating: 5 out of 5 stars5/5Make Money with ChatGPT: Your Guide to Making Passive Income Online with Ease using AI: AI Wealth Mastery Rating: 0 out of 5 stars0 ratingsAI Investing For Dummies Rating: 0 out of 5 stars0 ratingsCo-Intelligence: Living and Working with AI Rating: 4 out of 5 stars4/5ChatGPT Millionaire: Work From Home and Make Money Online, Tons of Business Models to Choose from Rating: 5 out of 5 stars5/53550+ Most Effective ChatGPT Prompts Rating: 0 out of 5 stars0 ratingsBuild a Career in Data Science Rating: 5 out of 5 stars5/5
Reviews for OpenCV 3 Computer Vision Application Programming Cookbook
0 ratings0 reviews
Book preview
OpenCV 3 Computer Vision Application Programming Cookbook - Robert Laganiere
Table of Contents
OpenCV 3 Computer Vision Application Programming Cookbook - Third Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Playing with Images
Introduction
Installing the OpenCV library
Getting ready
How to do it...
How it works...
There's more...
The Visualization Toolkit and the cv::viz module
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
See also
Exploring the cv::Mat data structure
How to do it...
How it works...
There's more...
The input and output arrays
Manipulating small matrices
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 arithmetic
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 the Colors of an Image
Introduction
Comparing colors using the Strategy design pattern
How to do it…
How it works…
There's more…
Computing the distance between two color vectors
Using OpenCV functions
The floodFill function
Functor or function object
The OpenCV base class for algorithms
See also
Segmenting an image with the GrabCut algorithm
How to do it…
How it works…
See also
Converting color representations
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
See also
4. Counting the Pixels with Histograms
Introduction
Computing an image histogram
Getting ready
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's appearance
How to do it...
How it works...
There's more...
Stretching a histogram to improve the image contrast
Applying a look-up table to 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
Applying morphological operators on gray-level images
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
6. Filtering the Images
Introduction
Filtering images using low-pass filters
How to do it...
How it works...
See also
Downsampling images with filters
How to do it...
How it works...
There's more...
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 connected components
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
See also
Detecting features quickly
How to do it...
How it works...
There's more...
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 and matching local intensity patterns
How to do it...
How it works...
There's more...
Cross-checking matches
The ratio test
Distance thresholding
See also
Matching keypoints with binary descriptors
How to do it...
How it works...
There's more...
FREAK
See also
10. Estimating Projective Relations in Images
Introduction
Image formation
Computing the fundamental matrix of an image pair
Getting ready
How to do it...
How it works...
See also
Matching images using 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...
Generating image panoramas with the cv::Stitcher module
See also
Detecting a planar target in images
How to do it...
How it works...
See also
11. Reconstructing 3D Scenes
Introduction
Digital 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
Recovering camera pose
How to do it...
How it works...
There's more...
cv::Viz, a 3D Visualizer module
See also
Reconstructing a 3D scene from calibrated cameras
How to do it...
How it works...
There's more...
Decomposing a homography
Bundle adjustment
See also
Computing depth from stereo image
Getting ready
How to do it...
How it works...
See also
12. 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
Extracting the foreground objects in a video
How to do it...
How it works...
There's more...
The Mixture of Gaussian method
See also
13. Tracking Visual Motion
Introduction
Tracing feature points in a video
How to do it...
How it works...
See also
Estimating the optical flow
Getting ready
How to do it...
How it works...
See also
Tracking an object in a video
How to do it...
How it works...
See also
14. Learning from Examples
Introduction
Recognizing faces using nearest neighbors of local binary patterns
How to do it...
How it works...
See also
Finding objects and faces with a cascade of Haar features
Getting ready
How to do it...
How it works...
There's more...
Face detection with a Haar cascade
See also
Detecting objects and people with Support Vector Machines and histograms of oriented gradients
Getting ready
How to do it...
How it works...
There's more...
HOG visualization
People detection
Deep learning and Convolutional Neural Networks
See also
OpenCV 3 Computer Vision Application Programming Cookbook - Third Edition
OpenCV 3 Computer Vision Application Programming Cookbook - Third Edition
Copyright © 2017 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
Third edition: February 2017
Production reference: 1070217
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78646-971-7
www.packtpub.com
Credits
About the Author
Robert Laganiere 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, driver-assistance, object detection, and tracking. Robert authored the OpenCV2 Computer Vision Application Programming Cookbook in 2011 and co-authored Object Oriented Software Development published by McGraw Hill in 2001. He co-founded Visual Cortek in 2006, an Ottawa-based video analytics startup that was later acquired by http://iwatchlife.com/ in 2009. He is also a consultant in computer vision and has assumed the role of Chief Scientist in a number of startups companies such as Cognivue Corp, iWatchlife, and Tempo Analytics. Robert has a Bachelor of Electrical Engineering degree from Ecole Polytechnique in Montreal (1987) and MSc and PhD degrees from INRS-Telecommunications, Montreal (1996). Visit the author’s website at http://www.laganiere.name/.
I wish to thank all my students at the VIVA lab; I learn so much from them.
About the Reviewer
Luca Del Tongo is a computer engineer with a strong passion for algorithms, computer vision, and image processing techniques. He's the coauthor of a free e-book called Data Structures and Algorithms (DSA) with over 100k downloads so far and has published several image processing tutorials on his YouTube channel using Emgu CV. During his master's thesis, he developed an image forensic algorithm published in a scientific paper called Copy Move forgery detection and localization by means of robust clustering with J-Linkage. Currently, Luca works as a software engineer in the ophthalmology field developing corneal topography, processing algorithms, IOL calculation, and computerized chart projector. He loves to play sport and follow MOOC courses in his spare time.
You can contact him through his blog at http://blogs.ugidotnet.org/wetblog.
www.PacktPub.com
For support files and downloads related to your book, please visit www.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.
www.PacktPub.comhttps://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously—that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn.
You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: [email protected].
Preface
Augmented reality, driving assistance, video monitoring; more and more applications are now using computer vision and image analysis technologies, and yet we are still in the infancy of the development of new computerized systems capable of understanding our worlds through the sense of vision. And with the advent of powerful and affordable computing devices and visual sensors, it has never been easier to create sophisticated imaging applications. A multitude of software tools and libraries manipulating images and videos are available, but for anyone who wishes to develop smart vision-based applications, the OpenCV library is the tool to use. OpenCV (Open source Computer Vision) is an open source library containing 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, which we use in this book. In 2012, OpenCV reshaped itself as a non-profit foundation (http://opencv.org/) relying on crowdfunding for its future development. OpenCV3 was introduced in 2013; changes were made mainly to improve the usability of library. Its structure has been revised to remove the unnecessary dependencies, large modules have been split into smaller ones and the API has been refined. This book is the third edition of the OpenCV Computer Vision Application Programming Cookbook and the first one that covers OpenCV version 3. All the programming recipes of the previous editions have been reviewed and updated. We also have added new content and new chapters to provide readers with even better coverage of the essential functionalities of the library. This book covers many of the library’s features and explains 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. We also explore, in this book, 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. But 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 about what the library can do for you. You can also visit the author’s website at http://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 the Colors of an Image, consists of recipes presenting various object-oriented design patterns that can help you to 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 that 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 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 detector 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 in the same scene. It also describes how to detect specific targets in an image.
Chapter 11, Reconstructing 3D scenes, allows you to reconstruct the 3D elements of a scene from multiple images and recover the camera pose. It also includes a description of the camera calibration process.
Chapter 12, Processing Video Sequences, provide a framework to read and write a video sequence and to process its frames. It shows you also how it is possible to extract the foreground objects moving in front of a camera.
Chapter 13, Tracking Visual Motion, addresses the visual tracking problem. It shows you how to compute the apparent motion in videos. It also explains how to track moving objects in an image sequence.
Chapter 14, Learning from Examples, introduces basic concepts in machine learning. It shows how object classifiers can be built from image samples.
What you need for this book
This cookbook is based on the C++ API of the OpenCV library. It is therefore 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 wants to be introduced to the concepts of computer vision programming. It can be used as a companion book in university-level computer vision courses. It constitutes an excellent reference for graduate students and researchers in image processing and computer vision.
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, we 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 the reader more knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: We can include other contexts through the use of the include directive.
A block of code is set as follows:
// Compute Laplacian using LaplacianZC class
LaplacianZC laplacian;
laplacian.setAperture(7); // 7x7 laplacian
cv::Mat flap= laplacian.computeLaplacian(image);
laplace= laplacian.getLaplacianImage();
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
// Compute Laplacian using LaplacianZC class
LaplacianZC laplacian;
laplacian.setAperture(7); // 7x7 laplacian
cv::Mat flap= laplacian.computeLaplacian(image);
laplace= laplacian.getLaplacianImage();
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: Clicking the Next button moves you to the next screen.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail [email protected], and mention the book's title in 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 at 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 this book 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.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
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/OpenCV3-Computer-Vision-Application-Programming-Cookbook-Third-Edition . We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
The source code files of the examples presented in this cookbook are also hosted in the author's Github repository. You can visit the author's repository at https://github.com/laganiere to obtain the latest version of the code.
Downloading the color images of this book
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/OpenCV3ComputerVisionApplicationProgrammingCookbookThirdEdition_ColorImages.pdf.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
Chapter 1. Playing with Images
In this chapter, we will get you started with the OpenCV library. You will learn how to perform the following tasks:
Installing the OpenCV library
Loading, displaying, and saving images
Exploring the cv::Mat data structure
Defining regions of interest
Introduction
This chapter will teach you the basic elements of OpenCV and will show you how to accomplish the most fundamental image processing tasks: reading, displaying, and saving images. However, before you start with OpenCV, you need to install the library. This is a simple process that is explained in the first recipe of this chapter.
All your computer vision applications will involve the processing of images. This is why OpenCV offers you a data structure to handle images and matrices. It is a powerful data structure with many useful attributes and methods. It also incorporates an advanced memory management model that greatly facilitates the development of applications. The last two recipes of this chapter will teach you how to use this important data structure of OpenCV.
Installing the OpenCV library
OpenCV is an open source library for developing computer vision applications that can run on multiple platforms, such as Windows, Linux, Mac, Android, and iOS. It can be used in both academic and commercial applications under a BSD license that allows you to freely use, distribute, and adapt it. This recipe will show you how to install the library on your machine.
Getting ready
When you visit the OpenCV official website at http://opencv.org/ , you will find the latest release of the library, the online documentation describing the Application Programming Interface (API), and many other useful resources on OpenCV.
How to do it...
From the OpenCV website, find the latest available downloads and select the one that corresponds to the platform of your choice (Windows, Linux/Mac, or iOS). Once the OpenCV package is downloaded, run the WinZip self-extractor and select the location of your choice. An opencv directory will be created; it is a good idea to rename it in a way that will show which version you are using (for example, in Windows, your final directory could be C:\opencv-3.2). This directory will contain a collection of files and directories that constitute the library. Notably, you will find the sources directory that will contain all the source files (yes, it is open source!).
In order to complete the installation of the library and have it ready for use, you need to take an important step: generate the binary files of the library for the environment of your choice. This is indeed the point where you have to make a decision on the target platform you wish to use to create your OpenCV applications. Which operating system do you prefer to use? Which compiler should you select? Which version? 32-bit or 64-bit? As you can see, there are many possible options, and this is why you have to build the library that fits your needs.
The Integrated Development Environment (IDE) you will use in your project development will also guide you to make these choices. Note that the library package also comes with precompiled binaries that you can directly use if they correspond to your situation (check the build directory adjacent to the sources directory). If one of the precompiled binaries satisfies your requirements, then you are ready to go.
One important remark, however. Since version 3, OpenCV has been split into two major components. The first one is the main OpenCV source repository that includes the mature algorithms. This is the one you have downloaded. A separate contribution repository also exists, and it contains the new computer vision algorithm, recently added by the OpenCV contributors. If your plan is to use only the core functions of OpenCV, you do not need the contrib package. But if you want to play with the latest state-of-the-art algorithms, then there is a good chance that you will need this extra module. As a matter of fact, this cookbook will show you how to use several of these advanced algorithms. You therefore need the contrib modules to follow the recipes of this book. So you have to go to https://github.com/opencv/opencv_contrib and download OpenCV's extra modules (download the ZIP file). You can unzip the extra modules into the directory of your choice; these modules should be found at opencv_contrib-master/modules. For simplicity, you can rename this directory as contrib and copy it directly inside the sources directory of the main package. Note that you can also pick the extra modules of your choice and only save them; however, you will probably find it easier, at this point, to simply keep everything.
You are now ready to proceed with the installation. To build the OpenCV binaries, it is highly suggested that you use the CMake tool, available at http://cmake.org . CMake is another open source software tool designed to control the compilation process of a software system using platform-independent configuration files. It generates the required makefile or solution files needed for compiling a software library in your environment. Therefore, you have to download and install CMake. Also see the There's more... section of this recipe for an additional software package, the Visualization Toolkit (VTK), that you may want to install before compiling the library.
You can run cmake using a command-line interface, but it is easier to use CMake with its graphical interface (cmake-gui). In the latter case, all you need to do is specify the folder containing the OpenCV library source and the one that will contain the binaries. Now click on Configure and select the compiler of your choice:
How to do it...Once this initial configuration is completed, CMake will provide you with a number of configuration options. You have to decide, for example, whether you want to have the documentation installed or whether you wish to have some additional libraries installed. Unless you know what you are doing, it is probably better to leave the default options as they are. However, since we want to include the extra modules, we have to specify the directory where they can be found:
How to do it...Once the extra module path is specified, click on Configure again. You are now ready to generate the project files by clicking on the Generate button. These files will allow you to compile the library. This is the last step of the installation process, which will make the library ready to be used in your development environment. For example, if you select MS Visual Studio, then all you need to do is open the top-level solution file that CMake has created for you (the OpenCV.sln file). You then select the INSTALL project (under CMakeTargets) and issue the Build command (use right-click).
How to do it...To get both a Release and Debug build, you will have to repeat the compilation process twice, one for each configuration. If everything goes well, you will have an install directory (under build) created. This directory will contain all the binary files of the OpenCV library to be linked with your application as well as the dynamic library files that your executables have to call at runtime. Make sure you set your system's PATH environment variable (from Control Panel) such that your operating system would be able to find the .dll files when you run your applications (for example, C:\opencv-3.2\build \install\x64\vc14\bin). You should also define the environment variable, OPENCV_DIR pointing to the INSTALL directory. This way, CMake will be able to find the library when configuring future projects.
In Linux environments, you can use Cmake to generate the required Makefiles; you then complete the installation by executing a sudo make install command. Alternatively, you could also use the packaging tool apt-get which can automatically perform a complete installation of the library. For Mac OS, you should use the Homebrew package manager. Once installed, you just have to type brew install opencv3 --with-contrib in order to have the complete library installed (run brew info opencv3 to view all possible options).
How it works...
OpenCV is a library that is in constant evolution. With version 3, the library continues to expand offering a lot of new functionalities with enhanced performances. The move to having a full C++ API, which was initiated in version 2, is now almost complete, and more uniform interfaces have been implemented. One of the major changes introduced in this new version is the restructuring of the modules of the library in order to facilitate its distribution. In particular, a separate repository containing the most recent algorithms has been created. This contrib repository also contains non-free algorithms that are subject to specific licenses. The idea is for OpenCV to be able to offer state-of-the-art functionalities that developers and researchers want to share while still being able to offer a very stable and well-maintained core API. The main modules are therefore the ones you get when you download the library at http://opencv.org/. The extra modules must be downloaded directly from the development repository hosted on GitHub ( https://github.com/opencv/ ). Since these extra modules are in constant development, you should expect more frequent changes to the algorithms they contain.
The OpenCV library is divided into several modules. For example, the opencv_core module contains the core functionalities of the library; the opencv_imgproc module includes the main image processing functions; the opencv_highgui module offers the image and video reading and writing functions along with some user interface functions; and so on. To use a particular module, you have to include the corresponding top-level header file. For instance, most applications that use OpenCV start with the following declarations:
#include
#include
#include
As you learn to work with OpenCV, you will discover more and more functionalities available in its numerous modules.
There's more...
The OpenCV website at http://opencv.org/ contains detailed instructions on