OpenCV Basic Modules

OpenCV is a powerful computer vision library that contains multiple modules, each focusing on different functions.

OpenCV is composed of multiple modules, each providing different functionalities.

The following are some of the most commonly used modules in OpenCV:

  • cv2.core: Core module, contains basic functions for image processing (such as representation and manipulation of image arrays).
  • cv2.imgproc: Image processing module, provides various image operations such as filtering, image transformation, morphological operations, etc.
  • cv2.highgui: GUI module, provides functions for displaying images and videos.
  • cv2.video: Provides video processing functions, such as video capture, video stream processing, etc.
  • cv2.features2d: Feature detection and matching module, includes corner, edge, keypoint detection, etc.
  • cv2.ml: Machine learning module, provides multiple machine learning algorithms for image classification, regression, clustering, etc.
  • cv2.calib3d : Camera calibration and 3D reconstruction module.
  • cv2.objdetect : Object detection module.
  • cv2.dnn : Deep learning module.

1. Core Module

Functions:Provides OpenCV's core functionality, including basic data structures, matrix operations, drawing functions, etc.

Main classes and functions:

  • Mat:The basic data structure in OpenCV for storing images and matrices.

  • Scalar:Used to represent colors or pixel values.

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

  • Basic drawing functions: cv.line()、cv.circle()、cv.rectangle()、cv.putText()etc.

Application scenarios:

  • Basic image operations (such as creating, copying, cropping).

  • Drawing geometric shapes and text.


2. Imgproc Module

Functions:Provides image processing functions, including image filtering, geometric transformations, color space conversions, etc.

Main classes and functions:

  • Image filtering: cv.blur()、cv.GaussianBlur()、cv.medianBlur()etc.

  • Geometric transformations: cv.resize()、cv.warpAffine()、cv.warpPerspective()etc.

  • Color space conversion: cv.cvtColor()(e.g., BGR to grayscale, BGR to HSV).

  • Thresholding: cv.threshold()、cv.adaptiveThreshold()。

  • Edge detection: cv.Canny()、cv.Sobel()、cv.Laplacian()。

Application scenarios:

  • Image smoothing, sharpening, edge detection.

  • Image scaling, rotation, affine transformation.

  • Image binarization, color space conversion.


3. HighGUI Module

Functions:Provides high-level GUI and media I/O functions for image display and interaction.

Main classes and functions:

  • Image display: cv.imshow()、cv.waitKey()、cv.destroyAllWindows()。

  • Video capture: cv.VideoCapture()、cv.VideoWriter()。

  • Mouse and keyboard events: cv.setMouseCallback()。

Application scenarios:

  • Display images and videos.

  • Capture camera or video files.

  • Handle user interaction (such as mouse clicks, keyboard input).


4. Video Module

Functions:Provides video analysis functions, including motion detection, object tracking, etc.

Main classes and functions:

  • Background subtraction: cv.createBackgroundSubtractorMOG2()、cv.createBackgroundSubtractorKNN()。

  • Optical flow: cv.calcOpticalFlowPyrLK()。

  • Object tracking: cv.TrackerKCF_create()、cv.TrackerMOSSE_create()。

Application scenarios:

  • Motion detection in video.

  • Object tracking (e.g., pedestrian, vehicle tracking).


5. Calib3d Module

Functions:Provides camera calibration and 3D reconstruction functions.

Main classes and functions:

  • Camera calibration: cv.calibrateCamera()、cv.findChessboardCorners()。

  • 3D reconstruction: cv.solvePnP()、cv.reprojectImageTo3D()。

Application scenarios:

  • Camera calibration (used to remove lens distortion).

  • 3D reconstruction (e.g., recovering 3D information from 2D images).


6. Features2d Module

Functions:Provides feature detection and description functions.

Main classes and functions:

  • Feature detection: cv.SIFT_create()、cv.ORB_create()、cv.SURF_create()。

  • Feature matching: cv.BFMatcher()、cv.FlannBasedMatcher()。

  • Keypoint drawing: cv.drawKeypoints()。

Application scenarios:

  • Image feature extraction and matching.

  • Image stitching, object recognition.


7. Objdetect Module

Functions:Provides object detection functions.

Main classes and functions:

  • Haar feature classifier: cv.CascadeClassifier()(used for face detection).

  • HOG feature classifier:Used for pedestrian detection.

Application scenarios:

  • Face detection, pedestrian detection.


8. ML Module

Functions:Provides machine learning algorithms.

Main classes and functions:

  • Support Vector Machine (SVM): cv.ml.SVM_create()。

  • K-Means clustering: cv.kmeans()。

  • Neural Network (ANN): cv.ml.ANN_MLP_create()。

Application scenarios:

  • Image classification, clustering analysis.


9. DNN Module

Functions:Provides deep learning capabilities, supports loading and running pre-trained deep learning models.

Main classes and functions:

  • Model loading: cv.dnn.readNetFromCaffe()、cv.dnn.readNetFromTensorflow()。

  • Forward propagation: net.forward()。

Application scenarios:

  • Image classification, object detection, semantic segmentation.


10. Other Modules

  • Flann:Fast approximate nearest neighbor search.

  • Photo:Image inpainting and denoising.

  • Stitching:Image stitching.

  • Shape:Shape matching and distance calculation.

Other extensions