C++ OpenCV 基本模块介绍

OpenCV(Open Source Computer Vision Library)是一个功能强大的开源计算机视觉和图像处理库,广泛应用于图像处理、视频分析、机器学习等领域。在 C++ 中,OpenCV 提供了多个模块,每个模块专注于特定功能。以下是对 OpenCV 主要模块的介绍,基于 OpenCV 4.x,旨在帮助你快速了解其核心功能和用途。

1. 核心模块 (core)

  • 功能:提供基本数据结构、矩阵运算和基本数学工具,是 OpenCV 的基础。
  • 关键内容
  • Mat:核心数据结构,用于存储图像或矩阵。
  • 基本操作:矩阵加减、乘除、转置、求逆等。
  • 示例
    cpp #include <opencv2/core.hpp> using namespace cv; Mat img = Mat::zeros(100, 100, CV_8UC3); // 创建 100x100 RGB 图像 Mat matrix = (Mat_<float>(2, 2) << 1, 2, 3, 4); // 2x2 矩阵 Mat inverse; invert(matrix, inverse); // 矩阵求逆
  • 用途:图像存储、基本运算、数据预处理。

2. 图像处理模块 (imgproc)

  • 功能:提供图像处理功能,包括滤波、变换、边缘检测、轮廓分析等。
  • 关键内容
  • 滤波:高斯模糊(GaussianBlur)、中值模糊(medianBlur)。
  • 阈值处理thresholdadaptiveThreshold
  • 边缘检测:Canny 算法(Canny)。
  • 几何变换:缩放(resize)、旋转(warpAffine)、透视变换(warpPerspective)。
  • 轮廓检测findContoursdrawContours
  • 示例
    cpp #include <opencv2/imgproc.hpp> Mat src = imread("image.jpg", IMREAD_GRAYSCALE); Mat blurred, edges; GaussianBlur(src, blurred, Size(5, 5), 0); Canny(blurred, edges, 100, 200);
  • 用途:图像增强、分割、特征提取。

3. 高层次 GUI 模块 (highgui)

  • 功能:提供图像/视频显示、用户交互和文件读写功能。
  • 关键内容
  • 图像读写imreadimwrite
  • 窗口管理namedWindowimshowdestroyWindow
  • 键盘/鼠标交互waitKey、鼠标回调(setMouseCallback)。
  • 示例
    cpp #include <opencv2/highgui.hpp> Mat img = imread("image.jpg"); namedWindow("Display", WINDOW_AUTOSIZE); imshow("Display", img); waitKey(0);
  • 用途:可视化图像、交互式调试。

4. 视频模块 (video)

  • 功能:处理视频流和运动分析。
  • 关键内容
  • 视频读写VideoCapture(读取视频/摄像头)、VideoWriter(保存视频)。
  • 运动跟踪:光流(calcOpticalFlowPyrLK)、背景分割(BackgroundSubtractor)。
  • 示例
    cpp #include <opencv2/video.hpp> VideoCapture cap(0); // 打开默认摄像头 if (!cap.isOpened()) return -1; Mat frame; while (cap.read(frame)) { imshow("Camera", frame); if (waitKey(30) == 27) break; // 按 ESC 退出 }
  • 用途:视频处理、实时分析。

5. 图像编解码模块 (imgcodecs)

  • 功能:处理图像文件的编码和解码。
  • 关键内容
  • 支持多种格式:JPG、PNG、BMP 等。
  • 扩展 imreadimwrite 功能,支持参数控制(如 JPG 压缩质量)。
  • 示例
    cpp #include <opencv2/imgcodecs.hpp> Mat img = imread("image.png", IMREAD_GRAYSCALE); imwrite("output.jpg", img, {IMWRITE_JPEG_QUALITY, 90});
  • 用途:图像格式转换、压缩。

6. 特征检测模块 (features2d)

  • 功能:检测和描述图像特征点,用于匹配和识别。
  • 关键内容
  • 特征检测:SIFT、SURF、ORB(ORB::create())。
  • 特征匹配BFMatcherFlannBasedMatcher
  • 示例
    cpp #include <opencv2/features2d.hpp> Mat img1 = imread("img1.jpg", IMREAD_GRAYSCALE); Mat img2 = imread("img2.jpg", IMREAD_GRAYSCALE); Ptr<ORB> orb = ORB::create(); vector<KeyPoint> keypoints1, keypoints2; Mat descriptors1, descriptors2; orb->detectAndCompute(img1, noArray(), keypoints1, descriptors1); orb->detectAndCompute(img2, noArray(), keypoints2, descriptors2); BFMatcher matcher(NORM_HAMMING); vector<DMatch> matches; matcher.match(descriptors1, descriptors2, matches);
  • 用途:图像匹配、物体识别。

7. 相机校正和 3D 重建模块 (calib3d)

  • 功能:相机校正、立体视觉和 3D 重建。
  • 关键内容
  • 相机校正calibrateCamera
  • 立体匹配StereoBMStereoSGBM
  • 姿态估计solvePnP
  • 用途:3D 重建、增强现实。

8. 机器学习模块 (ml)

  • 功能:提供机器学习算法,结合图像处理使用。
  • 关键内容
  • 支持算法:SVM、决策树、KNN、神经网络等。
  • 示例
    cpp #include <opencv2/ml.hpp> Ptr<ml::SVM> svm = ml::SVM::create(); svm->setType(ml::SVM::C_SVC); svm->setKernel(ml::SVM::LINEAR);
  • 用途:图像分类、目标检测。

9. 其他模块

  • objdetect:目标检测,如人脸检测(Haar、DNN)。
  #include <opencv2/objdetect.hpp>
  CascadeClassifier face_cascade;
  face_cascade.load("haarcascade_frontalface_default.xml");
  • photo:图像修复、去噪。
  • stitching:图像拼接,生成全景图。
  • dnn:深度学习支持,加载 TensorFlow、PyTorch 模型。

10. 使用建议

  • 模块选择:根据任务选择模块,例如图像增强用 imgproc,视频处理用 video
  • 头文件:OpenCV 4.x 使用 <opencv2/module.hpp> 格式,例如 <opencv2/imgproc.hpp>
  • 性能优化:优先使用内置函数,避免手动循环处理像素。
  • 官方文档:查阅 https://docs.opencv.org/4.x/ 获取最新 API 和示例。

11. 综合示例

以下代码结合多个模块,展示图像加载、处理和特征检测:

#include <opencv2/opencv.hpp>
#include <opencv2/features2d.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main() {
    // 核心模块:加载图像
    Mat img = imread("image.jpg", IMREAD_GRAYSCALE);
    if (img.empty()) {
        cout << "无法加载图像!" << endl;
        return -1;
    }

    // imgproc 模块:高斯模糊
    Mat blurred;
    GaussianBlur(img, blurred, Size(5, 5), 0);

    // features2d 模块:ORB 特征检测
    Ptr<ORB> orb = ORB::create();
    vector<KeyPoint> keypoints;
    Mat descriptors;
    orb->detectAndCompute(blurred, noArray(), keypoints, descriptors);

    // highgui 模块:显示结果
    Mat output;
    drawKeypoints(img, keypoints, output, Scalar::all(-1));
    imshow("Keypoints", output);
    waitKey(0);

    // imgcodecs 模块:保存结果
    imwrite("keypoints.jpg", output);

    destroyAllWindows();
    return 0;
}

如果你需要针对某个模块(如 dnnstitching)的深入讲解或具体任务的代码示例,请告诉我!

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注