Skip to content

Commit 819ba1a

Browse files
committed
(#169) Some function extraction
1 parent 3cbca51 commit 819ba1a

File tree

1 file changed

+51
-60
lines changed

1 file changed

+51
-60
lines changed

lib/provider/opencv/template-matching-finder.class.ts

+51-60
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ function isValidSearch(needle: cv.Mat, haystack: cv.Mat): boolean {
6060
return (needle.cols <= haystack.cols) && (needle.rows <= haystack.rows);
6161
}
6262

63+
function createResultForInvalidSearch (currentScale: number) {
64+
return new ScaledMatchResult(0,
65+
currentScale,
66+
new Region(
67+
0,
68+
0,
69+
0,
70+
0
71+
),
72+
new Error("The provided image sample is larger than the provided search region")
73+
)
74+
}
75+
6376
export default class TemplateMatchingFinder implements FinderInterface {
6477
private initialScale = [1.0];
6578
private scaleSteps = [0.9, 0.8, 0.7, 0.6, 0.5];
@@ -94,72 +107,15 @@ export default class TemplateMatchingFinder implements FinderInterface {
94107
const matchResults = this.initialScale.map(
95108
async (currentScale) => {
96109
if (!isValidSearch(needle, haystack)) {
97-
return new ScaledMatchResult(0,
98-
currentScale,
99-
new Region(
100-
0,
101-
0,
102-
0,
103-
0
104-
),
105-
new Error("The provided image sample is larger than the provided search region")
106-
);
110+
return createResultForInvalidSearch(currentScale);
107111
}
108112
const matchResult = await matchImages(haystack, needle);
109113
return new ScaledMatchResult(matchResult.confidence, currentScale, matchResult.location);
110114
}
111115
);
116+
112117
if (matchRequest.searchMultipleScales) {
113-
const scaledNeedleResult = this.scaleSteps.map(
114-
async (currentScale) => {
115-
const scaledNeedle = await scaleImage(needle, currentScale);
116-
if (!isValidSearch(scaledNeedle, haystack)) {
117-
return new ScaledMatchResult(0,
118-
currentScale,
119-
new Region(
120-
0,
121-
0,
122-
0,
123-
0
124-
),
125-
new Error("The provided image sample is larger than the provided search region")
126-
);
127-
}
128-
const matchResult = await matchImages(haystack, scaledNeedle);
129-
return new ScaledMatchResult(
130-
matchResult.confidence,
131-
currentScale,
132-
matchResult.location,
133-
);
134-
}
135-
);
136-
const scaledHaystackResult = this.scaleSteps.map(
137-
async (currentScale) => {
138-
const scaledHaystack = await scaleImage(haystack, currentScale);
139-
if (!isValidSearch(needle, scaledHaystack)) {
140-
return new ScaledMatchResult(0,
141-
currentScale,
142-
new Region(
143-
0,
144-
0,
145-
0,
146-
0
147-
),
148-
new Error("The provided image sample is larger than the provided search region")
149-
);
150-
}
151-
const matchResult = await matchImages(scaledHaystack, needle);
152-
return new ScaledMatchResult(
153-
matchResult.confidence,
154-
currentScale,
155-
scaleLocation(
156-
matchResult.location,
157-
currentScale
158-
)
159-
);
160-
}
161-
);
162-
matchResults.push(...scaledHaystackResult, ...scaledNeedleResult);
118+
matchResults.push(...this.searchMultipleScales(needle, haystack))
163119
}
164120

165121
return Promise.all(matchResults).then(results => {
@@ -200,4 +156,39 @@ export default class TemplateMatchingFinder implements FinderInterface {
200156
}
201157
});
202158
}
159+
160+
private searchMultipleScales(needle: cv.Mat, haystack: cv.Mat) {
161+
const scaledNeedleResult = this.scaleSteps.map(
162+
async (currentScale) => {
163+
const scaledNeedle = await scaleImage(needle, currentScale);
164+
if (!isValidSearch(scaledNeedle, haystack)) {
165+
return createResultForInvalidSearch(currentScale);
166+
}
167+
const matchResult = await matchImages(haystack, scaledNeedle);
168+
return new ScaledMatchResult(
169+
matchResult.confidence,
170+
currentScale,
171+
matchResult.location,
172+
);
173+
}
174+
);
175+
const scaledHaystackResult = this.scaleSteps.map(
176+
async (currentScale) => {
177+
const scaledHaystack = await scaleImage(haystack, currentScale);
178+
if (!isValidSearch(needle, scaledHaystack)) {
179+
return createResultForInvalidSearch(currentScale);
180+
}
181+
const matchResult = await matchImages(scaledHaystack, needle);
182+
return new ScaledMatchResult(
183+
matchResult.confidence,
184+
currentScale,
185+
scaleLocation(
186+
matchResult.location,
187+
currentScale
188+
)
189+
);
190+
}
191+
);
192+
return [...scaledHaystackResult, ...scaledNeedleResult];
193+
}
203194
}

0 commit comments

Comments
 (0)