"This library is useful for practitioners, and is an excellent tool for those entering the field: it is a set of computer vision algorithms that work as advertised." -William T. Freeman, Computer Science and Artificial Intelligence Laboratory, Massachusetts Institute of Technology Learning OpenCV puts you in the middle of the rapidly expanding field of computer vision. Written by the creators of the free open source OpenCV library, this book introduces you to computer vision and demonstrates how you can quickly build applications that enable computers to "see" and make decisions based on that data. Computer vision is everywhere-in security systems, manufacturing inspection systems, medical image analysis, Unmanned Aerial Vehicles, and more. It stitches Google maps and Google Earth together, checks the pixels on LCD screens, and makes sure the stitches in your shirt are sewn properly. OpenCV provides an easy-to-use computer vision framework and a comprehensive library with more than 500 functions that can run vision code in real time. Learning OpenCV will teach any developer or hobbyist to use the framework quickly with the help of hands-on exercises in each chapter. This book includes: A thorough introduction to OpenCV Getting input from cameras Transforming images Segmenting images and shape matching Pattern recognition, including face detection Tracking and motion in 2 and 3 dimensions 3D reconstruction from stereo vision Machine learning algorithms Getting machines to see is a challenging but entertaining goal. Whether you want to build simple or sophisticated vision applications, Learning OpenCV is the book you need to get started.
Author(s): Gary Bradski, Adrian Kaehler
Edition: 1st
Publisher: O'Reilly Media
Year: 2008
Language: English
Pages: 571
Contents......Page 3
Purpose......Page 8
What This Book Is Not......Page 9
How This Book Is Best Used......Page 10
Conventions Used in This Book......Page 11
We’d Like to Hear from You......Page 12
Thanks for Help on OpenCV......Page 13
Gary Adds . . .......Page 14
Adrian Adds . . .......Page 15
Who Uses OpenCV?......Page 17
What Is Computer Vision?......Page 18
The Origin of OpenCV......Page 22
Speeding Up OpenCV with IPP......Page 23
Install......Page 24
Linux......Page 25
Getting the Latest OpenCV via CVS......Page 26
Documentation Available in HTML......Page 27
Documentation via the Wiki......Page 28
OpenCV Structure and Content......Page 29
Portability......Page 30
Exercises......Page 31
First Program—Display a Picture......Page 32
Second Program—AVI Video......Page 34
Moving Around......Page 35
A Simple Transformation......Page 38
A Not-So-Simple Transformation......Page 40
Input from a Camera......Page 42
Writing to an AVI File......Page 43
Exercises......Page 45
OpenCV Primitive Data Types......Page 47
Matrix and Image Types......Page 48
CvMat Matrix Structure......Page 49
The easy way......Page 52
The hard way......Page 53
The right way......Page 55
Arrays of Points......Page 56
IplImage Data Structure......Page 58
Accessing Image Data......Page 60
More on ROI and widthStep......Page 61
Matrix and Image Operators......Page 63
cvAbs, cvAbsDiff, and cvAbsDiffS......Page 65
cvAdd, cvAddS, cvAddWeighted, and alpha blending......Page 66
cvAnd and cvAndS......Page 68
cvAvgSdv......Page 69
cvCalcCovarMatrix......Page 70
cvCmp and cvCmpS......Page 71
cvConvertScale......Page 72
cvCrossProduct......Page 73
cvCvtColor......Page 74
cvDotProduct......Page 76
cvFlip......Page 77
cvGetCol and cvGetCols......Page 78
cvGetDims and cvGetDimSize......Page 79
cvGetSize......Page 80
cvInvert......Page 81
cvMax and cvMaxS......Page 82
cvMerge......Page 83
cvMul......Page 84
cvNorm......Page 85
cvNormalize......Page 86
cvReduce......Page 87
cvSet and cvSetZero......Page 88
cvSplit......Page 89
cvSum......Page 90
cvSVBkSb......Page 91
cvXor and cvXorS......Page 92
Lines......Page 93
Circles and Ellipses......Page 94
Polygons......Page 95
Fonts and Text......Page 96
Data Persistence......Page 98
Integrated Performance Primitives......Page 102
Exercises......Page 103
A Portable Graphics Toolkit......Page 106
Creating a Window......Page 107
Loading an Image......Page 108
Displaying Images......Page 109
Mouse Events......Page 111
Sliders, Trackbars, and Switches......Page 115
No Buttons......Page 117
Working with Video......Page 118
Reading Video......Page 119
Writing Video......Page 121
ConvertImage......Page 122
Exercises......Page 123
Smoothing......Page 125
Dilation and Erosion......Page 131
Making Your Own Kernel......Page 134
Opening and closing......Page 136
Morphological gradient......Page 137
Top Hat and Black Hat......Page 139
Flood Fill......Page 140
Resize......Page 145
Image Pyramids......Page 146
Threshold......Page 151
Adaptive Threshold......Page 154
Exercises......Page 157
Convolution......Page 160
Convolution Boundaries......Page 162
Gradients and Sobel Derivatives......Page 164
Laplace......Page 166
Canny......Page 167
Hough Line Transform......Page 169
Hough Circle Transform......Page 174
Remap......Page 178
Stretch, Shrink, Warp, and Rotate......Page 179
Dense affine transformations......Page 180
Computing the affine map matrix......Page 182
Perspective Transform......Page 185
Computing the perspective map matrix......Page 186
Sparse perspective transformations......Page 187
CartToPolar and PolarToCart......Page 188
LogPolar......Page 190
Discrete Fourier Transform (DFT)......Page 193
Spectrum Multiplication......Page 195
Convolution and DFT......Page 196
Integral Images......Page 198
Distance Transform......Page 201
Histogram Equalization......Page 202
Exercises......Page 206
CHAPTER 7: Histograms and Matching......Page 209
Basic Histogram Data Structure......Page 211
Accessing Histograms......Page 214
Basic Manipulations with Histograms......Page 215
Correlation (method = CV_COMP_CORREL)......Page 217
Bhattacharyya distance (method = CV_COMP_BHATTACHARYYA)......Page 218
Histogram Usage Examples......Page 219
Some More Complicated Stuff......Page 222
Earth Mover’s Distance......Page 223
Back Projection......Page 225
Patch-based back projection......Page 227
Template Matching......Page 230
Correlation matching methods (method = CV_TM_CCORR)......Page 231
Normalized methods......Page 232
Exercises......Page 235
Memory Storage......Page 238
Sequences......Page 239
Creating a Sequence......Page 240
Direct Access to Sequence Elements......Page 242
Slices, Copying, and Moving Data......Page 243
Using a Sequence As a Stack......Page 245
Sequence Readers and Sequence Writers......Page 247
Sequences and Arrays......Page 249
Contour Finding......Page 250
Contours Are Sequences......Page 254
Freeman Chain Codes......Page 256
Drawing Contours......Page 257
A Contour Example......Page 258
Another Contour Example......Page 259
More to Do with Contours......Page 260
Polygon Approximations......Page 261
Length......Page 263
Bounding boxes......Page 264
Enclosing circles and ellipses......Page 265
Geometry......Page 266
Matching Contours......Page 267
Moments......Page 268
More About Moments......Page 269
Matching with Hu Moments......Page 271
Hierarchical Matching......Page 272
Contour Convexity and Convexity Defects......Page 274
Pairwise Geometrical Histograms......Page 277
Exercises......Page 278
Background Subtraction......Page 281
Weaknesses of Background Subtraction......Page 282
A Slice of Pixels......Page 283
Frame Differencing......Page 286
Averaging Background Method......Page 287
Accumulating means, variances, and covariances......Page 292
Advanced Background Method......Page 294
Learning the background......Page 297
Learning with moving foreground objects......Page 300
Background differencing: Finding foreground objects......Page 301
Connected Components for Foreground Cleanup......Page 303
A quick test......Page 308
Comparing Background Methods......Page 310
Watershed Algorithm......Page 311
Image Repair by Inpainting......Page 313
Mean-Shift Segmentation......Page 314
Delaunay Triangulation, Voronoi Tesselation......Page 316
Creating a Delaunay or Voronoi Subdivision......Page 319
Navigating Delaunay Subdivisions......Page 320
Walking on edges......Page 322
Points from edges......Page 324
Method 2: Step through a sequence of points or edges......Page 325
Usage Examples......Page 326
Exercises......Page 329
Corner Finding......Page 332
Subpixel Corners......Page 335
Invariant Features......Page 337
Optical Flow......Page 338
Lucas-Kanade Method......Page 339
How Lucas-Kanade works......Page 340
Pyramid Lucas-Kanade code......Page 345
Dense Tracking Techniques......Page 350
Horn-Schunck method......Page 351
Block matching method......Page 352
Mean-Shift......Page 353
Motion Templates......Page 357
Estimators......Page 364
The Kalman Filter......Page 366
Some Kalman math......Page 367
Systems with dynamics......Page 369
Kalman equations......Page 371
OpenCV and the Kalman filter......Page 374
Kalman filter example code......Page 375
A Brief Note on the Extended Kalman Filter......Page 379
The Condensation Algorithm......Page 380
Exercises......Page 383
CHAPTER 11: Camera Models and Calibration......Page 386
Camera Model......Page 387
Basic Projective Geometry......Page 389
Lens Distortions......Page 391
Calibration......Page 394
Rotation Matrix and Translation Vector......Page 395
Chessboards......Page 397
Drawing chessboard corners......Page 399
Homography......Page 400
Camera Calibration......Page 403
How many chess corners for how many parameters?......Page 404
What’s under the hood?......Page 405
Calibration function......Page 408
Computing extrinsics only......Page 411
Undistortion......Page 412
Putting Calibration All Together......Page 413
Rodrigues Transform......Page 417
Exercises......Page 419
Projections......Page 421
Affine and Perspective Transformations......Page 423
Bird’s-Eye View Transform Example......Page 424
POSIT: 3D Pose Estimation......Page 428
Triangulation......Page 431
Epipolar Geometry......Page 435
The Essential and Fundamental Matrices......Page 437
Essential matrix math......Page 438
Fundamental matrix math......Page 439
How OpenCV handles all of this......Page 440
Computing Epipolar Lines......Page 442
Stereo Calibration......Page 443
Stereo Rectification......Page 446
Uncalibrated stereo rectification: Hartley’s algorithm......Page 447
Calibrated stereo rectification: Bouguet’s algorithm......Page 449
Rectification map......Page 452
Stereo Correspondence......Page 454
Stereo Calibration, Rectification, and Correspondence Code......Page 461
Depth Maps from 3D Reprojection......Page 468
Structure from Motion......Page 469
Fitting Lines in Two and Three Dimensions......Page 470
Exercises......Page 474
Training and Test Set......Page 475
Supervised and Unsupervised Data......Page 476
Generative and Discriminative Models......Page 477
OpenCV ML Algorithms......Page 478
Using Machine Learning in Vision......Page 479
Variable Importance......Page 481
Diagnosing Machine Learning Problems......Page 482
Cross-validation, bootstrapping, ROC curves, and confusion matrices......Page 485
Common Routines in the ML Library......Page 487
Training......Page 488
Controlling Training Iterations......Page 491
Mahalanobis Distance......Page 492
Problems and Solutions......Page 495
K-Means Code......Page 497
Naïve/Normal Bayes Classifier......Page 499
Naïve/Normal Bayes Code......Page 501
Classification Impurity......Page 502
Decision Tree Usage......Page 503
Training the tree......Page 504
Decision Tree Results......Page 508
Boosting......Page 511
AdaBoost......Page 513
Boosting Code......Page 514
Random Trees......Page 517
Random Tree Code......Page 518
Using Random Trees......Page 521
Face Detection or Haar Classifier......Page 522
Boosting in the Haar cascade......Page 523
Viola-Jones Classifier Theory......Page 524
Works well on . . .......Page 525
Code for Detecting Faces......Page 527
Learning New Objects......Page 529
K-Nearest Neighbors......Page 532
Exercises......Page 533
Past and Future......Page 537
Directions......Page 538
Specific Items......Page 540
OpenCV for Artists......Page 541
Afterword......Page 542
Bibliography......Page 543
Index......Page 558
Colophon......Page 571