The OpenCV Tutorials
Release 2.4.3
October 24, 2012
CONTENTS
1 Introduction to OpenCV 5
1.1 Installation in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2 Using OpenCV with gcc and CMake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3 Using OpenCV with Eclipse (plugin CDT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4 Installation in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.5 How to build applications with OpenCV inside the Microsoft Visual Studio . . . . . . . . . . . . . . 26
1.6 Introduction into Android Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.7 OpenCV4Android SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
1.8 Android development with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
1.9 Installation in iOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
1.10 Load and Display an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
1.11 Load, Modify, and Save an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
1.12 How to write a tutorial for OpenCV? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
2 core module. The Core Functionality 109
2.1 Mat - The Basic Image Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
2.2 How to scan images, lookup tables and time measurement with OpenCV . . . . . . . . . . . . . . . 117
2.3 Mask operations on matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
2.4 Adding (blending) two images using OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
2.5 Changing the contrast and brightness of an image! . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
2.6 Basic Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
2.7 Random generator and text with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
2.8 Discrete Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
2.9 File Input and Output using XML and YAML files . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
2.10 Interoperability with OpenCV 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
3 imgproc module. Image Processing 155
3.1 Smoothing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
3.2 Eroding and Dilating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
3.3 More Morphology Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
3.4 Image Pyramids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
3.5 Basic Thresholding Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
3.6 Making your own linear filters! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
3.7 Adding borders to your images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
3.8 Sobel Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
3.9 Laplace Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
3.10 Canny Edge Detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
3.11 Hough Line Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
3.12 Hough Circle Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
i
3.13 Remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
3.14 Affine Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
3.15 Histogram Equalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
3.16 Histogram Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
3.17 Histogram Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
3.18 Back Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
3.19 Template Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
3.20 Finding contours in your image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
3.21 Convex Hull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
3.22 Creating Bounding boxes and circles for contours . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
3.23 Creating Bounding rotated boxes and ellipses for contours . . . . . . . . . . . . . . . . . . . . . . . 274
3.24 Image Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
3.25 Point Polygon Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
4 highgui module. High Level GUI and Media 281
4.1 Adding a Trackbar to our applications! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
4.2 Video Input with OpenCV and similarity measurement . . . . . . . . . . . . . . . . . . . . . . . . . 285
4.3 Creating a video with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
5 calib3d module. Camera calibration and 3D reconstruction 297
5.1 Camera calibration with square chessboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
5.2 Camera calibration With OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
6 feature2d module. 2D Features framework 309
6.1 Feature Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
6.2 Harris corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
6.3 Feature Matching with FLANN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
6.4 Features2D + Homography to find a known object . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
6.5 Shi-Tomasi corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
6.6 Creating yor own corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
6.7 Detecting corners location in subpixeles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
6.8 Feature Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
6.9 Feature Matching with FLANN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
6.10 Features2D + Homography to find a known object . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
6.11 Detection of planar objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
7 video module. Video analysis 343
8 objdetect module. Object Detection 345
8.1 Cascade Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
9 ml module. Machine Learning 351
9.1 Introduction to Support Vector Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
9.2 Support Vector Machines for Non-Linearly Separable Data . . . . . . . . . . . . . . . . . . . . . . . 357
10 gpu module. GPU-Accelerated Computer Vision 363
10.1 Similarity check (PNSR and SSIM) on the GPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
11 OpenCV iOS 373
11.1 OpenCV iOS Hello . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
11.2 OpenCV iOS - Image Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
11.3 OpenCV iOS - Video Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
12 General tutorials 387
ii
The OpenCV Tutorials, Release 2.4.3
The following links describe a set of basic OpenCV tutorials. All the source code mentioned here is provide as part
of the OpenCV regular releases, so check before you start copy & pasting the code. The list of tutorials below is
automatically generated from reST files located in our GIT repository.
As always, we would be happy to hear your comments and receive your contributions on any tutorial.
• Introduction to OpenCV
You will learn how to setup OpenCV on your computer!
• core module. The Core Functionality
Here you will learn the about the basic building blocks of the library. A
must read and know for understanding how to manipulate the images on a
pixel level.
• imgproc module. Image Processing
In this section you will learn about the image processing (manipulation)
functions inside OpenCV.
• highgui module. High Level GUI and Media
This section contains valuable tutorials about how to read/save your im-
age/video files and how to use the built-in graphical user interface of the
library.
• calib3d module. Camera calibration and 3D reconstruction
Although we got most of our images in a 2D format they do come from a 3D
world. Here you will learn how to find out from the 2D images information
about the 3D world.
• feature2d module. 2D Features framework
CONTENTS 1