-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtemplate-matching-finder.class.ts
executable file
·189 lines (173 loc) · 7.27 KB
/
template-matching-finder.class.ts
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
import * as cv from "opencv4nodejs-prebuilt";
import * as path from "path";
import {Image} from "../../image.class";
import {MatchRequest} from "../../match-request.class";
import {MatchResult} from "../../match-result.class";
import {Region} from "../../region.class";
import {ScaledMatchResult} from "../../scaled-match-result.class";
import {DataSource} from "./data-source.interface";
import {determineScaledSearchRegion} from "./determine-searchregion.function";
import {FinderInterface} from "./finder.interface";
import {ImageReader} from "./image-reader.class";
import {matchImages} from "./match-image.function";
import {scaleImage} from "./scale-image.function";
import {scaleLocation} from "./scale-location.function";
import {fromImageWithAlphaChannel, fromImageWithoutAlphaChannel} from "./image-processor.class";
async function loadNeedle(image: Image): Promise<cv.Mat> {
if (image.hasAlphaChannel) {
return fromImageWithAlphaChannel(image);
}
return fromImageWithoutAlphaChannel(image);
}
async function loadHaystack(matchRequest: MatchRequest): Promise<cv.Mat> {
const searchRegion = determineScaledSearchRegion(matchRequest);
if (matchRequest.haystack.hasAlphaChannel) {
return fromImageWithAlphaChannel(
matchRequest.haystack,
searchRegion,
);
} else {
return fromImageWithoutAlphaChannel(
matchRequest.haystack,
searchRegion,
);
}
}
function debugImage(image: cv.Mat, filename: string, suffix?: string) {
const parsedPath = path.parse(filename);
let fullFilename = parsedPath.name;
if (suffix) {
fullFilename = fullFilename + "_" + suffix;
}
fullFilename += parsedPath.ext;
const fullPath = path.join(parsedPath.dir, fullFilename);
cv.imwriteAsync(fullPath, image);
}
// function debugResult(image: cv.Mat, result: MatchResult, filename: string, suffix?: string) {
// const roiRect = new cv.Rect(
// Math.min(Math.max(result.location.left, 0), image.cols),
// Math.min(Math.max(result.location.top, 0), image.rows),
// Math.min(result.location.width, image.cols - result.location.left),
// Math.min(result.location.height, image.rows - result.location.top));
// debugImage(image.getRegion(roiRect), filename, suffix);
// }
function isValidSearch(needle: cv.Mat, haystack: cv.Mat): boolean {
return (needle.cols <= haystack.cols) && (needle.rows <= haystack.rows);
}
function createResultForInvalidSearch (currentScale: number) {
return new ScaledMatchResult(0,
currentScale,
new Region(
0,
0,
0,
0
),
new Error("The provided image sample is larger than the provided search region")
)
}
export default class TemplateMatchingFinder implements FinderInterface {
private initialScale = [1.0];
private scaleSteps = [0.9, 0.8, 0.7, 0.6, 0.5];
constructor(
private source: DataSource = new ImageReader(),
) {
}
public async findMatches(matchRequest: MatchRequest, debug: boolean = false): Promise<ScaledMatchResult[]> {
let needle: cv.Mat;
try {
const needleInput = await this.source.load(matchRequest.pathToNeedle);
needle = await loadNeedle(needleInput);
} catch (e) {
throw new Error(
`Failed to load ${matchRequest.pathToNeedle}. Reason: '${e}'.`,
);
}
if (!needle || needle.empty) {
throw new Error(
`Failed to load ${matchRequest.pathToNeedle}, got empty image.`,
);
}
const haystack = await loadHaystack(matchRequest);
if (debug) {
debugImage(needle, "input_needle.png");
debugImage(haystack, "input_haystack.png");
}
const matchResults = this.initialScale.map(
async (currentScale) => {
if (!isValidSearch(needle, haystack)) {
return createResultForInvalidSearch(currentScale);
}
const matchResult = await matchImages(haystack, needle);
return new ScaledMatchResult(matchResult.confidence, currentScale, matchResult.location);
}
);
if (matchRequest.searchMultipleScales) {
matchResults.push(...this.searchMultipleScales(needle, haystack))
}
return Promise.all(matchResults).then(results => {
results.forEach(matchResult => {
matchResult.location.left /= matchRequest.haystack.pixelDensity.scaleX;
matchResult.location.width /= matchRequest.haystack.pixelDensity.scaleX;
matchResult.location.top /= matchRequest.haystack.pixelDensity.scaleY;
matchResult.location.height /= matchRequest.haystack.pixelDensity.scaleY;
});
return results.sort(
(first, second) => second.confidence - first.confidence,
);
});
}
public async findMatch(matchRequest: MatchRequest, debug: boolean = false): Promise<MatchResult> {
const matches = await this.findMatches(matchRequest, debug);
const potentialMatches = matches
.filter(match => match.confidence >= matchRequest.confidence);
if (potentialMatches.length === 0) {
matches.sort((a, b) => a.confidence - b.confidence);
const bestMatch = matches.pop();
if (bestMatch) {
if(bestMatch.error) {
throw bestMatch.error
}else {
throw new Error(`No match with required confidence ${matchRequest.confidence}. Best match: ${bestMatch.confidence} at ${bestMatch.location}`)
}
} else {
throw new Error(`Unable to locate ${matchRequest.pathToNeedle}, no match!`);
}
}
return potentialMatches[0];
}
private searchMultipleScales(needle: cv.Mat, haystack: cv.Mat) {
const scaledNeedleResult = this.scaleSteps.map(
async (currentScale) => {
const scaledNeedle = await scaleImage(needle, currentScale);
if (!isValidSearch(scaledNeedle, haystack)) {
return createResultForInvalidSearch(currentScale);
}
const matchResult = await matchImages(haystack, scaledNeedle);
return new ScaledMatchResult(
matchResult.confidence,
currentScale,
matchResult.location,
);
}
);
const scaledHaystackResult = this.scaleSteps.map(
async (currentScale) => {
const scaledHaystack = await scaleImage(haystack, currentScale);
if (!isValidSearch(needle, scaledHaystack)) {
return createResultForInvalidSearch(currentScale);
}
const matchResult = await matchImages(scaledHaystack, needle);
return new ScaledMatchResult(
matchResult.confidence,
currentScale,
scaleLocation(
matchResult.location,
currentScale
)
);
}
);
return [...scaledHaystackResult, ...scaledNeedleResult];
}
}