You are previewing Learning OpenCV.
O'Reilly logo
Learning OpenCV

Book Description

"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.

Table of Contents

  1. Learning OpenCV
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. Purpose
        1. Who This Book Is For
        2. What This Book Is Not
      2. About the Programs in This Book
      3. Prerequisites
      4. How This Book Is Best Used
      5. Conventions Used in This Book
      6. Using Code Examples
      7. Safari® Books Online
      8. We'd Like to Hear from You
      9. Acknowledgments
        1. Thanks for Help on OpenCV
        2. Thanks for Help on the Book
        3. Gary Adds…
        4. Adrian Adds…
    4. 1. Overview
      1. What Is OpenCV?
      2. Who Uses OpenCV?
      3. What Is Computer Vision?
      4. The Origin of OpenCV
        1. Speeding Up OpenCV with IPP
        2. Who Owns OpenCV?
      5. Downloading and Installing OpenCV
        1. Install
          1. Windows
          2. Linux
          3. MacOS X
      6. Getting the Latest OpenCV via CVS
      7. More OpenCV Documentation
        1. Documentation Available in HTML
        2. Documentation via the Wiki
      8. OpenCV Structure and Content
      9. Portability
      10. Exercises
    5. 2. Introduction to OpenCV
      1. Getting Started
      2. First Program—Display a Picture
      3. Second Program—AVI Video
      4. Moving Around
      5. A Simple Transformation
      6. A Not-So-Simple Transformation
      7. Input from a Camera
      8. Writing to an AVI File
      9. Onward
      10. Exercises
    6. 3. Getting to Know OpenCV
      1. OpenCV Primitive Data Types
        1. Matrix and Image Types
      2. CvMat Matrix Structure
        1. Accessing Data in Your Matrix
          1. The easy way
          2. The hard way
          3. The right way
        2. Arrays of Points
      3. IplImage Data Structure
        1. Accessing Image Data
        2. More on ROI and widthStep
      4. Matrix and Image Operators
        1. Matrix and Image Operators
          1. cvAbs, cvAbsDiff, and cvAbsDiffS
          2. cvAdd, cvAddS, cvAddWeighted, and alpha blending
          3. cvAnd and cvAndS
          4. cvAvg
          5. cvAvgSdv
          6. cvCalcCovarMatrix
          7. cvCmp and cvCmpS
          8. cvConvertScale
          9. cvConvertScaleAbs
          10. cvCopy
          11. cvCountNonZero
          12. cvCrossProduct
          13. cvCvtColor
          14. cvDet
          15. cvDiv
          16. cvDotProduct
          17. cvEigenVV
          18. cvFlip
          19. cvGEMM
          20. cvGetCol and cvGetCols
          21. cvGetDiag
          22. cvGetDims and cvGetDimSize
          23. cvGetRow and cvGetRows
          24. cvGetSize
          25. cvGetSubRect
          26. cvInRange and cvInRangeS
          27. cvInvert
          28. cvMahalanobis
          29. cvMax and cvMaxS
          30. cvMerge
          31. cvMin and cvMinS
          32. cvMinMaxLoc
          33. cvMul
          34. cvNot
          35. cvNorm
          36. cvNormalize
          37. cvOr and cvOrS
          38. cvReduce
          39. cvRepeat
          40. cvScale
          41. cvSet and cvSetZero
          42. cvSetIdentity
          43. cvSolve
          44. cvSplit
          45. cvSub, cvSubS, and cvSubRS
          46. cvSum
          47. cvSVD
          48. cvSVBkSb
          49. cvTrace
          50. cvTranspose and cvT
          51. cvXor and cvXorS
          52. cvZero
      5. Drawing Things
        1. Lines
        2. Circles and Ellipses
        3. Polygons
        4. Fonts and Text
      6. Data Persistence
      7. Integrated Performance Primitives
        1. Verifying Installation
      8. Summary
      9. Exercises
    7. 4. HighGUI
      1. A Portable Graphics Toolkit
      2. Creating a Window
      3. Loading an Image
      4. Displaying Images
        1. WaitKey
        2. Mouse Events
        3. Sliders, Trackbars, and Switches
        4. No Buttons
      5. Working with Video
        1. Reading Video
        2. Writing Video
      6. ConvertImage
      7. Exercises
    8. 5. Image Processing
      1. Overview
      2. Smoothing
      3. Image Morphology
        1. Dilation and Erosion
        2. Making Your Own Kernel
        3. More General Morphology
          1. Opening and closing
          2. Morphological gradient
          3. Top Hat and Black Hat
      4. Flood Fill
      5. Resize
      6. Image Pyramids
      7. Threshold
        1. Adaptive Threshold
      8. Exercises
    9. 6. Image Transforms
      1. Overview
      2. Convolution
        1. Convolution Boundaries
      3. Gradients and Sobel Derivatives
        1. Scharr Filter
      4. Laplace
      5. Canny
      6. Hough Transforms
        1. Hough Line Transform
        2. Hough Circle Transform
      7. Remap
      8. Stretch, Shrink, Warp, and Rotate
        1. Affine Transform
          1. Dense affine transformations
          2. cvWarpAffine performance
          3. Computing the affine map matrix
          4. Sparse affine transformations
        2. Perspective Transform
          1. Dense perspective transform
          2. Computing the perspective map matrix
          3. Sparse perspective transformations
      9. CartToPolar and PolarToCart
      10. LogPolar
      11. Discrete Fourier Transform (DFT)
        1. Spectrum Multiplication
        2. Convolution and DFT
      12. Discrete Cosine Transform (DCT)
      13. Integral Images
      14. Distance Transform
      15. Histogram Equalization
      16. Exercises
    10. 7. Histograms and Matching
      1. Basic Histogram Data Structure
      2. Accessing Histograms
      3. Basic Manipulations with Histograms
        1. Comparing Two Histograms
          1. Correlation (method = CV_COMP_CORREL)
          2. Chi-square (method = CV_COMP_CHISQR)
          3. Intersection (method = CV_COMP_INTERSECT)
          4. Bhattacharyya distance (method = CV_COMP_BHATTACHARYYA)
        2. Histogram Usage Examples
      4. Some More Complicated Stuff
        1. Earth Mover's Distance
        2. Back Projection
          1. Patch-based back projection
        3. Template Matching
          1. Square difference matching method (method = CV_TM_SQDIFF)
          2. Correlation matching methods (method = CV_TM_CCORR)
          3. Correlation coefficient matching methods (method = CV_TM_CCOEFF)
          4. Normalized methods
      5. Exercises
    11. 8. Contours
      1. Memory Storage
      2. Sequences
        1. Creating a Sequence
        2. Deleting a Sequence
        3. Direct Access to Sequence Elements
        4. Slices, Copying, and Moving Data
        5. Using a Sequence As a Stack
        6. Inserting and Removing Elements
        7. Sequence Block Size
        8. Sequence Readers and Sequence Writers
        9. Sequences and Arrays
      3. Contour Finding
        1. Contours Are Sequences
        2. Freeman Chain Codes
        3. Drawing Contours
        4. A Contour Example
      4. Another Contour Example
      5. More to Do with Contours
        1. Polygon Approximations
        2. Summary Characteristics
          1. Length
          2. Bounding boxes
          3. Enclosing circles and ellipses
        3. Geometry
      6. Matching Contours
        1. Moments
        2. More About Moments
        3. Matching with Hu Moments
        4. Hierarchical Matching
        5. Contour Convexity and Convexity Defects
        6. Pairwise Geometrical Histograms
      7. Exercises
    12. 9. Image Parts and Segmentation
      1. Parts and Segments
      2. Background Subtraction
        1. Weaknesses of Background Subtraction
        2. Scene Modeling
        3. A Slice of Pixels
        4. Frame Differencing
        5. Averaging Background Method
        6. Accumulating means, variances, and covariances
        7. Advanced Background Method
          1. Structures
          2. Learning the background
          3. Learning with moving foreground objects
          4. Background differencing: Finding foreground objects
          5. Using the codebook background model
          6. A few more thoughts on codebook models
        8. Connected Components for Foreground Cleanup
          1. A quick test
        9. Comparing Background Methods
      3. Watershed Algorithm
      4. Image Repair by Inpainting
      5. Mean-Shift Segmentation
      6. Delaunay Triangulation, Voronoi Tesselation
        1. Creating a Delaunay or Voronoi Subdivision
        2. Navigating Delaunay Subdivisions
          1. Walking on edges
          2. Points from edges
          3. Method 1: Use an external point to locate an edge or vertex
          4. Method 2: Step through a sequence of points or edges
          5. Identifying the bounding triangle or edges on the convex hull and walking the hull
        3. Usage Examples
      7. Exercises
    13. 10. Tracking and Motion
      1. The Basics of Tracking
      2. Corner Finding
      3. Subpixel Corners
      4. Invariant Features
      5. Optical Flow
        1. Lucas-Kanade Method
          1. How Lucas-Kanade works
          2. Lucas-Kanade code
          3. Pyramid Lucas-Kanade code
        2. Dense Tracking Techniques
          1. Horn-Schunck method
          2. Block matching method
      6. Mean-Shift and Camshift Tracking
        1. Mean-Shift and Camshift Tracking
          1. Mean-Shift
        2. Camshift
      7. Motion Templates
      8. Estimators
        1. The Kalman Filter
          1. Some Kalman math
          2. Systems with dynamics
          3. Kalman equations
          4. OpenCV and the Kalman filter
          5. Kalman filter example code
        2. A Brief Note on the Extended Kalman Filter
      9. The Condensation Algorithm
      10. Exercises
    14. 11. Camera Models and Calibration
      1. Camera Model
        1. Basic Projective Geometry
        2. Lens Distortions
      2. Calibration
        1. Rotation Matrix and Translation Vector
        2. Chessboards
          1. Subpixel corners
          2. Drawing chessboard corners
        3. Homography
        4. Camera Calibration
          1. How many chess corners for how many parameters?
          2. What's under the hood?
          3. Calibration function
          4. Computing extrinsics only
      3. Undistortion
      4. Putting Calibration All Together
      5. Rodrigues Transform
      6. Exercises
    15. 12. Projection and 3D Vision
      1. Projections
      2. Affine and Perspective Transformations
        1. Bird's-Eye View Transform Example
      3. POSIT: 3D Pose Estimation
      4. Stereo Imaging
        1. Triangulation
        2. Epipolar Geometry
        3. The Essential and Fundamental Matrices
          1. Essential matrix math
          2. Fundamental matrix math
          3. How OpenCV handles all of this
        4. Computing Epipolar Lines
        5. Stereo Calibration
        6. Stereo Rectification
          1. Uncalibrated stereo rectification: Hartley's algorithm
          2. Calibrated stereo rectification: Bouguet's algorithm
          3. Rectification map
        7. Stereo Correspondence
        8. Stereo Calibration, Rectification, and Correspondence Code
        9. Depth Maps from 3D Reprojection
      5. Structure from Motion
      6. Fitting Lines in Two and Three Dimensions
      7. Exercises
    16. 13. Machine Learning
      1. What Is Machine Learning
        1. Training and Test Set
        2. Supervised and Unsupervised Data
        3. Generative and Discriminative Models
        4. OpenCV ML Algorithms
        5. Using Machine Learning in Vision
        6. Variable Importance
        7. Diagnosing Machine Learning Problems
          1. Cross-validation, bootstrapping, ROC curves, and confusion matrices
      2. Common Routines in the ML Library
        1. Training
        2. Prediction
        3. Controlling Training Iterations
      3. Mahalanobis Distance
      4. K-Means
        1. Problems and Solutions
        2. K-Means Code
      5. Naïve/Normal Bayes Classifier
        1. Naïve/Normal Bayes Code
      6. Binary Decision Trees
        1. Regression Impurity
        2. Classification Impurity
          1. Entropy impurity
          2. Gini impurity
          3. Misclassification impurity
        3. Decision Tree Usage
          1. Training the tree
        4. Decision Tree Results
      7. Boosting
        1. AdaBoost
        2. Boosting Code
      8. Random Trees
        1. Random Tree Code
        2. Using Random Trees
      9. Face Detection or Haar Classifier
        1. Supervised Learning and Boosting Theory
          1. Boosting in the Haar cascade
        2. Viola-Jones Classifier Theory
          1. Works well on …
        3. Code for Detecting Faces
        4. Learning New Objects
      10. Other Machine Learning Algorithms
        1. Expectation Maximization
        2. K-Nearest Neighbors
        3. Multilayer Perceptron
        4. Support Vector Machine
      11. Exercises
    17. 14. OpenCV's Future
      1. Past and Future
      2. Directions
        1. Specific Items
      3. OpenCV for Artists
      4. Afterword
    18. 15. Bibliography
    19. Index
    20. About the Authors
    21. Colophon
    22. SPECIAL OFFER: Upgrade this ebook with O’Reilly