The OpenCV Tutorials
Release 2.4.9.0
April 21, 2014
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.5 How to build applications with OpenCV inside the Microsoft Visual Studio . . . . . . . . . . . . . . 28
1.6 Image Watch: viewing in-memory images in the Visual Studio debugger . . . . . . . . . . . . . . . 34
1.7 Introduction to Java Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
1.8 Using OpenCV Java with Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
1.9 Introduction to OpenCV Development with Clojure . . . . . . . . . . . . . . . . . . . . . . . . . . 57
1.10 Introduction into Android Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
1.11 OpenCV4Android SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
1.12 Android Development with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
1.13 Installation in iOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
1.14 Cross compilation for ARM based Linux systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
1.15 Load and Display an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
1.16 Load, Modify, and Save an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
1.17 How to write a tutorial for OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
2 core module. The Core Functionality 135
2.1 Mat - The Basic Image Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
2.2 How to scan images, lookup tables and time measurement with OpenCV . . . . . . . . . . . . . . . 143
2.3 Mask operations on matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
2.4 Adding (blending) two images using OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
2.5 Changing the contrast and brightness of an image! . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
2.6 Basic Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
2.7 Random generator and text with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
2.8 Discrete Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
2.9 File Input and Output using XML and YAML files . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
2.10 Interoperability with OpenCV 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
3 imgproc module. Image Processing 181
3.1 Smoothing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
3.2 Eroding and Dilating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
3.3 More Morphology Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
3.4 Image Pyramids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
3.5 Basic Thresholding Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
3.6 Making your own linear filters! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
3.7 Adding borders to your images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
i
3.8 Sobel Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
3.9 Laplace Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
3.10 Canny Edge Detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
3.11 Hough Line Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
3.12 Hough Circle Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
3.13 Remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
3.14 Affine Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
3.15 Histogram Equalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
3.16 Histogram Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
3.17 Histogram Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
3.18 Back Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
3.19 Template Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
3.20 Finding contours in your image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
3.21 Convex Hull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
3.22 Creating Bounding boxes and circles for contours . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
3.23 Creating Bounding rotated boxes and ellipses for contours . . . . . . . . . . . . . . . . . . . . . . . 300
3.24 Image Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
3.25 Point Polygon Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
4 highgui module. High Level GUI and Media 307
4.1 Adding a Trackbar to our applications! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
4.2 Video Input with OpenCV and similarity measurement . . . . . . . . . . . . . . . . . . . . . . . . . 311
4.3 Creating a video with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
5 calib3d module. Camera calibration and 3D reconstruction 325
5.1 Camera calibration with square chessboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
5.2 Camera calibration With OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
6 feature2d module. 2D Features framework 337
6.1 Feature Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
6.2 Harris corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
6.3 Feature Matching with FLANN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
6.4 Features2D + Homography to find a known object . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
6.5 Shi-Tomasi corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
6.6 Creating yor own corner detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
6.7 Detecting corners location in subpixeles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
6.8 Feature Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
6.9 Feature Matching with FLANN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
6.10 Features2D + Homography to find a known object . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
6.11 Detection of planar objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
7 video module. Video analysis 371
8 objdetect module. Object Detection 373
8.1 Cascade Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
9 ml module. Machine Learning 379
9.1 Introduction to Support Vector Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
9.2 Support Vector Machines for Non-Linearly Separable Data . . . . . . . . . . . . . . . . . . . . . . . 385
10 gpu module. GPU-Accelerated Computer Vision 393
10.1 Similarity check (PNSR and SSIM) on the GPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
11 contrib module. The additional contributions made available ! 403
11.1 Discovering the human retina and its use for image processing . . . . . . . . . . . . . . . . . . . . . 404
ii
12 OpenCV iOS 417
12.1 OpenCV iOS Hello . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
12.2 OpenCV iOS - Image Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
12.3 OpenCV iOS - Video Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
13 OpenCV Viz 431
13.1 Launching Viz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
13.2 Pose of a widget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
13.3 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
13.4 Creating Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
14 General tutorials 445
iii
- 1
- 2
- 3
- 4
- 5
- 6
前往页