Book Image

OpenCV Computer Vision Application Programming Cookbook

By : Robert Laganiere
Book Image

OpenCV Computer Vision Application Programming Cookbook

By: Robert Laganiere

Overview of this book

Table of Contents (18 chapters)
OpenCV Computer Vision Application Programming Cookbook Second Edition
About the Author
About the Reviewers


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 ( 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 to stay updated on what the library can do for you. You can also visit the author's website at 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.


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 , 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

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 If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


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 would 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, 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright 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 with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.