C++ OpenCV
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It consists of a series of C functions and a small number of C++ classes, and provides interfaces for languages such as Python, Java, and MATLAB.
The design goal of OpenCV is to provide a general-purpose computer vision library that helps developers quickly build complex visual applications.
OpenCV was originally developed by Intel, later supported by Willow Garage and Itseez, and is now maintained by OpenCV.org.
OpenCV is widely used in image processing, video analysis, object detection, face recognition, machine learning, and other fields.
OpenCV Core Features
The core features of OpenCV include:
-
Image processing: such as image filtering, geometric transformation, color space conversion, edge detection, etc.
-
Video processing: such as video capture, background subtraction, optical flow computation, etc.
-
Feature detection and description: such as SIFT, SURF, ORB, and other algorithms.
-
Object detection and tracking: such as Haar cascade detection, HOG detection, deep learning models, etc.
-
Camera calibration and 3D reconstruction: such as camera calibration, stereo vision, point cloud processing, etc.
-
Machine learning: such as traditional machine learning algorithms like KNN, SVM, decision trees, etc.
-
Deep learning: supports loading and running models from frameworks such as TensorFlow, PyTorch, Caffe, etc.
OpenCV Module Structure
OpenCV's functionality is organized into multiple modules, each focusing on a different task. The following is an introduction to the main modules:
| Module name | Description |
|---|---|
| Core | Provides basic data structures and functions, such as image storage, matrix operations, file I/O, etc. |
| Imgproc | Image processing functions, including filtering, geometric transformation, color space conversion, edge detection, morphological operations, etc. |
| Highgui | Display of images and video, window management, and user interaction (such as mouse events, sliders). |
| Video | Video processing functions, including video capture, background subtraction, optical flow computation, etc. |
| Calib3d | Camera calibration, 3D reconstruction, pose estimation, etc. |
| Features2d | Feature detection and description, including keypoint detection, feature matching, etc. |
| Objdetect | Object detection functions, such as Haar cascade detection, HOG detection, etc. |
| DNN | Loading and inference of deep learning models, supporting frameworks such as TensorFlow, PyTorch, Caffe, etc. |
| ML | Machine learning algorithms, such as KNN, SVM, decision trees, etc. |
| Flann | Fast approximate nearest neighbor search (FLANN), used for feature matching and high-dimensional data search. |
| Photo | Image inpainting, denoising, HDR imaging, etc. |
| Stitching | Image stitching functions, used to create panoramas. |
| Shape | Shape analysis and matching. |
| Tracking | Object tracking algorithms, such as MIL, KCF, GOTURN, etc. |
OpenCV Main Modules
The OpenCV library consists of multiple modules, each focusing on a different function. The following are some of the main modules in OpenCV:
1. Core Module
The Core module is the core module of OpenCV, containing the most basic data structures and functions. It defines the most commonly used data types in OpenCV, such as Mat(matrix),Point, Rect and others. The Core module also provides basic math operations, memory management, file I/O, and other functions.
- Mat class:
Matis the most commonly used data structure in OpenCV, used to store image and matrix data. It is a multi-dimensional array that can represent grayscale images, color images, 3D matrices, etc. - Basic operations: The Core module provides basic operations on matrices such as addition, subtraction, multiplication, division, transposition, and inversion.
2. Imgproc Module
The Imgproc module is the image processing module, providing a large number of image processing functions. These functions can be used for image filtering, geometric transformation, color space conversion, histogram calculation, etc.
- Image filtering: including mean filtering, Gaussian filtering, median filtering, etc., used to remove noise from images.
- Geometric transformation: such as image scaling, rotation, affine transformation, etc.
- Edge detection: such as Canny edge detection, Sobel operator, etc.
- Histogram equalization: used to enhance the contrast of an image.
3. Highgui Module
The Highgui module provides graphical user interface (GUI) functions, allowing developers to create windows, display images, and handle mouse and keyboard events, etc.
- Image display: When using
imshowfunctions can display images in a window. - Video capture: When using
VideoCaptureclasses can capture video frames from a camera or video file. - Event Handling: can handle events such as mouse clicks and keyboard input.
4. Video Module
The Video module focuses on video analysis, providing functions such as video capture, background subtraction, and optical flow computation.
- Video capture: When using
VideoCaptureclasses can read video frames from a camera or video file. - Background subtraction: used to extract foreground objects from video.
- Optical flow computation: used to estimate the motion of objects in an image.
5. Calib3d Module
The Calib3d module provides camera calibration, 3D reconstruction, stereo vision, and other functions. It is mainly used to handle camera-related geometric problems.
- Camera calibration: used to estimate the camera's intrinsic and extrinsic parameters.
- 3D reconstruction: reconstruct a 3D scene from multiple images.
- Stereo vision: used to compute depth maps.
6. Features2d Module
The Features2d module provides feature detection and descriptor extraction functions. It includes various feature detection algorithms such as SIFT, SURF, ORB, etc.
- Feature detection: detects keypoints in an image.
- Descriptor extraction: generates a descriptor for each keypoint, used to match features across different images.
7. Objdetect Module
The Objdetect module provides object detection functions, especially cascade classifiers based on Haar features and LBP features.
- Face detection: uses Haar features and cascade classifiers to detect faces in images.
- Object detection: can detect other types of objects, such as eyes, vehicles, etc.
8. ML Module
The ML module is the machine learning module, providing various machine learning algorithms such as Support Vector Machines (SVM), K-Nearest Neighbors (KNN), decision trees, etc.
- Classification: uses algorithms such as SVM and KNN for classification.
- Regression: uses algorithms such as linear regression for regression analysis.
- Clustering: uses algorithms such as K-means for cluster analysis.
9. DNN Module
The DNN module is the deep learning module, providing functions for loading and running deep learning models. It supports multiple deep learning frameworks such as TensorFlow, Caffe, ONNX, etc.
- Model loading: can load pre-trained deep learning models.
- Inference: uses the loaded model to perform tasks such as image classification and object detection.
OpenCV Installation and Configuration
Before using OpenCV, you need to install and configure the development environment first.
The following are the basic steps to install OpenCV:
3.1 Install OpenCV
On Linux systems, you can install OpenCV using the package manager:
sudo apt-get install libopencv-dev
On Windows systems, you can download precompiled libraries from the OpenCV official website, or install using vcpkg.
Configure the development environment
When using OpenCV in a C++ project, you need to link the OpenCV library at compile time.
The following is a simple CMake configuration example:
cmake_minimum_required(VERSION 3.10)
project(OpenCVExample)
find_package(OpenCV REQUIRED)
add_executable(OpenCVExample main.cpp)
target_link_libraries(OpenCVExample ${OpenCV_LIBS})
A Simple OpenCV Example
The following is a simple example of using OpenCV to read and display an image:
Example
#include
#include
int
main
(
)
{
// Read image
cv
::
Mat
image
=
cv
::
imread
(
"example.jpg"
)
;
// Check if the image loaded successfully
if
(
image.
empty
(
)
)
{
std
::
cout
<<
"Failed to load image!"
<<
std
::
endl
;
return
1
;
}
// Create a window and display the image
cv
::
namedWindow
(
"Example"
, cv
::
WINDOW_AUTOSIZE
)
;
cv
::
imshow
(
"Example"
, image
)
;
// Wait for a key press
cv
::
waitKey
(
0
)
;
return
0
;
}
In the code above, we use imread to read the image, use imshow to display the image, and use waitKey to wait for a key press from the user.