-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.cpp
More file actions
379 lines (306 loc) · 11.5 KB
/
match.cpp
File metadata and controls
379 lines (306 loc) · 11.5 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "match.hpp"
namespace match {
#define MT_MASK_SUPPORT_CH_NUM 3
Mat sc3mat[MT_MASK_SUPPORT_CH_NUM];
Mat tl3mat[MT_MASK_SUPPORT_CH_NUM];
f64 rate3L[MT_MASK_SUPPORT_CH_NUM];
pt2 point3L[MT_MASK_SUPPORT_CH_NUM];
Mat result3L[MT_MASK_SUPPORT_CH_NUM];
#define forCH(start) for(int32 i = (start); i < MT_MASK_SUPPORT_CH_NUM; i++)
void ResultDrawAndShow(Mat &mat, const vector<pt2> &pt) {
if (mat.channels() == 3) cvtColor(mat, mat, COLOR_BGR2GRAY);
if (mat.channels() == 1) cvtColor(mat, mat, COLOR_GRAY2BGR);
for (pt2 p: pt) {
circle(mat, p, 4, Scalar(0, 0, 255), FILLED, LINE_8);
}
imshow("debug", mat);
}
pt2 mask(Mat &screen, const typeTL &tl, f32 thh, int32 reg) {
clock_type start = clock_now;
// --------- 受入检查 ---------
if (tl.isNull || tl.alpha.empty() || tl.image.empty() || tl.channel != 4) {
Logger.error(fformat("MT Mask ERROR ({0}) 无效模板类/空蒙版/空模板/非透明图像", tl.text));
return MT_FALSE_PT2;
}
bool roi_enable = !(tl.reg & ROI_FULL_BIT);
if (roi_enable) screen = screen(tl.roi);
if (screen.size().height <= tl.height || screen.size().width <= tl.width) {
Logger.error(fformat("MT Rect ERROR ({0}) 背景过小", tl.text));
return MT_FALSE_PT2;
}
// --------- 匹配初始化 ---------
split(screen, sc3mat);
split(tl.image, tl3mat);
Mat MtResult{};
f64 MtRate{};
pt2 MtPoint{};
// --------- 匹配 ---------
forCH(0) {
matchTemplate(sc3mat[i], tl3mat[i], result3L[i], TM_CCORR_NORMED, tl.alpha);
minMaxLoc(result3L[i], nullptr, &rate3L[i], nullptr, &point3L[i]);
if (isinf(rate3L[i])) return MT_FALSE_PT2;
}
// --------- 额外调试日志 ---------
if (reg & MT_DEBUG_EXTRALOG) {
stringstream tempstr;
if (roi_enable)tempstr << "ROI" << tl.roi;
Logger.trace(fformat("MT Mask Result ({0}) {1}", tl.text, tempstr.str()));
forCH(0) cout << " " << point3L[i] << " " << rate3L[i] << endl;
}
// --------- 判断结果 ---------
if (reg & MT_MASK_MIXED) {
MtResult = result3L[0] / MT_MASK_SUPPORT_CH_NUM;
forCH(1) MtResult += result3L[i] / MT_MASK_SUPPORT_CH_NUM;
minMaxLoc(MtResult, nullptr, &MtRate, nullptr, &MtPoint);
} else {
for (int32 i = -1; i < MT_MASK_SUPPORT_CH_NUM - 1; i++) {
MtPoint = point3L[i == -1 ? MT_MASK_SUPPORT_CH_NUM - 1 : i] - point3L[i + 1];
if (hypot(MtPoint.x, MtPoint.y) > 20) return MT_FALSE_PT2;
}
MtRate = rate3L[0];
MtPoint = point3L[0];
MtResult = result3L[0];
forCH(1) {
MtRate += rate3L[i];
MtPoint += point3L[i];
MtResult += result3L[i];
}
MtRate /= MT_MASK_SUPPORT_CH_NUM;
MtPoint /= MT_MASK_SUPPORT_CH_NUM;
MtResult /= MT_MASK_SUPPORT_CH_NUM;
}
if (MtRate < (thh < 0.95 ? 0.95 : thh) || MtRate > 1.0) return MT_FALSE_PT2;
// --------- 返回概率 ---------
if (reg & MT_RET_RATE) return {int32(MtRate * 1000)};
// --------- 调整坐标 ---------
if (reg & MT_RET_XYCENTER) MtPoint += pt2(tl.halfW, tl.halfH);
// --------- 显示结果 ---------
if (reg & MT_DEBUG_SHOWPT) {
ResultDrawAndShow(screen, {MtPoint});
waitKey(1);
}
if (roi_enable) MtPoint += tl.roi.tl();
if (reg & MT_RET_XYSCALE) MtPoint = MtPoint * cv_scale;
if (!(reg & MT_DISABLE_LOG)) {
Logger.info(fformat(
"MATCH MASK({0})={1},{2} {3}% {4}ms {5}fps {6}",
tl.text, MtPoint.x, MtPoint.y, int32(MtRate * 100),
difftime_ms(clock_now - start).count(),
1000 / difftime_ms(clock_now - start).count(),
(reg & MT_MASK_MIXED) ? "MIXED" : "DISTANCE"
));
}
return MtPoint;
}
vector<pt2> mask_multi(Mat &screen, const typeTL &tl, f32 thh, int32 reg) {
clock_type start = clock_now;
// --------- 受入检查 ---------
if (tl.isNull || tl.alpha.empty() || tl.image.empty() || tl.channel != 4) {
Logger.error(fformat("MT Mask Multi ERROR ({0}) 无效模板类/空蒙版/空模板/非透明图像", tl.text));
return MT_FALSE_VEC;
}
bool RoiEnable = !(tl.reg & ROI_FULL_BIT);
if (RoiEnable) screen = screen(tl.roi);
if (screen.size().height <= tl.height || screen.size().width <= tl.width) {
Logger.error(fformat("MT Rect Multi ERROR ({0}) 背景过小", tl.text));
return MT_FALSE_VEC;
}
// --------- 匹配初始化 ---------
split(screen, sc3mat);
split(tl.image, tl3mat);
Mat MtResult{};
f64 MtRate{};
pt2 MtPoint{};
vector<pt2> MtPointList{};
// --------- 匹配 ---------
forCH(0) {
matchTemplate(sc3mat[i], tl3mat[i], result3L[i], TM_CCORR_NORMED, tl.alpha);
minMaxLoc(result3L[i], nullptr, &rate3L[i], nullptr, &point3L[i]);
if (isinf(rate3L[i])) return MT_FALSE_VEC;
}
// --------- 额外调试日志 ---------
if (reg & MT_DEBUG_EXTRALOG) {
stringstream tempstr;
if (RoiEnable)tempstr << "ROI" << tl.roi;
Logger.trace(fformat("MT Mask Multi Result ({0}) {1}", tl.text, tempstr.str()));
forCH(0) cout << " " << point3L[i] << " " << rate3L[i] << endl;
}
// --------- 判断结果 ---------
MtResult = result3L[0] / MT_MASK_SUPPORT_CH_NUM;
forCH(1) MtResult += result3L[i] / MT_MASK_SUPPORT_CH_NUM;
while (true) {
minMaxLoc(MtResult, nullptr, &MtRate, nullptr, &MtPoint);
if (isinf(MtRate) || MtRate < thh) break;
rectangle(MtResult, MtPoint, MtPoint + pt2(tl.width, tl.height), Scalar(0, 0, 0), 4);
if (reg & MT_RET_XYCENTER) MtPoint = pt2(MtPoint.x + tl.width / 2, MtPoint.y + tl.height / 2);
MtPointList.push_back(MtPoint);
}
if (MtPointList.empty()) return MT_FALSE_VEC;
// --------- 成功结果 ---------
if (!(reg & MT_DISABLE_LOG)) {
Logger.info(fformat(
"MATCH RECT MULTI({0}):{1} {4}ms {5}fps {6}",
tl.text, MtPointList.size(), "", "",
difftime_ms(clock_now - start).count(),
1000 / difftime_ms(clock_now - start).count(),
(reg & MT_MASK_MIXED) ? "MIXED" : "DISTANCE"
));
}
// --------- 显示结果 ---------
if (reg & MT_DEBUG_SHOWPT) {
ResultDrawAndShow(screen, MtPointList);
waitKey(1);
}
// --------- 转换结果 ---------
for (auto pt: MtPointList) {
if (RoiEnable) pt += tl.roi.tl();
if (reg & MT_RET_XYSCALE) pt *= cv_scale;
}
return MtPointList;
}
pt2 rect(Mat &screen, const typeTL &tl, f32 thh, int32 reg) {
clock_type start = clock_now;
if (tl.isNull || tl.image.empty()) {
Logger.error(fformat("MT Rect ERROR ({0}) 无效模板类/空模板", tl.text));
return MT_FALSE_PT2;
}
bool RoiEnable = !(tl.reg & ROI_FULL_BIT);
Mat MtResult{};
f64 MtRate{};
pt2 MtPoint{};
if (RoiEnable) screen = screen(tl.roi);
if (screen.size().height <= tl.height || screen.size().width <= tl.width) {
Logger.error(fformat("MT Rect ERROR ({0}) 背景过小", tl.text));
return MT_FALSE_PT2;
}
Mat tlMat = tl.image.clone();
if (!(reg & MT_MODE_RGB)) {
if (screen.channels() != 1) cvtColor(screen, screen, COLOR_BGRA2GRAY);
if (tl.channel != 1) cvtColor(tlMat, tlMat, COLOR_BGR2GRAY);
if (!tl.alpha.empty()) Logger.warn(fformat("正在将透明模板({0})灰度处理,可能会导致极低的匹配率", tl.text));
}
matchTemplate(screen, tlMat, MtResult, TM_CCOEFF_NORMED);
minMaxLoc(MtResult, nullptr, &MtRate, nullptr, &MtPoint);
if (isinf(MtRate)) return MT_FALSE_PT2;
if (MtRate < thh) return MT_FALSE_PT2;
if (reg & MT_RET_RATE) return {int32(MtRate * 1000)};
if (reg & MT_RET_XYCENTER) MtPoint = pt2(MtPoint.x + tl.width / 2, MtPoint.y + tl.height / 2);
if (reg & MT_DEBUG_SHOWPT) {
ResultDrawAndShow(screen, {MtPoint});
waitKey(1);
}
if (RoiEnable) MtPoint += tl.roi.tl();
if (reg & MT_RET_XYSCALE) MtPoint = MtPoint * cv_scale;
if (!(reg & MT_DISABLE_LOG)) {
Logger.info(fformat(
"MATCH RECT({0})={1},{2} {3}% {4}ms {5}fps {6}",
tl.text, MtPoint.x, MtPoint.y, int32(MtRate * 100),
difftime_ms(clock_now - start).count(),
1000 / difftime_ms(clock_now - start).count(),
(!(reg & MT_MODE_RGB)) ? "GARY" : "RGB"
));
}
return MtPoint;
}
vector<pt2> rect_multi(Mat &screen, const typeTL &tl, f32 thh, int32 reg) {
clock_type start = clock_now;
// --------- 输入检查 ---------
if (tl.isNull || tl.image.empty()) {
Logger.error(fformat("MT Rect Multi ERROR ({0}) 无效模板类/空模板", tl.text));
return MT_FALSE_VEC;
}
bool RoiEnable = !(tl.reg & ROI_FULL_BIT);
if (RoiEnable) screen = screen(tl.roi);
if (screen.size().height <= tl.height || screen.size().width <= tl.width) {
Logger.error(fformat("MT Rect Multi ERROR ({0}) 背景过小", tl.text));
return MT_FALSE_VEC;
}
// --------- 匹配初始化 ---------
Mat MtResult{};
f64 MtRate{};
pt2 MtPoint{};
vector<pt2> MtPointList{};
Mat tlMat = tl.image.clone();
if (!(reg & MT_MODE_RGB)) {
if (screen.channels() != 1) cvtColor(screen, screen, COLOR_BGRA2GRAY);
if (tl.channel != 1) cvtColor(tlMat, tlMat, COLOR_BGR2GRAY);
if (!tl.alpha.empty()) Logger.warn(fformat("MT Rect Multi WARN 正在将透明模板({0})灰度处理,可能会导致极低的匹配率", tl.text));
}
// --------- 匹配 ---------
matchTemplate(screen, tlMat, MtResult, TM_CCOEFF_NORMED);
while (true) {
minMaxLoc(MtResult, nullptr, &MtRate, nullptr, &MtPoint);
if (isinf(MtRate) || MtRate < thh) break;
rectangle(MtResult, MtPoint, MtPoint + pt2(tl.width, tl.height), Scalar(0, 0, 0), 4);
if (reg & MT_RET_XYCENTER) MtPoint = pt2(MtPoint.x + tl.width / 2, MtPoint.y + tl.height / 2);
MtPointList.push_back(MtPoint);
}
if (MtPointList.empty()) return MT_FALSE_VEC;
// --------- 成功结果 ---------
if (!(reg & MT_DISABLE_LOG)) {
Logger.info(fformat(
"MATCH RECT MULTI({0}):{1} {4}ms {5}fps {6}",
tl.text, MtPointList.size(), "", "",
difftime_ms(clock_now - start).count(),
1000 / difftime_ms(clock_now - start).count(),
(!(reg & MT_MODE_RGB)) ? "GARY" : "RGB"
));
}
// --------- 显示结果 ---------
if (reg & MT_DEBUG_SHOWPT) {
ResultDrawAndShow(screen, MtPointList);
waitKey(1);
}
// --------- 转换结果 ---------
for (auto pt: MtPointList) {
if (RoiEnable) pt += tl.roi.tl();
if (reg & MT_RET_XYSCALE) pt *= cv_scale;
}
return MtPointList;
}
vec_int32 matchtl(Mat &screen, vec_tl &tllist, f32 thh, int32 reg) {
for (const templ::TL &tl: tllist) {
if (tl.alpha.empty()) {
if (reg & MT_RET_COUNT) {
vector<pt2> ptlist = rect_multi(screen, tl, thh, reg);
if (ptlist.empty()) return MT_FALSE_VEC;
return {int32(ptlist.size())};
}
pt2 result = rect(screen, tl, thh, reg);
if (result.x == -1) return MT_FALSE_VEC;
return {result.x, result.y};
} else {
if (reg & MT_RET_COUNT) {
Logger.error(fformat("MatchTL({0}) 透明模板对象没有可用的计数函数 (MT_RET_COUNT)无效 ", tl.text));
return MT_FALSE_VEC;
}
pt2 result = mask(screen, tl, thh, reg);
if (result.x == -1) return MT_FALSE_VEC;
return {result.x, result.y};
}
}
return MT_FALSE_VEC;
}
vec_int32 matchtl(Mat &screen, const templ::TL &tl, f32 thh, int32 reg) {
if (tl.alpha.empty()) {
if (reg & MT_RET_COUNT) {
vector<pt2> ptlist = rect_multi(screen, tl, thh, reg);
if (ptlist.empty()) return MT_FALSE_VEC;
return {int32(ptlist.size())};
}
pt2 result = rect(screen, tl, thh, reg);
if (result.x < 0) return MT_FALSE_VEC;
return {result.x, result.y};
} else {
if (reg & MT_RET_COUNT) {
vector<pt2> ptlist = mask_multi(screen, tl, thh, reg);
if (ptlist.empty()) return MT_FALSE_VEC;
return {int32(ptlist.size())};
}
pt2 result = mask(screen, tl, thh, reg);
if (result.x < 0) return MT_FALSE_VEC;
return {result.x, result.y};
}
}
}