Introduction to C++ OpenCV Basic Modules

OpenCV is a powerful computer vision library containing multiple modules, each focusing on different tasks.

The following are some of the core modules in OpenCV:

Module NameMain FunctionsCommon Classes/Functions
CoreProvides basic data structures and functions, such as image storage, matrix operations, file I/O, etc.Mat, Point, Size, Rect, Scalar, FileStorage, cv::format
ImgprocImage processing functions, including filtering, geometric transformations, color space conversions, edge detection, morphological operations, thresholding, etc.cvtColor, GaussianBlur, Canny, threshold, resize, warpAffine
HighguiDisplay of images and videos, window management, user interaction (such as mouse events, trackbars).imshow, namedWindow, waitKey, createTrackbar, setMouseCallback
VideoVideo processing functions, including video capture, background subtraction, optical flow calculation, etc.VideoCapture, VideoWriter, BackgroundSubtractor, calcOpticalFlowPyrLK
Calib3dCamera calibration, 3D reconstruction, pose estimation, etc.findChessboardCorners, calibrateCamera, solvePnP, recoverPose
Features2dFeature detection and description, including keypoint detection, feature matching, etc.ORB, SIFT, SURF, BFMatcher, FlannBasedMatcher
ObjdetectObject detection functions, such as Haar cascade detection, HOG detection, etc.CascadeClassifier, HOGDescriptor
DNNLoading and inference of deep learning models, supporting frameworks such as TensorFlow, PyTorch, Caffe, etc.readNet, blobFromImage, Net::forward
MLMachine learning algorithms, such as KNN, SVM, decision trees, etc.KNearest, SVM, DTrees, TrainData
FlannFast Approximate Nearest Neighbor Search (FLANN), used for feature matching and high-dimensional data search.Index, KDTreeIndexParams, SearchParams
PhotoImage inpainting, denoising, HDR imaging, etc.inpaint, fastNlMeansDenoising, createTonemap
StitchingImage stitching functionality, used to create panoramas.Stitcher, Stitcher::create
ShapeShape analysis and matching.ShapeDistanceExtractor, ShapeContextDistanceExtractor
TrackingObject tracking algorithms, such as MIL, KCF, GOTURN, etc.TrackerMIL, TrackerKCF, TrackerGOTURN
VideoioVideo input/output functionality, supporting multiple video formats and cameras.VideoCapture, VideoWriter, CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT
ImgcodecsReading and saving image files, supporting multiple image formats.imread, imwrite, imdecode, imencode
Xfeatures2dAdditional feature detection and description algorithms, such as SIFT, SURF, FREAK, etc.SIFT, SURF, FREAK, DAISY
SuperresSuper-resolution image processing.SuperResolution, DenseOpticalFlowExt
OptflowOptical flow calculation and motion analysis.calcOpticalFlowFarneback, calcOpticalFlowPyrLK
CudaGPU-accelerated computer vision algorithms.cuda::GpuMat, cuda::Stream, cuda::resize
ContribAdditional features contributed by the community, such as face recognition, text detection, etc.FaceRecognizer, TextDetector

1. Core Module

coreThe Core module is the core module of OpenCV, providing basic data structures and functions.

Main Functions

  • Basic Data Structures:

    • Mat: Used to store image and matrix data.

    • Point、Size、Rect: Used to represent points, sizes, and rectangular regions.

    • Scalar: Used to represent colors or pixel values.

  • Matrix Operations:

    • Matrix creation, copying, conversion, arithmetic operations, etc.

  • File I/O:

    • Read and save images, videos, XML/YAML files, etc.

  • Memory Management:

    • Automatic memory management with support for reference counting.

Example

#include <opencv2/core.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main() {
    // Create a 3x3 matrix
    Mat mat = (Mat_<int>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);

    // Output the matrix
    cout << "Matrix:\n" << mat << endl;

    // Access matrix elements
    int value = mat.at<int>(1, 1);
    cout << "Value at (1, 1): " << value << endl;

    return 0;
}

2. Imgproc Module

imgprocThe module provides image processing functions, including filtering, geometric transformations, color space conversions, etc.

Main Functions

  • Image Filtering:

    • Mean filtering, Gaussian filtering, median filtering, etc.

  • Geometric Transformations:

    • Scaling, rotation, affine transformation, perspective transformation, etc.

  • Color Space Conversions:

    • Conversion from RGB to grayscale, HSV, Lab, and other color spaces.

  • Edge Detection:

    • Edge detection algorithms such as Canny, Sobel, Laplacian, etc.

  • Morphological Operations:

    • Erosion, dilation, opening, closing, etc.

  • Thresholding:

    • Simple thresholding, adaptive thresholding, etc.

Example

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;

int main() {
    // Read image
    Mat image = imread("test.jpg");
    if (image.empty()) return -1;

    // Convert to grayscale image
    Mat grayImage;
    cvtColor(image, grayImage, COLOR_BGR2GRAY);

    // Gaussian filter
    Mat blurredImage;
    GaussianBlur(grayImage, blurredImage, Size(5, 5), 0);

    // Display the result
    imshow("Original Image", image);
    imshow("Blurred Image", blurredImage);
    waitKey(0);

    return 0;
}

3. Highgui Module

highguiThe module provides image and video display, window management, and user interaction functions.

Main Functions

  • Image Display:

    • Create windows, display images, and wait for user input.

  • Video Capture:

    • Read frames from a camera or video file.

  • User Interaction:

    • Mouse events, trackbars, buttons, etc.

Example

#include <opencv2/highgui.hpp>

using namespace cv;

int main() {
    // Read image
    Mat image = imread("test.jpg");
    if (image.empty()) return -1;

    // Create a window and display the image
    namedWindow("Display Window", WINDOW_AUTOSIZE);
    imshow("Display Window", image);

    // Wait for user keypress
    waitKey(0);

    // Close the window
    destroyAllWindows();

    return 0;
}

4. Video Module

videoThe module provides video processing functions, including video capture, background subtraction, optical flow calculation, etc.

Main Functions

  • Video Capture:

    • Read frames from a camera or video file.

  • Background Subtraction:

    • Extract foreground objects from the video.

  • Optical Flow Calculation:

    • Calculate the motion of objects in an image.

Example

#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;

int main() {
    // Open camera
    VideoCapture cap(0);
    if (!cap.isOpened()) return -1;

    Mat frame;
    while (true) {
        // Read a frame
        cap >> frame;
        if (frame.empty()) break;

        // Display the frame
        imshow("Camera Feed", frame);

        // Press ESC to exit
        if (waitKey(30) == 27) break;
    }

    // Release the camera and close the window
    cap.release();
    destroyAllWindows();

    return 0;
}

5. Calib3d Module

calib3dThe module provides camera calibration, 3D reconstruction, pose estimation, and other functions.

Main Functions

  • Camera Calibration:

    • Calculate camera intrinsic parameters and distortion coefficients.

  • 3D Reconstruction:

    • Reconstruct a 3D scene from multi-view images.

  • Pose Estimation:

    • Estimate the 3D pose of an object.

Example

#include <opencv2/calib3d.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;

int main() {
    // Read image
    Mat image1 = imread("left.jpg");
    Mat image2 = imread("right.jpg");
    if (image1.empty() || image2.empty()) return -1;

    // Feature point detection and matching
    Ptr<Feature2D> detector = ORB::create();
    vector<KeyPoint> keypoints1, keypoints2;
    Mat descriptors1, descriptors2;
    detector->detectAndCompute(image1, noArray(), keypoints1, descriptors1);
    detector->detectAndCompute(image2, noArray(), keypoints2, descriptors2);

    BFMatcher matcher(NORM_HAMMING);
    vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

    // Compute the fundamental matrix
    vector<Point2f> points1, points2;
    for (const auto& match : matches) {
        points1.push_back(keypoints1[match.queryIdx].pt);
        points2.push_back(keypoints2[match.trainIdx].pt);
    }
    Mat fundamentalMatrix = findFundamentalMat(points1, points2, FM_RANSAC);

    // Output the fundamental matrix
    cout << "Fundamental Matrix:\n" << fundamentalMatrix << endl;

    return 0;
}

6. DNN Module

dnnThe module provides functions for loading and inference of deep learning models.

Main Functions

  • Model Loading:

    • Supports models from frameworks such as TensorFlow, PyTorch, Caffe, etc.

  • Inference:

    • Perform classification, object detection, semantic segmentation, etc. on images.

Example

#include <opencv2/dnn.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;
using namespace dnn;

int main() {
    // Load model
    Net net = readNetFromTensorflow("model.pb", "config.pbtxt");

    // Read image
    Mat image = imread("test.jpg");
    if (image.empty()) return -1;

    // Preprocess
    Mat blob = blobFromImage(image, 1.0, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
    net.setInput(blob);

    // Inference
    Mat output = net.forward();

    // Process output
    // ...

    return 0;
}
Other Extensions