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 Name | Main Functions | Common Classes/Functions |
|---|---|---|
| Core | Provides basic data structures and functions, such as image storage, matrix operations, file I/O, etc. | Mat, Point, Size, Rect, Scalar, FileStorage, cv::format |
| Imgproc | Image processing functions, including filtering, geometric transformations, color space conversions, edge detection, morphological operations, thresholding, etc. | cvtColor, GaussianBlur, Canny, threshold, resize, warpAffine |
| Highgui | Display of images and videos, window management, user interaction (such as mouse events, trackbars). | imshow, namedWindow, waitKey, createTrackbar, setMouseCallback |
| Video | Video processing functions, including video capture, background subtraction, optical flow calculation, etc. | VideoCapture, VideoWriter, BackgroundSubtractor, calcOpticalFlowPyrLK |
| Calib3d | Camera calibration, 3D reconstruction, pose estimation, etc. | findChessboardCorners, calibrateCamera, solvePnP, recoverPose |
| Features2d | Feature detection and description, including keypoint detection, feature matching, etc. | ORB, SIFT, SURF, BFMatcher, FlannBasedMatcher |
| Objdetect | Object detection functions, such as Haar cascade detection, HOG detection, etc. | CascadeClassifier, HOGDescriptor |
| DNN | Loading and inference of deep learning models, supporting frameworks such as TensorFlow, PyTorch, Caffe, etc. | readNet, blobFromImage, Net::forward |
| ML | Machine learning algorithms, such as KNN, SVM, decision trees, etc. | KNearest, SVM, DTrees, TrainData |
| Flann | Fast Approximate Nearest Neighbor Search (FLANN), used for feature matching and high-dimensional data search. | Index, KDTreeIndexParams, SearchParams |
| Photo | Image inpainting, denoising, HDR imaging, etc. | inpaint, fastNlMeansDenoising, createTonemap |
| Stitching | Image stitching functionality, used to create panoramas. | Stitcher, Stitcher::create |
| Shape | Shape analysis and matching. | ShapeDistanceExtractor, ShapeContextDistanceExtractor |
| Tracking | Object tracking algorithms, such as MIL, KCF, GOTURN, etc. | TrackerMIL, TrackerKCF, TrackerGOTURN |
| Videoio | Video input/output functionality, supporting multiple video formats and cameras. | VideoCapture, VideoWriter, CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT |
| Imgcodecs | Reading and saving image files, supporting multiple image formats. | imread, imwrite, imdecode, imencode |
| Xfeatures2d | Additional feature detection and description algorithms, such as SIFT, SURF, FREAK, etc. | SIFT, SURF, FREAK, DAISY |
| Superres | Super-resolution image processing. | SuperResolution, DenseOpticalFlowExt |
| Optflow | Optical flow calculation and motion analysis. | calcOpticalFlowFarneback, calcOpticalFlowPyrLK |
| Cuda | GPU-accelerated computer vision algorithms. | cuda::GpuMat, cuda::Stream, cuda::resize |
| Contrib | Additional 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 <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/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
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/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/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/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;
}