logo资料库

Learning OpenCV Computer Vision with the OpenCV Library.pdf

第1页 / 共576页
第2页 / 共576页
第3页 / 共576页
第4页 / 共576页
第5页 / 共576页
第6页 / 共576页
第7页 / 共576页
第8页 / 共576页
资料共576页,剩余部分请下载后查看
Contents
Preface
Purpose
Who This Book Is For
What This Book Is Not
About the Programs in This Book
Prerequisites
How This Book Is Best Used
Conventions Used in This Book
Using Code Examples
Safari® Books Online
We’d Like to Hear from You
Acknowledgments
Thanks for Help on OpenCV
Thanks for Help on the Book
Gary Adds . . .
Adrian Adds . . .
CHAPTER 1: Overview
What Is OpenCV?
Who Uses OpenCV?
What Is Computer Vision?
The Origin of OpenCV
Speeding Up OpenCV with IPP
Who Owns OpenCV?
Downloading and Installing OpenCV
Install
Windows
Linux
MacOS X
Getting the Latest OpenCV via CVS
More OpenCV Documentation
Documentation Available in HTML
Documentation via the Wiki
OpenCV Structure and Content
Portability
Exercises
CHAPTER 2: Introduction to OpenCV
Getting Started
First Program—Display a Picture
Second Program—AVI Video
Moving Around
A Simple Transformation
A Not-So-Simple Transformation
Input from a Camera
Writing to an AVI File
Onward
Exercises
CHAPTER 3: Getting to Know OpenCV
OpenCV Primitive Data Types
Matrix and Image Types
CvMat Matrix Structure
Accessing Data in Your Matrix
The easy way
The hard way
The right way
Arrays of Points
IplImage Data Structure
Accessing Image Data
More on ROI and widthStep
Matrix and Image Operators
cvAbs, cvAbsDiff, and cvAbsDiffS
cvAdd, cvAddS, cvAddWeighted, and alpha blending
cvAnd and cvAndS
cvAvg
cvAvgSdv
cvCalcCovarMatrix
cvCmp and cvCmpS
cvConvertScale
cvConvertScaleAbs
cvCopy
cvCountNonZero
cvCrossProduct
cvCvtColor
cvDet
cvDiv
cvDotProduct
cvEigenVV
cvFlip
cvGEMM
cvGetCol and cvGetCols
cvGetDiag
cvGetDims and cvGetDimSize
cvGetRow and cvGetRows
cvGetSize
cvGetSubRect
cvInRange and cvInRangeS
cvInvert
cvMahalonobis
cvMax and cvMaxS
cvMerge
cvMin and cvMinS
cvMinMaxLoc
cvMul
cvNot
cvNorm
cvNormalize
cvOr and cvOrS
cvReduce
cvRepeat
cvScale
cvSet and cvSetZero
cvSetIdentity
cvSolve
cvSplit
cvSub
cvSub, cvSubS, and cvSubRS
cvSum
cvSVD
cvSVBkSb
cvTrace
cvTranspose and cvT
cvXor and cvXorS
cvZero
Drawing Things
Lines
Circles and Ellipses
Polygons
Fonts and Text
Data Persistence
Integrated Performance Primitives
Verifying Installation
Summary
Exercises
CHAPTER 4: HighGUI
A Portable Graphics Toolkit
Creating a Window
Loading an Image
Displaying Images
WaitKey
Mouse Events
Sliders, Trackbars, and Switches
No Buttons
Working with Video
Reading Video
Writing Video
ConvertImage
Exercises
CHAPTER 5: Image Processing
Overview
Smoothing
Image Morphology
Dilation and Erosion
Making Your Own Kernel
More General Morphology
Opening and closing
Morphological gradient
Top Hat and Black Hat
Flood Fill
Resize
Image Pyramids
Threshold
Adaptive Threshold
Exercises
CHAPTER 6: Image Transforms
Overview
Convolution
Convolution Boundaries
Gradients and Sobel Derivatives
Scharr Filter
Laplace
Canny
Hough Transforms
Hough Line Transform
Hough Circle Transform
Remap
Stretch, Shrink, Warp, and Rotate
Affine Transform
Dense affine transformations
cVWarpAffine performance
Computing the affine map matrix
Sparse affine transformations
Perspective Transform
Dense perspective transform
Computing the perspective map matrix
Sparse perspective transformations
CartToPolar and PolarToCart
LogPolar
Discrete Fourier Transform (DFT)
Spectrum Multiplication
Convolution and DFT
Discrete Cosine Transform (DCT)
Integral Images
Distance Transform
Histogram Equalization
Exercises
CHAPTER 7: Histograms and Matching
Basic Histogram Data Structure
Accessing Histograms
Basic Manipulations with Histograms
Comparing Two Histograms
Correlation (method = CV_COMP_CORREL)
Chi-square (method = CV_COMP_CHISQR)
Intersection (method = CV_COMP_INTERSECT)
Bhattacharyya distance (method = CV_COMP_BHATTACHARYYA)
Histogram Usage Examples
Some More Complicated Stuff
Earth Mover’s Distance
Back Projection
Patch-based back projection
Template Matching
Square difference matching method (method = CV_TM_SQDIFF)
Correlation matching methods (method = CV_TM_CCORR)
Correlation coefficient matching methods (method = CV_TM_CCOEFF)
Normalized methods
Exercises
CHAPTER 8: Contours
Memory Storage
Sequences
Creating a Sequence
Deleting a Sequence
Direct Access to Sequence Elements
Slices, Copying, and Moving Data
Using a Sequence As a Stack
Inserting and Removing Elements
Sequence Block Size
Sequence Readers and Sequence Writers
Sequences and Arrays
Contour Finding
Contours Are Sequences
Freeman Chain Codes
Drawing Contours
A Contour Example
Another Contour Example
More to Do with Contours
Polygon Approximations
Summary Characteristics
Length
Bounding boxes
Enclosing circles and ellipses
Geometry
Matching Contours
Moments
More About Moments
Matching with Hu Moments
Hierarchical Matching
Contour Convexity and Convexity Defects
Pairwise Geometrical Histograms
Exercises
CHAPTER 9: Image Parts and Segmentation
Parts and Segments
Background Subtraction
Weaknesses of Background Subtraction
Scene Modeling
A Slice of Pixels
Frame Differencing
Averaging Background Method
Accumulating means, variances, and covariances
Advanced Background Method
Structures
Learning the background
Learning with moving foreground objects
Background differencing: Finding foreground objects
Using the codebook background model
A few more thoughts on codebook models
Connected Components for Foreground Cleanup
A quick test
Comparing Background Methods
Watershed Algorithm
Image Repair by Inpainting
Mean-Shift Segmentation
Delaunay Triangulation, Voronoi Tesselation
Creating a Delaunay or Voronoi Subdivision
Navigating Delaunay Subdivisions
Walking on edges
Points from edges
Method 1: Use an external point to locate an edge or vertex
Method 2: Step through a sequence of points or edges
Identifying the bounding triangle or edges on the convex hull and walking the hull
Usage Examples
Exercises
CHAPTER 10: Tracking and Motion
The Basics of Tracking
Corner Finding
Subpixel Corners
Invariant Features
Optical Flow
Lucas-Kanade Method
How Lucas-Kanade works
Lucas-Kanade code
Pyramid Lucas-Kanade code
Dense Tracking Techniques
Horn-Schunck method
Block matching method
Mean-Shift and Camshift Tracking
Mean-Shift
Camshift
Motion Templates
Estimators
The Kalman Filter
Some Kalman math
Systems with dynamics
Kalman equations
OpenCV and the Kalman filter
Kalman filter example code
A Brief Note on the Extended Kalman Filter
The Condensation Algorithm
Exercises
CHAPTER 11: Camera Models and Calibration
Camera Model
Basic Projective Geometry
Lens Distortions
Calibration
Rotation Matrix and Translation Vector
Chessboards
Subpixel corners
Drawing chessboard corners
Homography
Camera Calibration
How many chess corners for how many parameters?
What’s under the hood?
Calibration function
Computing extrinsics only
Undistortion
Putting Calibration All Together
Rodrigues Transform
Exercises
CHAPTER 12: Projection and 3D Vision
Projections
Affine and Perspective Transformations
Bird’s-Eye View Transform Example
POSIT: 3D Pose Estimation
Stereo Imaging
Triangulation
Epipolar Geometry
The Essential and Fundamental Matrices
Essential matrix math
Fundamental matrix math
How OpenCV handles all of this
Computing Epipolar Lines
Stereo Calibration
Stereo Rectification
Uncalibrated stereo rectification: Hartley’s algorithm
Calibrated stereo rectification: Bouguet’s algorithm
Rectification map
Stereo Correspondence
Stereo Calibration, Rectification, and Correspondence Code
Depth Maps from 3D Reprojection
Structure from Motion
Fitting Lines in Two and Three Dimensions
Exercises
CHAPTER 13: Machine Learning
What Is Machine Learning
Training and Test Set
Supervised and Unsupervised Data
Generative and Discriminative Models
OpenCV ML Algorithms
Using Machine Learning in Vision
Variable Importance
Diagnosing Machine Learning Problems
Cross-validation, bootstrapping, ROC curves, and confusion matrices
Common Routines in the ML Library
Training
Prediction
Controlling Training Iterations
Mahalanobis Distance
K-Means
Problems and Solutions
K-Means Code
Naïve/Normal Bayes Classifier
Naïve/Normal Bayes Code
Binary Decision Trees
Regression Impurity
Classification Impurity
Entropy impurity
Gini impurity
Misclassification impurity
Decision Tree Usage
Training the tree
Decision Tree Results
Boosting
AdaBoost
Boosting Code
Random Trees
Random Tree Code
Using Random Trees
Face Detection or Haar Classifier
Supervised Learning and Boosting Theory
Boosting in the Haar cascade
Viola-Jones Classifier Theory
Works well on . . .
Code for Detecting Faces
Learning New Objects
Other Machine Learning Algorithms
Expectation Maximization
K-Nearest Neighbors
Multilayer Perceptron
Support Vector Machine
Exercises
CHAPTER 14: OpenCV’s Future
Past and Future
Directions
Specific Items
OpenCV for Artists
Afterword
Bibliography
Index
About the Authors
Colophon
Learning OpenCV Gary Bradski and Adrian Kaehler Beijing · Cambridge · Farnham · Köln · Sebastopol · Taipei · Tokyo
Learning OpenCV by Gary Bradski and Adrian Kaehler Copyright © 2008 Gary Bradski and Adrian Kaehler. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Mike Loukides Production Editor: Rachel Monaghan Production Services: Newgen Publishing and Data Services Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: September 2008: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning OpenCV, the image of a giant peacock moth, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. This book uses Repkover,™ a durable and flexible lay-flat binding. ISBN: 978-0-596-51613-0 [M]
Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. 2. Overview What Is OpenCV? Who Uses OpenCV? What Is Computer Vision? The Origin of OpenCV Downloading and Installing OpenCV Getting the Latest OpenCV via CVS More OpenCV Documentation OpenCV Structure and Content Portability Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 2 6 8 10 11 13 14 15 Introduction to OpenCV Getting Started First Program—Display a Picture Second Program—AVI Video Moving Around A Simple Transformation A Not-So-Simple Transformation Input from a Camera Writing to an AVI File Onward Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 16 16 18 19 22 24 26 27 29 29 iii
3. 4. 5. 6. Getting to Know OpenCV OpenCV Primitive Data Types CvMat Matrix Structure IplImage Data Structure Matrix and Image Operators Drawing Things Data Persistence Integrated Performance Primitives Summary Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 31 33 42 47 77 82 86 87 87 HighGUI A Portable Graphics Toolkit Creating a Window Loading an Image Displaying Images Working with Video ConvertImage Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 90 91 92 93 102 106 107 Image Processing Overview Smoothing Image Morphology Flood Fill Resize Image Pyramids Threshold Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 109 109 115 124 129 130 135 141 Image Transforms Overview Convolution Gradients and Sobel Derivatives Laplace Canny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 144 144 148 150 151 iv | Contents
Hough Transforms Remap Stretch, Shrink, Warp, and Rotate CartToPolar and PolarToCart LogPolar Discrete Fourier Transform (DFT) Discrete Cosine Transform (DCT) Integral Images Distance Transform Histogram Equalization Exercises 153 162 163 172 174 177 182 182 185 186 190 7. 8. 9. Histograms and Matching Basic Histogram Data Structure Accessing Histograms Basic Manipulations with Histograms Some More Complicated Stuff Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 195 198 199 206 219 Contours Memory Storage Sequences Contour Finding Another Contour Example More to Do with Contours Matching Contours Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 222 223 234 243 244 251 262 Image Parts and Segmentation Parts and Segments Background Subtraction Watershed Algorithm Image Repair by Inpainting Mean-Shift Segmentation Delaunay Triangulation, Voronoi Tesselation Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 265 265 295 297 298 300 313 Contents | v
10. 11. 12. 13. Tracking and Motion The Basics of Tracking Corner Finding Subpixel Corners Invariant Features Optical Flow Mean-Shift and Camshift Tracking Motion Templates Estimators The Condensation Algorithm Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 316 316 319 321 322 337 341 348 364 367 Camera Models and Calibration Camera Model Calibration Undistortion Putting Calibration All Together Rodrigues Transform Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 371 378 396 397 401 403 Projection and 3D Vision Projections Affine and Perspective Transformations POSIT: 3D Pose Estimation Stereo Imaging Structure from Motion Fitting Lines in Two and Three Dimensions Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 405 407 412 415 453 454 458 Machine Learning What Is Machine Learning Common Routines in the ML Library Mahalanobis Distance K-Means Naïve/Normal Bayes Classifier Binary Decision Trees Boosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 459 471 476 479 483 486 495 vi | Contents
Random Trees Face Detection or Haar Classifier Other Machine Learning Algorithms Exercises 501 506 516 517 14. OpenCV’s Future Past and Future Directions OpenCV for Artists Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 521 522 525 526 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 Contents | vii
分享到:
收藏