Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ OpenCV

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:

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.

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.

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.

4. Video Module

The Video module focuses on video analysis, providing functions such as video capture, background subtraction, and optical flow computation.

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.

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.

7. Objdetect Module

The Objdetect module provides object detection functions, especially cascade classifiers based on Haar features and LBP features.

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.

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.


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.


OpenCV Related Content

No. 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