Skip to content

Commit 3cbca51

Browse files
committedSep 9, 2020
(#169) Added error message in case needle is way to large for haystack
1 parent bc1f191 commit 3cbca51

5 files changed

+33
-5
lines changed
 

Diff for: ‎lib/match-result.class.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export class MatchResult {
44
constructor(
55
public readonly confidence: number,
66
public readonly location: Region,
7+
public readonly error?: Error
78
) {}
89
}

Diff for: ‎lib/provider/opencv/__mocks__/fat-needle.png

41.8 KB
Loading

Diff for: ‎lib/provider/opencv/template-matching-finder.class.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,23 @@ describe("Template-matching finder", () => {
9191
.rejects
9292
.toThrowError(`Failed to load ${pathToHaystack}. Reason: 'Failed to load image from '${pathToHaystack}''.`);
9393
});
94+
95+
it("findMatch should reject, if needle was way lager than the haystack", async () => {
96+
// GIVEN
97+
const imageLoader = new ImageReader();
98+
const SUT = new TemplateMatchingFinder();
99+
const haystackPath = path.resolve(__dirname, "./__mocks__/mouse.png");
100+
const needlePath = path.resolve(__dirname, "./__mocks__/fat-needle.png");
101+
const haystack = await imageLoader.load(haystackPath);
102+
const minConfidence = 0.99;
103+
const searchRegion = new Region(0, 0, haystack.width, haystack.height);
104+
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
105+
const expectedRejection = "The provided image sample is larger than the provided search region"
106+
107+
// WHEN
108+
const findMatchPromise = SUT.findMatch(matchRequest);
109+
110+
// THEN
111+
await expect(findMatchPromise).rejects.toEqual(expectedRejection)
112+
});
94113
});

Diff for: ‎lib/provider/opencv/template-matching-finder.class.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ export default class TemplateMatchingFinder implements FinderInterface {
101101
0,
102102
0,
103103
0
104-
)
104+
),
105+
new Error("The provided image sample is larger than the provided search region")
105106
);
106107
}
107108
const matchResult = await matchImages(haystack, needle);
@@ -120,7 +121,8 @@ export default class TemplateMatchingFinder implements FinderInterface {
120121
0,
121122
0,
122123
0
123-
)
124+
),
125+
new Error("The provided image sample is larger than the provided search region")
124126
);
125127
}
126128
const matchResult = await matchImages(haystack, scaledNeedle);
@@ -142,7 +144,8 @@ export default class TemplateMatchingFinder implements FinderInterface {
142144
0,
143145
0,
144146
0
145-
)
147+
),
148+
new Error("The provided image sample is larger than the provided search region")
146149
);
147150
}
148151
const matchResult = await matchImages(scaledHaystack, needle);
@@ -182,7 +185,11 @@ export default class TemplateMatchingFinder implements FinderInterface {
182185
matches.sort((a, b) => a.confidence - b.confidence);
183186
const bestMatch = matches.pop();
184187
if (bestMatch) {
185-
reject(`No match with required confidence ${matchRequest.confidence}. Best match: ${bestMatch.confidence} at ${bestMatch.location}`)
188+
if(bestMatch.error) {
189+
reject(bestMatch.error.message)
190+
}else {
191+
reject(`No match with required confidence ${matchRequest.confidence}. Best match: ${bestMatch.confidence} at ${bestMatch.location}`)
192+
}
186193
} else {
187194
reject(`Unable to locate ${matchRequest.pathToNeedle}, no match!`);
188195
}

Diff for: ‎lib/scaled-match-result.class.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export class ScaledMatchResult extends MatchResult {
55
constructor(public readonly confidence: number,
66
public readonly scale: number,
77
public readonly location: Region,
8+
public readonly error?: Error
89
) {
9-
super(confidence, location);
10+
super(confidence, location, error);
1011
}
1112
}

0 commit comments

Comments
 (0)
Please sign in to comment.