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 also provides interfaces for languages such as Python, Java, and MATLAB.

OpenCV's design goal is to provide a general-purpose computer vision library that helps developers quickly build complex visual applications.

OpenCV was initially developed by Intel, later supported by Willow Garage and Itseez, and is now maintained by OpenCV.org.

OpenCV is widely used in fields such as image processing, video analysis, object detection, face recognition, and machine learning.

OpenCV's core functions

The core features of OpenCV include:

  • Image Processing: such as image filtering, geometric transformations, color space conversion, edge detection, etc.

  • Video Processing: such as video capture, background subtraction, optical flow calculation, etc.

  • Feature Detection and Description: Algorithms such as SIFT, SURF, ORB, etc.

  • 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's module structure

The functions of OpenCV are organized into multiple modules, each focusing on different tasks. The following is a brief introduction to the main modules:

Module NameFunction Description
CoreProvides basic data structures and functions, such as image storage, matrix operations, file I/O, etc.
ImgprocImage processing functions, including filtering, geometric transformations, color space conversion, edge detection, morphological operations, etc.
HighguiDisplay of images and videos, window management, user interaction (such as mouse events, sliders).
VideoVideo processing functions, including video capture, background subtraction, optical flow calculation, etc.
Calib3dCamera calibration, 3D reconstruction, pose estimation, etc.
Features2dFeature detection and description, including keypoint detection, feature matching, etc.
ObjdetectObject detection functions, such as Haar cascade detection, HOG detection, etc.
DNNLoading and inference of deep learning models, supporting frameworks such as TensorFlow, PyTorch, Caffe, etc.
MLMachine learning algorithms, such as KNN, SVM, decision trees, etc.
FlannFast Approximate Nearest Neighbor Search (FLANN), used for feature matching and high-dimensional data searching.
PhotoImage inpainting, denoising, HDR imaging, etc.
StitchingImage stitching functionality, used to create panoramic images.
ShapeShape analysis and matching.
TrackingObject tracking algorithms, such as MIL, KCF, GOTURN, etc.

Main modules of OpenCV

The OpenCV library consists of multiple modules, each focusing on different functions. 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 asMat(Matrix),Point、Rectetc. The Core module also provides basic mathematical operations, memory management, file I/O, and other functions.

  • Mat Class:MatIt is 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 such as matrix 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 transformations, color space conversion, histogram calculation, etc.

  • Image Filtering: Including mean filtering, Gaussian filtering, median filtering, etc., used to remove noise in 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 image contrast.

3. Highgui Module

The Highgui module provides graphical user interface (GUI) functionality, allowing developers to create windows, display images, handle mouse and keyboard events, etc.

  • Image Display: UseimshowThe function can display images in a window.
  • Video Capture: UseVideoCaptureThe class can capture video frames from a camera or a video file.
  • Event Handling: Can handle events such as mouse clicks, keyboard input, etc.

4. Video Module

The Video module focuses on video analysis, providing video capture, background subtraction, optical flow calculation, and other functions.

  • Video Capture: UseVideoCaptureThe class can read video frames from a camera or a video file.
  • Background Subtraction: Used to extract foreground objects from videos.
  • Optical Flow Calculation: Used to estimate the motion of objects in images.

5. Calib3d Module

The Calib3d module provides functions such as camera calibration, 3D reconstruction, and stereo vision. It is mainly used to handle camera-related geometric problems.

  • Camera Calibration: Used to estimate camera intrinsic and extrinsic parameters.
  • 3D Reconstruction: Reconstruct 3D scenes from multiple images.
  • Stereo Vision: Used to compute depth maps.

6. Features2d Module

The Features2d module provides feature detection and descriptor extraction functionality. It includes a variety of feature detection algorithms, such as SIFT, SURF, ORB, etc.

  • Feature Detection: Detect keypoints in images.
  • Descriptor Extraction: Generates a descriptor for each keypoint, used to match features in different images.

7. Objdetect Module

The Objdetect module provides object detection functionality, 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 a variety of machine learning algorithms, such as Support Vector Machines (SVM), K-Nearest Neighbors (KNN), decision trees, etc.

  • Category: Uses algorithms such as SVM, KNN for classification.
  • Regression: Uses algorithms such as linear regression for regression analysis.
  • Clustering: Uses algorithms such as K-means for clustering analysis.

9. DNN Module

The DNN module is a deep learning module that provides the ability to load and run 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 models for tasks such as image classification, object detection, etc.

Installation and configuration of OpenCV

Before using OpenCV, you need to install and configure the development environment.

The following are the basic steps for installing OpenCV:

3.1 Installing OpenCV

On Linux systems, you can install OpenCV using the package manager:

sudo apt-get install libopencv-dev

On Windows, you can download the precompiled libraries from the OpenCV official website, or use vcpkg to install it.

Configuring the Development Environment

When using OpenCV in a C++ project, you need to link the OpenCV libraries 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 <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // Read image
    cv::Mat image = cv::imread("example.jpg");

    // Check if the image loaded successfully
    if (image.empty()) {
        std::cout << "Unable 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 key press
    cv::waitKey(0);

    return 0;
}

In the above code, we useimreadfunction to read an image, useimshowfunction to display an image, and usewaitKeyfunction to wait for user key input.


OpenCV Related Content

Serial Number content
1 C++ OpenCV Installation
2 C++ OpenCV Basic Operations
3 C++ OpenCV Image Processing
4 C++ OpenCV Basic Modules
5 C++ OpenCV Feature Detection and Description
6 C++ OpenCV Advanced Image Processing
7 C++ OpenCV Video Processing
8 C++ OpenCV Machine Learning and Deep Learning
9 C++ OpenCV Project Practice
other extensions