O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning OpenCV 3

Book Description

Get started in the rapidly expanding field of computer vision with this practical guide. Written by Adrian Kaehler and Gary Bradski, creator of the open source OpenCV library, this book provides a thorough introduction for developers, academics, roboticists, and hobbyists. You’ll learn what it takes to build applications that enable computers to "see" and make decisions based on that data. This book gives you a firm grounding in computer vision and OpenCV for building simple or sophisticated vision applications.

Table of Contents

  1. Preface
    1. Purpose of This Book
      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. O’Reilly Safari
    8. We’d Like to Hear from You
    9. Acknowledgments
      1. Thanks for Help on OpenCV
      2. Thanks for Help on This Book
      3. Adrian Adds...
      4. Gary Adds...
  2. 1. Overview
    1. What Is OpenCV?
    2. Who Uses OpenCV?
    3. What Is Computer Vision?
    4. The Origin of OpenCV
      1. OpenCV Block Diagram
      2. Speeding Up OpenCV with IPP
      3. Who Owns OpenCV?
    5. Downloading and Installing OpenCV
      1. Installation
    6. Getting the Latest OpenCV via Git
    7. More OpenCV Documentation
      1. Supplied Documentation
      2. Online Documentation and the Wiki
    8. OpenCV Contribution Repository
      1. Downloading and Building Contributed Modules
    9. Portability
    10. Summary
    11. Exercises
  3. 2. Introduction to OpenCV
    1. Include Files
      1. Resources
    2. First Program—Display a Picture
    3. Second Program—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. Summary
    10. Exercises
  4. 3. Getting to Know OpenCV Data Types
    1. The Basics
    2. OpenCV Data Types
      1. Overview of the Basic Types
      2. Basic Types: Getting Down to Details
      3. Helper Objects
      4. Utility Functions
      5. The Template Structures
    3. Summary
    4. Exercises
  5. 4. Images and Large Array Types
    1. Dynamic and Variable Storage
      1. The cv::Mat Class: N-Dimensional Dense Arrays
      2. Creating an Array
      3. Accessing Array Elements Individually
      4. The N-ary Array Iterator: NAryMatIterator
      5. Accessing Array Elements by Block
      6. Matrix Expressions: Algebra and cv::Mat
      7. Saturation Casting
      8. More Things an Array Can Do
      9. The cv::SparseMat Class: Sparse Arrays
      10. Accessing Sparse Array Elements
      11. Functions Unique to Sparse Arrays
      12. Template Structures for Large Array Types
    2. Summary
    3. Exercises
  6. 5. Array Operations
    1. More Things You Can Do with Arrays
      1. cv::abs()
      2. cv::absdiff()
      3. cv::add()
      4. cv::addWeighted()
      5. cv::bitwise_and()
      6. cv::bitwise_not()
      7. cv::bitwise_or()
      8. cv::bitwise_xor()
      9. cv::calcCovarMatrix()
      10. cv::cartToPolar()
      11. cv::checkRange()
      12. cv::compare()
      13. cv::completeSymm()
      14. cv::convertScaleAbs()
      15. cv::countNonZero()
      16. cv::cvarrToMat()
      17. cv::dct()
      18. cv::dft()
      19. cv::cvtColor()
      20. cv::determinant()
      21. cv::divide()
      22. cv::eigen()
      23. cv::exp()
      24. cv::extractImageCOI()
      25. cv::flip()
      26. cv::gemm()
      27. cv::getConvertElem() and cv::getConvertScaleElem()
      28. cv::idct()
      29. cv::idft()
      30. cv::inRange()
      31. cv::insertImageCOI()
      32. cv::invert()
      33. cv::log()
      34. cv::LUT()
      35. cv::magnitude()
      36. cv::Mahalanobis()
      37. cv::max()
      38. cv::mean()
      39. cv::meanStdDev()
      40. cv::merge()
      41. cv::min()
      42. cv::minMaxIdx()
      43. cv::minMaxLoc()
      44. cv::mixChannels()
      45. cv::mulSpectrums()
      46. cv::multiply()
      47. cv::mulTransposed()
      48. cv::norm()
      49. cv::normalize()
      50. cv::perspectiveTransform()
      51. cv::phase()
      52. cv::polarToCart()
      53. cv::pow()
      54. cv::randu()
      55. cv::randn()
      56. cv::randShuffle()
      57. cv::reduce()
      58. cv::repeat()
      59. cv::scaleAdd()
      60. cv::setIdentity()
      61. cv::solve()
      62. cv::solveCubic()
      63. cv::solvePoly()
      64. cv::sort()
      65. cv::sortIdx()
      66. cv::split()
      67. cv::sqrt()
      68. cv::subtract()
      69. cv::sum()
      70. cv::trace()
      71. cv::transform()
      72. cv::transpose()
    2. Summary
    3. Exercises
  7. 6. Drawing and Annotating
    1. Drawing Things
      1. Line Art and Filled Polygons
      2. Fonts and Text
    2. Summary
    3. Exercises
  8. 7. Functors in OpenCV
    1. Objects That “Do Stuff”
      1. Principal Component Analysis (cv::PCA)
      2. Singular Value Decomposition (cv::SVD)
      3. Random Number Generator (cv::RNG)
    2. Summary
    3. Exercises
  9. 8. Image, Video, and Data Files
    1. HighGUI: Portable Graphics Toolkit
    2. Working with Image Files
      1. Loading and Saving Images
      2. A Note About Codecs
      3. Compression and Decompression
    3. Working with Video
      1. Reading Video with the cv::VideoCapture Object
      2. Writing Video with the cv::VideoWriter Object
    4. Data Persistence
      1. Writing to a cv::FileStorage
      2. Reading from a cv::FileStorage
      3. cv::FileNode
    5. Summary
    6. Exercises
  10. 9. Cross-Platform and Native Windows
    1. Working with Windows
      1. HighGUI Native Graphical User Interface
      2. Working with the Qt Backend
      3. Integrating OpenCV with Full GUI Toolkits
    2. Summary
    3. Exercises
  11. 10. Filters and Convolution
    1. Overview
    2. Before We Begin
      1. Filters, Kernels, and Convolution
      2. Border Extrapolation and Boundary Conditions
    3. Threshold Operations
      1. Otsu’s Algorithm
      2. Adaptive Threshold
    4. Smoothing
      1. Simple Blur and the Box Filter
      2. Median Filter
      3. Gaussian Filter
      4. Bilateral Filter
    5. Derivatives and Gradients
      1. The Sobel Derivative
      2. Scharr Filter
      3. The Laplacian
    6. Image Morphology
      1. Dilation and Erosion
      2. The General Morphology Function
      3. Opening and Closing
      4. Morphological Gradient
      5. Top Hat and Black Hat
      6. Making Your Own Kernel
    7. Convolution with an Arbitrary Linear Filter
      1. Applying a General Filter with cv::filter2D()
      2. Applying a General Separable Filter with cv::sepFilter2D
      3. Kernel Builders
    8. Summary
    9. Exercises
  12. 11. General Image Transforms
    1. Overview
    2. Stretch, Shrink, Warp, and Rotate
      1. Uniform Resize
      2. Image Pyramids
      3. Nonuniform Mappings
      4. Affine Transformation
      5. Perspective Transformation
    3. General Remappings
      1. Polar Mappings
      2. LogPolar
      3. Arbitrary Mappings
    4. Image Repair
      1. Inpainting
      2. Denoising
    5. Histogram Equalization
      1. cv::equalizeHist(): Contrast equalization
    6. Summary
    7. Exercises
  13. 12. Image Analysis
    1. Overview
    2. Discrete Fourier Transform
      1. cv::dft(): The Discrete Fourier Transform
      2. cv::idft(): The Inverse Discrete Fourier Transform
      3. cv::mulSpectrums(): Spectrum Multiplication
      4. Convolution Using Discrete Fourier Transforms
      5. cv::dct(): The Discrete Cosine Transform
      6. cv::idct(): The Inverse Discrete Cosine Transform
    3. Integral Images
      1. cv::integral() for Standard Summation Integral
      2. cv::integral() for Squared Summation Integral
      3. cv::integral() for Tilted Summation Integral
    4. The Canny Edge Detector
      1. cv::Canny()
    5. Hough Transforms
      1. Hough Line Transform
      2. Hough Circle Transform
    6. Distance Transformation
      1. cv::distanceTransform() for Unlabeled Distance Transform
      2. cv::distanceTransform() for Labeled Distance Transform
    7. Segmentation
      1. Flood Fill
      2. Watershed Algorithm
      3. Grabcuts
      4. Mean-Shift Segmentation
    8. Summary
    9. Exercises
  14. 13. Histograms and Templates
    1. Histogram Representation in OpenCV
      1. cv::calcHist(): Creating a Histogram from Data
    2. Basic Manipulations with Histograms
      1. Histogram Normalization
      2. Histogram Threshold
      3. Finding the Most Populated Bin
      4. Comparing Two Histograms
      5. Histogram Usage Examples
    3. Some More Sophisticated Histograms Methods
      1. Earth Mover’s Distance
      2. Back Projection
    4. Template Matching
      1. Square Difference Matching Method (cv::TM_SQDIFF)
      2. Normalized Square Difference Matching Method (cv::TM_SQDIFF_NORMED)
      3. Correlation Matching Methods (cv::TM_CCORR)
      4. Normalized Cross-Correlation Matching Method (cv::TM_CCORR_NORMED)
      5. Correlation Coefficient Matching Methods (cv::TM_CCOEFF)
      6. Normalized Correlation Coefficient Matching Method (cv::TM_CCOEFF_NORMED)
    5. Summary
    6. Exercises
  15. 14. Contours
    1. Contour Finding
      1. Contour Hierarchies
      2. Drawing Contours
      3. A Contour Example
      4. Another Contour Example
      5. Fast Connected Component Analysis
    2. More to Do with Contours
      1. Polygon Approximations
      2. Geometry and Summary Characteristics
      3. Geometrical Tests
    3. Matching Contours and Images
      1. Moments
      2. More About Moments
      3. Matching and Hu Moments
      4. Using Shape Context to Compare Shapes
    4. Summary
    5. Exercises
  16. 15. Background Subtraction
    1. Overview of Background Subtraction
    2. Weaknesses of Background Subtraction
    3. Scene Modeling
      1. A Slice of Pixels
      2. Frame Differencing
    4. Averaging Background Method
      1. Accumulating Means, Variances, and Covariances
    5. A More Advanced Background Subtraction 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
    6. Connected Components for Foreground Cleanup
      1. A Quick Test
    7. Comparing Two Background Methods
    8. OpenCV Background Subtraction Encapsulation
      1. The cv::BackgroundSubtractor Base Class
      2. KaewTraKuPong and Bowden Method
      3. Zivkovic Method
    9. Summary
    10. Exercises
  17. 16. Keypoints and Descriptors
    1. Keypoints and the Basics of Tracking
      1. Corner Finding
      2. Introduction to Optical Flow
      3. Lucas-Kanade Method for Sparse Optical Flow
    2. Generalized Keypoints and Descriptors
      1. Optical Flow, Tracking, and Recognition
      2. How OpenCV Handles Keypoints and Descriptors, the General Case
      3. Core Keypoint Detection Methods
      4. Keypoint Filtering
      5. Matching Methods
      6. Displaying Results
    3. Summary
    4. Exercises
  18. 17. Tracking
    1. Concepts in Tracking
    2. Dense Optical Flow
      1. The Farnebäck Polynomial Expansion Algorithm
      2. The Dual TV-L1 Algorithm
      3. The Simple Flow Algorithm
    3. Mean-Shift and Camshift Tracking
      1. Mean-Shift
      2. Camshift
    4. Motion Templates
    5. Estimators
      1. The Kalman Filter
      2. A Brief Note on the Extended Kalman Filter
    6. Summary
    7. Exercises
  19. 18. Camera Models and Calibration
    1. Camera Model
      1. The Basics of Projective Geometry
      2. Rodrigues Transform
      3. Lens Distortions
    2. Calibration
      1. Rotation Matrix and Translation Vector
      2. Calibration Boards
      3. Homography
      4. Camera Calibration
    3. Undistortion
      1. Undistortion Maps
      2. Converting Undistortion Maps Between Representations with cv::convertMaps()
      3. Computing Undistortion Maps with cv::initUndistortRectifyMap()
      4. Undistorting an Image with cv::remap()
      5. Undistortion with cv::undistort()
      6. Sparse Undistortion with cv::undistortPoints()
    4. Putting Calibration All Together
    5. Summary
    6. Exercises
  20. 19. Projection and Three-Dimensional Vision
    1. Projections
    2. Affine and Perspective Transformations
      1. Bird’s-Eye-View Transform Example
    3. Three-Dimensional Pose Estimation
      1. Pose Estimation from a Single Camera
    4. Stereo Imaging
      1. Triangulation
      2. Epipolar Geometry
      3. The Essential and Fundamental Matrices
      4. Computing Epipolar Lines
      5. Stereo Calibration
      6. Stereo Rectification
      7. Stereo Correspondence
      8. Stereo Calibration, Rectification, and Correspondence Code Example
      9. Depth Maps from Three-Dimensional Reprojection
    5. Structure from Motion
    6. Fitting Lines in Two and Three Dimensions
    7. Summary
    8. Exercises
  21. 20. The Basics of Machine Learning in OpenCV
    1. What Is Machine Learning?
      1. Training and Test Sets
      2. Supervised and Unsupervised Learning
      3. Generative and Discriminative Models
      4. OpenCV ML Algorithms
      5. Using Machine Learning in Vision
      6. Variable Importance
      7. Diagnosing Machine Learning Problems
    2. Legacy Routines in the ML Library
      1. K-Means
      2. Mahalanobis Distance
    3. Summary
    4. Exercises
  22. 21. StatModel: The Standard Model for Learning in OpenCV
    1. Common Routines in the ML Library
      1. Training and the cv::ml::TrainData Structure
      2. Prediction
    2. Machine Learning Algorithms Using cv::StatModel
      1. Naïve/Normal Bayes Classifier
      2. Binary Decision Trees
      3. Boosting
      4. Random Trees
      5. Expectation Maximization
      6. K-Nearest Neighbors
      7. Multilayer Perceptron
      8. Support Vector Machine
    3. Summary
    4. Exercises
  23. 22. Object Detection
    1. Tree-Based Object Detection Techniques
      1. Cascade Classifiers
      2. Supervised Learning and Boosting Theory
      3. Learning New Objects
    2. Object Detection Using Support Vector Machines
      1. Latent SVM for Object Detection
      2. The Bag of Words Algorithm and Semantic Categorization
    3. Summary
    4. Exercises
  24. 23. Future of OpenCV
    1. Past and Present
      1. OpenCV 3.x
    2. How Well Did Our Predictions Go Last Time?
    3. Future Functions
      1. Current GSoC Work
      2. Community Contributions
      3. OpenCV.org
    4. Some AI Speculation
    5. Afterword
  25. A. Planar Subdivisions
    1. Delaunay Triangulation, Voronoi Tesselation
      1. Creating a Delaunay or Voronoi Subdivision
      2. Navigating Delaunay Subdivisions
      3. Usage Examples
    2. Exercises
  26. B. opencv_contrib
    1. An Overview of the opencv_contrib Modules
      1. Contents of opencv_contrib
  27. C. Calibration Patterns
    1. Calibration Patterns Used by OpenCV
  28. Bibliography
  29. Index