OpenCV
是一个功能强大的计算机视觉库,包含多个模块,每个模块专注于不同的任务。
以下是 OpenCV 中一些核心模块:


1. Core模块
core 模块是 OpenCV 的核心模块,提供了基本的数据结构和函数。
主要功能
- 基本数据结构:
- Mat:用于存储图像和矩阵数据。
- Point、Size、Rect:用于表示点、尺寸和矩形区域。
- Scalar:用于表示颜色或像素值。
- 矩阵操作:
- 文件 I/O:
- 内存管理:
实例
#include <opencv2/core.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
// 创建一个 3x3 的矩阵
Mat mat = (Mat_<int>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
// 输出矩阵
cout << "Matrix:\n" << mat << endl;
// 访问矩阵元素
int value = mat.at<int>(1, 1);
cout << "Value at (1, 1): " << value << endl;
return 0;
}
|
2. Imgproc 模块
imgproc 模块提供了图像处理功能,包括滤波、几何变换、颜色空间转换等。
主要功能
- 图像滤波:
- 几何变换:
- 颜色空间转换:
- RGB 到灰度、HSV、Lab 等颜色空间的转换。
- 边缘检测:
- Canny、Sobel、Laplacian 等边缘检测算法。
- 形态学操作:
- 阈值化:
实例
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main() {
// 读取图像
Mat image = imread("test.jpg");
if (image.empty()) return -1;
// 转换为灰度图像
Mat grayImage;
cvtColor(image, grayImage, COLOR_BGR2GRAY);
// 高斯滤波
Mat blurredImage;
GaussianBlur(grayImage, blurredImage, Size(5, 5), 0);
// 显示结果
imshow("Original Image", image);
imshow("Blurred Image", blurredImage);
waitKey(0);
return 0;
}
|
3. Highgui 模块
highgui 模块提供了图像和视频的显示、窗口管理以及用户交互功能。
主要功能
实例
#include <opencv2/highgui.hpp>
using namespace cv;
int main() {
// 读取图像
Mat image = imread("test.jpg");
if (image.empty()) return -1;
// 创建窗口并显示图像
namedWindow("Display Window", WINDOW_AUTOSIZE);
imshow("Display Window", image);
// 等待用户按键
waitKey(0);
// 关闭窗口
destroyAllWindows();
return 0;
}
|
4. Video 模块
video 模块提供了视频处理功能,包括视频捕获、背景减除、光流计算等。
主要功能
实例
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main() {
// 打开摄像头
VideoCapture cap(0);
if (!cap.isOpened()) return -1;
Mat frame;
while (true) {
// 读取一帧
cap >> frame;
if (frame.empty()) break;
// 显示帧
imshow("Camera Feed", frame);
// 按下 ESC 键退出
if (waitKey(30) == 27) break;
}
// 释放摄像头并关闭窗口
cap.release();
destroyAllWindows();
return 0;
}
|
5. Calib3d 模块
calib3d 模块提供了相机标定、3D 重建、姿态估计等功能。
主要功能
实例
#include <opencv2/calib3d.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main() {
// 读取图像
Mat image1 = imread("left.jpg");
Mat image2 = imread("right.jpg");
if (image1.empty() || image2.empty()) return -1;
// 特征点检测与匹配
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);
// 计算基础矩阵
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);
// 输出基础矩阵
cout << "Fundamental Matrix:\n" << fundamentalMatrix << endl;
return 0;
}
|
6. DNN 模块
dnn 模块提供了深度学习模型的加载和推理功能。
主要功能
- 模型加载:
- 支持 TensorFlow、PyTorch、Caffe 等框架的模型。
- 推理:
实例
#include <opencv2/dnn.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace dnn;
int main() {
// 加载模型
Net net = readNetFromTensorflow("model.pb", "config.pbtxt");
// 读取图像
Mat image = imread("test.jpg");
if (image.empty()) return -1;
// 预处理
Mat blob = blobFromImage(image, 1.0, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false);
net.setInput(blob);
// 推理
Mat output = net.forward();
// 处理输出
// ...
return 0;
}
|
|