The OpenCV Tutorials
Release 2.4.6.0
July 01, 2013
CONTENTS
1 Introduction to OpenCV 5
1.1 Installation in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 Using OpenCV with gcc and CMake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3 Using OpenCV with Eclipse (plugin CDT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Installation in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.5 How to build applications with OpenCV inside the Microsoft Visual Studio . . . . . . . . . . . . . . 27
1.6 Image Watch: viewing in-memory images in the Visual Studio debugger . . . . . . . . . . . . . . . 36
1.7 Introduction to Java Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
1.8 Introduction into Android Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
1.9 OpenCV4Android SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
1.10 Android Development with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
1.11 Installation in iOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
1.12 Cross compilation for ARM based Linux systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
1.13 Load and Display an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
1.14 Load, Modify, and Save an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
1.15 How to write a tutorial for OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
2 core module. The Core Functionality 125
2.1 Mat - The Basic Image Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
2.2 How to scan images, lookup tables and time measurement with OpenCV . . . . . . . . . . . . . . . 133
2.3 Mask operations on matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
2.4 Adding (blending) two images using OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
2.5 Changing the contrast and brightness of an image! . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
2.6 Basic Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
2.7 Random generator and text with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
2.8 Discrete Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
2.9 File Input and Output using XML and YAML files . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
2.10 Interoperability with OpenCV 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
3 imgproc module. Image Processing 171
3.1 Smoothing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
3.2 Eroding and Dilating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
3.3 More Morphology Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
3.4 Image Pyramids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
3.5 Basic Thresholding Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
3.6 Making your own linear filters! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
3.7 Adding borders to your images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
3.8 Sobel Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
3.9 Laplace Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
i
3.10 Canny Edge Detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
3.11 Hough Line Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
3.12 Hough Circle Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
3.13 Remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
3.14 Affine Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
3.15 Histogram Equalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
3.16 Histogram Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
3.17 Histogram Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
3.18 Back Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
3.19 Template Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
3.20 Finding contours in your image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
3.21 Convex Hull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
3.22 Creating Bounding boxes and circles for contours . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
3.23 Creating Bounding rotated boxes and ellipses for contours . . . . . . . . . . . . . . . . . . . . . . . 290
3.24 Image Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
3.25 Point Polygon Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
4 highgui module. High Level GUI and Media 297
4.1 Adding a Trackbar to our applications! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
4.2 Video Input with OpenCV and similarity measurement . . . . . . . . . . . . . . . . . . . . . . . . . 301
4.3 Creating a video with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
5 calib3d module. Camera calibration and 3D reconstruction 315
5.1 Camera calibration with square chessboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
5.2 Camera calibration With OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
6 feature2d module. 2D Features framework 327
6.1 Feature Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
6.2 Harris corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
6.3 Feature Matching with FLANN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
6.4 Features2D + Homography to find a known object . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
6.5 Shi-Tomasi corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
6.6 Creating yor own corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
6.7 Detecting corners location in subpixeles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
6.8 Feature Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
6.9 Feature Matching with FLANN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
6.10 Features2D + Homography to find a known object . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
6.11 Detection of planar objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
7 video module. Video analysis 361
8 objdetect module. Object Detection 363
8.1 Cascade Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
9 ml module. Machine Learning 369
9.1 Introduction to Support Vector Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
9.2 Support Vector Machines for Non-Linearly Separable Data . . . . . . . . . . . . . . . . . . . . . . . 375
10 gpu module. GPU-Accelerated Computer Vision 383
10.1 Similarity check (PNSR and SSIM) on the GPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
11 contrib module. The additional contributions made available ! 393
11.1 Discovering the human retina and its use for image processing . . . . . . . . . . . . . . . . . . . . . 394
12 OpenCV iOS 407
ii
12.1 OpenCV iOS Hello . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
12.2 OpenCV iOS - Image Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
12.3 OpenCV iOS - Video Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
13 General tutorials 421
iii