forked from BokSoSa/OMR_scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templateMatch.cpp
107 lines (86 loc) · 2.75 KB
/
templateMatch.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <opencv2/opencv.hpp>
#include "Pixel.h"
#include "Edge.h"
#include "Color.h"
#include "Filter.h"
#include "ImageAnalysis.h"
#include <vector>
using namespace cv;
using namespace std;
vector<Point> templateMatch(Mat img, Mat tImg, double threshold) {
CImageAnalysis ci = CImageAnalysis();
Mat templateImg = tImg.clone();
Mat result;
double minVal, maxVal;
Point minLoc, maxLoc;
int count = 0;
Mat dstImg = img.clone();
vector<Point> templateMatchedPoints;
matchTemplate(dstImg, templateImg, result, TM_SQDIFF_NORMED);
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
for (int i = 0; i < result.rows; i++) {
for (int j = 0; j < result.cols; j++) {
if (result.at<float>(i, j) < threshold) {
// Áߺ¹ üũ
bool isOverlapped = false;
for(int k=0; k< templateMatchedPoints.size(); k++){
if (j >= templateMatchedPoints[k].x - templateImg.cols
&& j <= templateMatchedPoints[k].x + templateImg.cols
&& i >= templateMatchedPoints[k].y - templateImg.rows
&& i <= templateMatchedPoints[k].y + templateImg.rows) {
isOverlapped = true;
break;
}
}
if (!isOverlapped) {
templateMatchedPoints.push_back(Point(j, i));
rectangle(dstImg, Point(j, i), Point(j + templateImg.cols, i + templateImg.rows), Scalar(0, 0, 255), 1);
count++;
}
}
}
}
cout << "Found: " << count << '\n';
//imshow("result", result);
return templateMatchedPoints;
}
vector<Point> templateMatch(Mat img, vector<Mat> tImg, double threshold) {
//imshow("template", tImg);
CImageAnalysis ci = CImageAnalysis();
Mat result;
double minVal, maxVal;
Point minLoc, maxLoc;
int count = 0;
Mat dstImg = img.clone();
vector<Point> templateMatchedPoints;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
for (int h = 0; h < tImg.size(); h++) {
Mat templateImg = tImg[h].clone();
matchTemplate(dstImg, templateImg, result, TM_SQDIFF_NORMED);
for (int i = 0; i < result.rows; i++) {
for (int j = 0; j < result.cols; j++) {
if (result.at<float>(i, j) < threshold) {
// Áߺ¹ üũ
bool isOverlapped = false;
for (int k = 0; k < templateMatchedPoints.size(); k++) {
if (j >= templateMatchedPoints[k].x - templateImg.cols
&& j <= templateMatchedPoints[k].x + templateImg.cols
&& i >= templateMatchedPoints[k].y - templateImg.rows
&& i <= templateMatchedPoints[k].y + templateImg.rows) {
isOverlapped = true;
break;
}
}
if (!isOverlapped) {
templateMatchedPoints.push_back(Point(j, i));
//rectangle(dstImg, Point(j, i), Point(j + templateImg.cols, i + templateImg.rows), Scalar(0, 0, 255), 1);
count++;
}
}
}
}
}
cout << "Found: " << count << '\n';
//imshow("result", result);
return templateMatchedPoints;
}