Opencv 2 Computer Vision Application Programming Cookbook
Total Page:16
File Type:pdf, Size:1020Kb
OpenCV 2 Computer Vision Application Programming Cookbook Over 50 recipes to master this library of programming functions for real-time computer vision Robert Laganière BIRMINGHAM - MUMBAI OpenCV 2 Computer Vision Application Programming Cookbook Copyright © 2011 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 Production Reference: 1180511 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849513-24-1 www.packtpub.com Cover Image by Asher Wishkerman ([email protected]) Credits Author Project Coordinator Robert Laganière Srimoyee Ghoshal Reviewers Proofreader Wajihullah Biaq Joel Johnson Luis Gomez Indexer Vladislav Gubarev Tejal Daruwale Haikel Guemar Xiangjun Shi Graphics Nilesh Mohite Acquisition Editor Usha Iyer Production Coordinator Kruthika Bangera Development Editor Roger D'souza Cover Work Kruthika Bangera Technical Editor Kavita Iyer Copy Editor Neha Shetty About the Author Robert Laganière is a professor at the University of Ottawa, Canada. He received his Ph.D. degree from INRS-Telecommunications in Montreal in 1996. Dr. Laganière is a researcher in computer vision with an interest in video analysis, intelligent visual surveillance, and image- based modeling. He is a co-founding member of the VIVA research lab. He is also a Chief Scientist at iWatchLife.com, a company offering a cloud-based solution for remote monitoring. Dr. Laganière is the co-author of Object-oriented Software Engineering published by McGraw Hill in 2001. 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. I am also grateful to my beloved Marie-Claude, Camille, and Emma for their continuous support. About the Reviewers Wajih Ullah Baig holds a Honors. Degree in Computer Science from Hamdard University, Karachi. He works mostly with with desktop applications and has good experience working with large-scale distributed systems. He has interest in DSP, image processing, pattern recognition, and network programming. He has worked on a large-scale content-based video retrieval project which is one of its kind. Currently, he is working with the Center for Advanced Research in Engineering, Islamabad, Pakistan where he holds a position as a design engineer. As a freelancer, he contributes work for open source projects and posts codes of his own. I would like to thank all my friends and family for their support whilst reviewing the book. Especially my roommate, Dara Baig! Lluís Gómez i Bigordà holds a Masters degree in Computer Science from the Universitat Oberta de Catalunya. I would like to thank all my family and friends for their support while I was reviewing this book, especially to Antonia, my daughter. I owe you one! Vladislav Gubarev was born in 1987 in Baku, USSR. He graduated from Southern Federal University (Russia) with honors. He has a Bachelor (2007) and a Specialist (2008) diplomas of applied mathematics and computer science. He started his career as a researcher and engineer in "Laboratory of Mathematical Methods of Artificial Intelligence". He later became a co-founder of CVisionLab company which provides computer vision solutions. His areas of research interests are mostly related to image and video processing. In addition to researcher skills, he has wide experience in software development. Many thanks to my wife Agatha. She is an applied mathematician, software developer, and the person who helped me a lot in reviewing this book. Also, thanks to my colleagues—a team of great researchers and professional developers. Haïkel Guémar has been a free software enthusiast and a Fedora developer for a few years now. He currently works as a senior software engineer in a startup in Lyon (France): SysFera. Turning coffee into code, QA process, and technical coaching are part of his daily occupation. SysFera's main product is SysFera-DS, the commercial version of the award-winning open source grid computing middleware DIET. DIET is an innovative grid middleware that offers seamless, robust, and high-performance access to heterogeneous computing resources. Besides being a code monkey, Haïkel enjoys practicing kendo and watching chambara movies. Xiangjun Shi received the M.E. degree in Computer Graphics from Zhengjian University, China in 1989, M.S. degree in Statistics, and Ph.D. degree in Computer Science from Utah State University in 2006. From 1989 to 1998, he was an Assistant Professor in Hangzhou University (1989–1995) and Shantou University (1995–1998). From 1998 to 2000, he was an Associate Professor in Shantou University. Since 2007, he has worked on the design and development of Intelligent Video Surveillance System. His research interests include: Data Mining, Data Cleansing, Statistical Inference/Analysis, Digital Video Mining, Computer Vision, Pattern Recognition and Image Processing, Mathematical modeling, and Algorithm Design and Optimization. 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? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f 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. Table of Contents Preface 1 Chapter 1: Playing with Images 7 Introduction 7 Installing the OpenCV library 8 Creating an OpenCV project with MS Visual C++ 11 Creating an OpenCV project with Qt 19 Loading, displaying, and saving images 26 Creating a GUI application using Qt 30 Chapter 2: Manipulating the Pixels 37 Introduction 37 Accessing pixel values 38 Scanning an image with pointers 41 Scanning an image with iterators 49 Writing efficient image scanning loops 51 Scanning an image with neighbor access 55 Performing simple image arithmetic 59 Defining regions of interest 63 Chapter 3: Processing Images with Classes 69 Introduction 69 Using the Strategy pattern in algorithm design 70 Using a Controller to communicate with processing modules 76 Using the Singleton design pattern 80 Using the Model-View-Controller architecture to design an application 82 Converting color spaces 85 Chapter 4: Counting the Pixels with Histograms 89 Introduction 89 Computing the image histogram 89 Table of Contents Applying look-up tables to modify image appearance 96 Equalizing the image histogram 101 Backprojecting a histogram to detect specific image content 103 Using the mean shift algorithm to find an object 108 Retrieving similar images using histogram comparison 112 Chapter 5: Transforming Images with Morphological Operations 117 Introduction 117 Eroding and dilating images using morphological filters 118 Opening and closing images using morphological filters 122 Detecting edges and corners using morphological filters 125 Segmenting images using watersheds 131 Extracting foreground objects with the GrabCut algorithm 137 Chapter 6: Filtering the Images 141 Introduction 141 Filtering images using low-pass filters 142 Filtering images using a median filter 147 Applying directional filters to detect edges 148 Computing the Laplacian of an image 156 Chapter 7: Extracting Lines, Contours, and Components 163 Introduction 163 Detecting image contours with the Canny operator 164 Detecting lines in images with the Hough transform 167 Fitting a line to a set of points 178 Extracting the components' contours 182 Computing components' shape descriptors 186 Chapter 8: Detecting and Matching Interest Points 191 Introduction 191 Detecting Harris corners 192 Detecting FAST features 203 Detecting the scale-invariant SURF features 206 Describing SURF features 212 Chapter 9: Estimating Projective Relations in Images 217 Introduction 217 Calibrating a camera 219 Computing the fundamental matrix