logo资料库

opencv2函数使用手册.pdf

第1页 / 共899页
第2页 / 共899页
第3页 / 共899页
第4页 / 共899页
第5页 / 共899页
第6页 / 共899页
第7页 / 共899页
第8页 / 共899页
资料共899页,剩余部分请下载后查看
Introduction
API Concepts
core. The Core Functionality
Basic Structures
Command Line Parser
Basic C Structures and Operations
Dynamic Structures
Operations on Arrays
Drawing Functions
XML/YAML Persistence
XML/YAML Persistence (C API)
Clustering
Utility and System Functions and Macros
OpenGL interoperability
Intel® IPP Asynchronous C/C++ Converters
imgproc. Image Processing
Image Filtering
Geometric Image Transformations
Miscellaneous Image Transformations
Histograms
Structural Analysis and Shape Descriptors
Motion Analysis and Object Tracking
Feature Detection
Object Detection
highgui. High-level GUI and Media I/O
User Interface
Reading and Writing Images and Video
Qt New Functions
video. Video Analysis
Motion Analysis and Object Tracking
calib3d. Camera Calibration and 3D Reconstruction
Camera Calibration and 3D Reconstruction
features2d. 2D Features Framework
Feature Detection and Description
Common Interfaces of Feature Detectors
Common Interfaces of Descriptor Extractors
Common Interfaces of Descriptor Matchers
Common Interfaces of Generic Descriptor Matchers
Drawing Function of Keypoints and Matches
Object Categorization
objdetect. Object Detection
Cascade Classification
Latent SVM
Scene Text Detection
ml. Machine Learning
Statistical Models
Normal Bayes Classifier
K-Nearest Neighbors
Support Vector Machines
Decision Trees
Boosting
Gradient Boosted Trees
Random Trees
Extremely randomized trees
Expectation Maximization
Neural Networks
MLData
flann. Clustering and Search in Multi-Dimensional Spaces
Fast Approximate Nearest Neighbor Search
Clustering
photo. Computational Photography
Inpainting
Denoising
HDR imaging
References
Decolorization
Seamless Cloning
Non-Photorealistic Rendering
stitching. Images stitching
Stitching Pipeline
References
High Level Functionality
Camera
Features Finding and Images Matching
Rotation Estimation
Autocalibration
Images Warping
Seam Estimation
Exposure Compensation
Image Blenders
nonfree. Non-free functionality
Feature Detection and Description
contrib. Contributed/Experimental Stuff
Stereo Correspondence
FaceRecognizer - Face Recognition with OpenCV
OpenFABMAP
legacy. Deprecated stuff
Motion Analysis
Expectation Maximization
Histograms
Planar Subdivisions (C API)
Feature Detection and Description
Common Interfaces of Descriptor Extractors
Common Interfaces of Generic Descriptor Matchers
cuda. CUDA-accelerated Computer Vision
CUDA Module Introduction
Initalization and Information
Data Structures
Object Detection
Camera Calibration and 3D Reconstruction
cudaarithm. CUDA-accelerated Operations on Matrices
Core Operations on Matrices
Per-element Operations
Matrix Reductions
Arithm Operations on Matrices
cudabgsegm. CUDA-accelerated Background Segmentation
Background Segmentation
cudacodec. CUDA-accelerated Video Encoding/Decoding
Video Decoding
Video Encoding
cudafeatures2d. CUDA-accelerated Feature Detection and Description
Feature Detection and Description
cudafilters. CUDA-accelerated Image Filtering
Image Filtering
cudaimgproc. CUDA-accelerated Image Processing
Color space processing
Histogram Calculation
Hough Transform
Feature Detection
Image Processing
cudaoptflow. CUDA-accelerated Optical Flow
Optical Flow
cudastereo. CUDA-accelerated Stereo Correspondence
Stereo Correspondence
cudawarping. CUDA-accelerated Image Warping
Image Warping
optim. Generic numerical optimization
Linear Programming
Downhill Simplex Method
Primal-Dual Algorithm
Nonlinear Conjugate Gradient
shape. Shape Distance and Matching
Shape Distance and Common Interfaces
Shape Transformers and Interfaces
Cost Matrix for Histograms Common Interface
EMD-L1
softcascade. Soft Cascade object detection and training.
Soft Cascade Classifier
Soft Cascade Training
CUDA version of Soft Cascade Classifier
superres. Super Resolution
Super Resolution
videostab. Video Stabilization
Introduction
Global Motion Estimation
Fast Marching Method
viz. 3D Visualizer
Viz
Widget
Bibliography
The OpenCV Reference Manual Release 3.0.0-dev June 25, 2014
CONTENTS 1 Introduction 1.1 API Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 . . . . . . . . . . 7 2 core. The Core Functionality 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic Structures . . 2.1 64 Command Line Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 65 Basic C Structures and Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 . . 2.4 Dynamic Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 . . 2.5 Operations on Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 2.6 Drawing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 2.7 XML/YAML Persistence . 2.8 XML/YAML Persistence (C API) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 2.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 2.10 Utility and System Functions and Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 2.11 OpenGL interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 . 241 2.12 Intel® IPP Asynchronous C/C++ Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Image Filtering . 3 imgproc. Image Processing . . 243 3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 3.2 Geometric Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 3.3 Miscellaneous Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 3.4 Histograms . 3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 3.6 Motion Analysis and Object Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 3.7 Feature Detection . 3.8 Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 Structural Analysis and Shape Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . 4 highgui. High-level GUI and Media I/O 345 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Reading and Writing Images and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 4.1 User Interface 4.2 4.3 Qt New Functions . . . . . . . . . . . . . . . . 5 video. Video Analysis 369 5.1 Motion Analysis and Object Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 6 calib3d. Camera Calibration and 3D Reconstruction 6.1 Camera Calibration and 3D Reconstruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 . 393 7 features2d. 2D Features Framework 425 Feature Detection and Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 7.1 i
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 Common Interfaces of Feature Detectors 7.2 Common Interfaces of Descriptor Extractors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440 7.3 Common Interfaces of Descriptor Matchers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 7.4 7.5 Common Interfaces of Generic Descriptor Matchers . . . . . . . . . . . . . . . . . . . . . . . . . . 448 7.6 Drawing Function of Keypoints and Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 7.7 Object Categorization . . . . . 8 objdetect. Object Detection 8.1 8.2 8.3 Cascade Classification . Latent SVM . . . . Scene Text Detection . . . . . . . . . . . . . 9 ml. Machine Learning Statistical Models . . . . . . . . . . . Boosting . . Support Vector Machines . . . 9.1 9.2 Normal Bayes Classifier . 9.3 K-Nearest Neighbors . . 9.4 . 9.5 Decision Trees . 9.6 . . 9.7 Gradient Boosted Trees 9.8 . 9.9 9.10 Expectation Maximization . . 9.11 Neural Networks . 9.12 MLData . . . . . . . . . . Random Trees . Extremely randomized trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 475 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 . . . . . . . . . . . . . . . 10 flann. Clustering and Search in Multi-Dimensional Spaces 10.1 Fast Approximate Nearest Neighbor Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Clustering . 529 . 529 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533 . . . . . . . . . . . . 11 photo. Computational Photography . . . . . . . . . . 11.1 Inpainting . . . . 11.2 Denoising . . . . . 11.3 HDR imaging . . . . 11.4 References . . . 11.5 Decolorization . . 11.6 Seamless Cloning . . 11.7 Non-Photorealistic Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549 . . . . . . . . . . . . . . . . . . . . . . 12 stitching. Images stitching . . 553 . 12.1 Stitching Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 12.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554 12.3 High Level Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554 12.4 Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558 12.5 Features Finding and Images Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563 12.6 Rotation Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 . 12.7 Autocalibration . . . . 12.8 Images Warping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 12.9 Seam Estimation . . . . 12.10 Exposure Compensation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576 12.11 Image Blenders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 nonfree. Non-free functionality 581 13.1 Feature Detection and Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 ii
14 contrib. Contributed/Experimental Stuff 587 14.1 Stereo Correspondence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 14.2 FaceRecognizer - Face Recognition with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 14.3 OpenFABMAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 . . . . . . . . . . . . . . . . 15 legacy. Deprecated stuff 669 . 15.1 Motion Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 . 15.2 Expectation Maximization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670 15.3 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674 15.4 Planar Subdivisions (C API) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676 15.5 Feature Detection and Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683 15.6 Common Interfaces of Descriptor Extractors . 690 . . . . . . . . . . . . . . . . . . . . . . . . . . 691 15.7 Common Interfaces of Generic Descriptor Matchers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 cuda. CUDA-accelerated Computer Vision 16.1 CUDA Module Introduction . 16.2 Initalization and Information . . 16.3 Data Structures . . 16.4 Object Detection . . 16.5 Camera Calibration and 3D Reconstruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712 . 718 . . . . . . . . . . . . . . . . 17 cudaarithm. CUDA-accelerated Operations on Matrices 17.1 Core Operations on Matrices . . 17.2 Per-element Operations 17.3 Matrix Reductions . . . 17.4 Arithm Operations on Matrices . . . . . . . . 721 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738 18 cudabgsegm. CUDA-accelerated Background Segmentation 743 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743 18.1 Background Segmentation . . . 19 cudacodec. CUDA-accelerated Video Encoding/Decoding 19.1 Video Decoding . 19.2 Video Encoding . . . . . . . . . . . . . . . . . 747 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750 20 cudafeatures2d. CUDA-accelerated Feature Detection and Description 755 20.1 Feature Detection and Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755 21 cudafilters. CUDA-accelerated Image Filtering 21.1 Image Filtering . . . . . . . . . 767 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767 22 cudaimgproc. CUDA-accelerated Image Processing 22.1 Color space processing . 22.2 Histogram Calculation . 22.3 Hough Transform . . . . 22.4 Feature Detection . 22.5 Image Processing . . . . . . . . . . . . . . . . . . . . . . . . . . 775 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 23 cudaoptflow. CUDA-accelerated Optical Flow 23.1 Optical Flow . . . . . . . . . . . 793 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793 24 cudastereo. CUDA-accelerated Stereo Correspondence 24.1 Stereo Correspondence . . . . . 799 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799 25 cudawarping. CUDA-accelerated Image Warping 25.1 Image Warping . . . . . . . . . 807 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807 iii
26 optim. Generic numerical optimization . 26.1 Linear Programming . . . 26.2 Downhill Simplex Method . 26.3 Primal-Dual Algorithm . . . 26.4 Nonlinear Conjugate Gradient . . . . . . . 813 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818 27 shape. Shape Distance and Matching 821 27.1 Shape Distance and Common Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821 27.2 Shape Transformers and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826 27.3 Cost Matrix for Histograms Common Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829 27.4 EMD-L1 . . . . . . . . . . . . 28 softcascade. Soft Cascade object detection and training. 831 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831 28.1 Soft Cascade Classifier . 28.2 Soft Cascade Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834 28.3 CUDA version of Soft Cascade Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836 . . . . . . . . 29 superres. Super Resolution . . 29.1 Super Resolution . . . . 30 videostab. Video Stabilization . 30.1 Introduction . . 30.2 Global Motion Estimation . . 30.3 Fast Marching Method . . . . . . . 31 viz. 3D Visualizer . . . 31.1 Viz . 31.2 Widget . . . . . . . . . . . . . . . . . . . . 839 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839 841 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846 849 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862 . . . . . . . . . . . . . Bibliography 887 iv
CHAPTER ONE INTRODUCTION OpenCV (Open Source Computer Vision Library: http://opencv.org) is an open-source BSD-licensed library that includes several hundreds of computer vision algorithms. The document describes the so-called OpenCV 2.x API, which is essentially a C++ API, as opposite to the C-based OpenCV 1.x API. The latter is described in opencv1x.pdf. OpenCV has a modular structure, which means that the package includes several shared or static libraries. The following modules are available: • core - a compact module defining basic data structures, including the dense multi-dimensional array Mat and basic functions used by all other modules. • imgproc - an image processing module that includes linear and non-linear image filtering, geometrical image transformations (resize, affine and perspective warping, generic table-based remapping), color space conversion, histograms, and so on. • video - a video analysis module that includes motion estimation, background subtraction, and object tracking algorithms. • calib3d - basic multiple-view geometry algorithms, single and stereo camera calibration, object pose estimation, stereo correspondence algorithms, and elements of 3D reconstruction. • features2d - salient feature detectors, descriptors, and descriptor matchers. • objdetect - detection of objects and instances of the predefined classes (for example, faces, eyes, mugs, people, cars, and so on). • highgui - an easy-to-use interface to video capturing, image and video codecs, as well as simple UI capabilities. • gpu - GPU-accelerated algorithms from different OpenCV modules. • ... some other helper modules, such as FLANN and Google test wrappers, Python bindings, and others. The further chapters of the document describe functionality of each module. But first, make sure to get familiar with the common API concepts used thoroughly in the library. 1.1 API Concepts cv Namespace All the OpenCV classes and functions are placed into the cv namespace. Therefore, to access this functionality from your code, use the cv:: specifier or using namespace cv; directive: #include "opencv2/core.hpp" ... 1
The OpenCV Reference Manual, Release 3.0.0-dev cv::Mat H = cv::findHomography(points1, points2, CV_RANSAC, 5); ... or #include "opencv2/core.hpp" using namespace cv; ... Mat H = findHomography(points1, points2, CV_RANSAC, 5 ); ... Some of the current or future OpenCV external names may conflict with STL or other libraries. In this case, use explicit namespace specifiers to resolve the name conflicts: Mat a(100, 100, CV_32F); randu(a, Scalar::all(1), Scalar::all(std::rand())); cv::log(a, a); a /= std::log(2.); Automatic Memory Management OpenCV handles all the memory automatically. First of all, std::vector, Mat, and other data structures used by the functions and methods have destructors that deallocate the underlying memory buffers when needed. This means that the destructors do not always deallocate the buffers as in case of Mat. They take into account possible data sharing. A destructor decrements the reference counter associated with the matrix data buffer. The buffer is deallocated if and only if the reference counter reaches zero, that is, when no other structures refer to the same buffer. Similarly, when a Mat instance is copied, no actual data is really copied. Instead, the reference counter is incremented to memorize that there is another owner of the same data. There is also the Mat::clone method that creates a full copy of the matrix data. See the example below: // create a big 8Mb matrix Mat A(1000, 1000, CV_64F); // create another header for the same matrix; // this is an instant operation, regardless of the matrix size. Mat B = A; // create another header for the 3-rd row of A; no data is copied either Mat C = B.row(3); // now create a separate copy of the matrix Mat D = B.clone(); // copy the 5-th row of B to C, that is, copy the 5-th row of A // to the 3-rd row of A. B.row(5).copyTo(C); // now let A and D share the data; after that the modified version // of A is still referenced by B and C. A = D; // now make B an empty matrix (which references no memory buffers), // but the modified version of A will still be referenced by C, // despite that C is just a single row of the original A B.release(); // finally, make a full copy of C. As a result, the big modified // matrix will be deallocated, since it is not referenced by anyone C = C.clone(); You see that the use of Mat and other basic structures is simple. But what about high-level classes or even user data types created without taking automatic memory management into account? For them, OpenCV offers the Ptr template 2 Chapter 1. Introduction
分享到:
收藏