-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplatematcher.cpp
36 lines (32 loc) · 1.2 KB
/
templatematcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "templatematcher.h"
cv::Mat TemplateMatcher::qImageToMat(const QImage &image)
{
switch (image.format()) {
case QImage::Format_RGB32: {
cv::Mat mat(image.height(), image.width(), CV_8UC4, (void*)image.bits(), image.bytesPerLine());
cv::cvtColor(mat, mat, cv::COLOR_BGRA2BGR);
return mat.clone(); // 深拷贝
}
case QImage::Format_RGB888: {
cv::Mat mat(image.height(), image.width(), CV_8UC3, (void*)image.bits(), image.bytesPerLine());
cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);
return mat.clone();
}
case QImage::Format_Grayscale8: {
cv::Mat mat(image.height(), image.width(), CV_8UC1, (void*)image.bits(), image.bytesPerLine());
return mat.clone();
}
default:
throw std::runtime_error("QImage 不支持该格式。");
}
}
cv::Mat TemplateMatcher::matchTemplate(const QImage &image, const QImage &templateImage)
{
// 转换为 OpenCV Mat
cv::Mat originalImage = qImageToMat(image);
cv::Mat templateImageMat = qImageToMat(templateImage);
// 进行模板匹配
cv::Mat result;
cv::matchTemplate(originalImage, templateImageMat, result, cv::TM_CCOEFF_NORMED);
return result;
}