Skip to content

Commit d3f3e51

Browse files
committed
(#68) Moved matching of images to separate function
1 parent 6d1b0c5 commit d3f3e51

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Diff for: lib/provider/opencv/match-image.function.spec.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as cv from "opencv4nodejs-prebuilt";
2+
import { mockPartial } from "sneer";
3+
import { matchImages } from "./match-image.function";
4+
5+
describe("matchImages", () => {
6+
it("should return minLoc position and needle size", async () => {
7+
// GIVEN
8+
const minLocX = 100;
9+
const minLocY = 1000;
10+
const matchMock = mockPartial<cv.Mat>({
11+
minMaxLocAsync: jest.fn(() => Promise.resolve({
12+
maxLoc: new cv.Point2(
13+
200,
14+
2000
15+
),
16+
maxVal: 100,
17+
minLoc: new cv.Point2(
18+
minLocX,
19+
minLocY
20+
),
21+
minVal: 0,
22+
}))
23+
});
24+
const haystackMock = mockPartial<cv.Mat>({
25+
matchTemplateAsync: jest.fn(() => Promise.resolve(matchMock))
26+
});
27+
const needleMock = mockPartial<cv.Mat>({
28+
cols: 123,
29+
rows: 456
30+
});
31+
32+
// WHEN
33+
const result = await matchImages(haystackMock, needleMock);
34+
35+
// THEN
36+
expect(result.location.left).toEqual(minLocX);
37+
expect(result.location.top).toEqual(minLocY);
38+
expect(result.location.width).toEqual(needleMock.cols);
39+
expect(result.location.height).toEqual(needleMock.rows);
40+
});
41+
});

Diff for: lib/provider/opencv/match-image.function.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as cv from "opencv4nodejs-prebuilt";
2+
import { MatchResult } from "../../match-result.class";
3+
import { Region } from "../../region.class";
4+
5+
export const matchImages = async (haystack: cv.Mat, needle: cv.Mat): Promise<MatchResult> => {
6+
const match = await haystack.matchTemplateAsync(
7+
needle,
8+
cv.TM_SQDIFF_NORMED,
9+
);
10+
const minMax = await match.minMaxLocAsync();
11+
return new MatchResult(
12+
1.0 - minMax.minVal,
13+
new Region(
14+
minMax.minLoc.x,
15+
minMax.minLoc.y,
16+
needle.cols,
17+
needle.rows,
18+
),
19+
);
20+
};

0 commit comments

Comments
 (0)