Overview
The C++ API is OpenCV’s native interface, providing direct access to all functionality with optimal performance. All OpenCV modules are written in C++ and offer the most complete feature set.Installation
Building from Source
OpenCV uses CMake as its build system. Here’s how to build and install OpenCV from source:- Linux
- macOS
- Windows
Key CMake Options
Customize your build with these important CMake flags:BUILD_EXAMPLES=ON- Build example applicationsBUILD_opencv_world=ON- Build single combined library (Windows)WITH_CUDA=ON- Enable CUDA support for GPU accelerationWITH_TBB=ON- Enable Intel TBB for parallel processingOPENCV_EXTRA_MODULES_PATH=<path>- Add opencv_contrib modules
Including OpenCV in Your Project
Using CMake
Create aCMakeLists.txt file:
Manual Compilation
Core Concepts
Including Headers
The main header includes all modules:Namespace
All OpenCV C++ functions and classes are in thecv namespace:
Code Examples
Reading and Displaying an Image
Face Detection with Cascade Classifier
Image Processing Pipeline
Working with Matrices
Best Practices
Memory Management: OpenCV uses reference counting for Mat objects. No need for manual memory management in most cases.
Performance Tips
- Use appropriate data types: Choose the smallest data type that fits your needs
- Avoid unnecessary copies: Use references and ROI (Region of Interest) operations
- Enable parallel processing: Build with TBB or OpenMP support
- Use GPU acceleration: Enable CUDA modules for compute-intensive operations
Mat Operations
Module Organization
OpenCV is organized into several modules:- core: Basic data structures and operations
- imgproc: Image processing functions
- imgcodecs: Image file reading and writing
- videoio: Video I/O operations
- highgui: GUI functionality
- video: Video analysis
- calib3d: Camera calibration and 3D reconstruction
- features2d: 2D feature detection and description
- objdetect: Object detection
- dnn: Deep neural network module
- ml: Machine learning
Resources
Next Steps
- Explore the Image Processing module
- Learn about Video Analysis
- Try Object Detection features
