Skip to content

Commit 3f549e1

Browse files
authored
Merge pull request #58 from nut-tree/bugfix/57/runtime_error
Closes #57
2 parents e8f2313 + 895417e commit 3f549e1

File tree

4 files changed

+74
-23
lines changed

4 files changed

+74
-23
lines changed

jest.config.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
module.exports = {
2-
preset: "ts-jest",
3-
testEnvironment: "node",
4-
collectCoverageFrom: [
5-
"index.ts",
6-
"lib/**/*.ts",
7-
"!lib/**/*.spec.ts",
8-
"!<rootDir>/node_modules/",
9-
"!<rootDir>/path/to/dir/"
10-
],
11-
testPathIgnorePatterns: [
12-
"/node_modules/",
13-
"/dist/"
14-
],
15-
testMatch: process.env.E2E_TEST ?
16-
[ "**/__tests__/?(e2e)/**/*.[jt]s?(x)", "**/?(*.)?(e2e.)+(spec|test).[jt]s?(x)" ] :
17-
[ "**/__tests__/!(e2e)/**/*.[jt]s?(x)", "**/!(*.e2e.*)+(spec|test).[jt]s?(x)" ]
18-
};
2+
collectCoverageFrom: [
3+
"index.ts",
4+
"lib/**/*.ts",
5+
"!lib/**/*.spec.ts",
6+
"!<rootDir>/node_modules/",
7+
"!<rootDir>/path/to/dir/",
8+
],
9+
preset: "ts-jest",
10+
testEnvironment: "node",
11+
testMatch: process.env.E2E_TEST ?
12+
["**/__tests__/?(e2e)/**/*.[jt]s?(x)", "**/?(*.)?(e2e.)+(spec|test).[jt]s?(x)"] :
13+
["**/__tests__/!(e2e)/**/*.[jt]s?(x)", "**/!(*.e2e.*)+(spec|test).[jt]s?(x)"],
14+
testPathIgnorePatterns: [
15+
"/node_modules/",
16+
"/dist/",
17+
],
18+
};

lib/provider/opencv/image-processor.class.spec.ts

+43
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { resolve } from "path";
2+
import { Region } from "../../region.class";
23
import { ImageProcessor } from "./image-processor.class";
34
import { ImageReader } from "./image-reader.class";
45

@@ -37,3 +38,45 @@ describe("ImageProcessor", () => {
3738
expect(mat.empty).toBeFalsy();
3839
});
3940
});
41+
42+
describe("ImageProcessor with ROI", () => {
43+
it("negative left or top values are updated to 0", async () => {
44+
// GIVEN
45+
const imageReader = new ImageReader();
46+
const imagePath = resolve(__dirname, "./__mocks__/mouse.png");
47+
const image = await imageReader.load(imagePath);
48+
49+
// WHEN
50+
const mat = await ImageProcessor.fromImageWithoutAlphaChannel(
51+
image,
52+
new Region(-100, -100, 10, 10)
53+
);
54+
55+
// THEN
56+
expect(image.hasAlphaChannel).toBeFalsy();
57+
expect(mat.channels).toEqual(3);
58+
expect(mat.rows).toEqual(10);
59+
expect(mat.cols).toEqual(10);
60+
expect(mat.empty).toBeFalsy();
61+
});
62+
63+
it("values bigger than the input are updated to width and height", async () => {
64+
// GIVEN
65+
const imageReader = new ImageReader();
66+
const imagePath = resolve(__dirname, "./__mocks__/mouse.png");
67+
const image = await imageReader.load(imagePath);
68+
69+
// WHEN
70+
const mat = await ImageProcessor.fromImageWithoutAlphaChannel(
71+
image,
72+
new Region(10, 10, image.width * 2, image.height * 2)
73+
);
74+
75+
// THEN
76+
expect(image.hasAlphaChannel).toBeFalsy();
77+
expect(mat.channels).toEqual(3);
78+
expect(mat.rows).toEqual(image.height - 10);
79+
expect(mat.cols).toEqual(image.width - 10);
80+
expect(mat.empty).toBeFalsy();
81+
});
82+
});

lib/provider/opencv/image-processor.class.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import * as cv from "opencv4nodejs-prebuilt";
22
import { Image } from "../../image.class";
33
import { Region } from "../../region.class";
44

5+
const determineROI = (img: Image, roi: Region): cv.Rect => {
6+
return new cv.Rect(
7+
Math.min(Math.max(roi.left, 0), img.width),
8+
Math.min(Math.max(roi.top, 0), img.height),
9+
Math.min(roi.width, img.width - roi.left),
10+
Math.min(roi.height, img.height - roi.top));
11+
};
12+
513
export class ImageProcessor {
614
/**
715
* fromImageWithAlphaChannel should provide a way to create a library specific
@@ -18,7 +26,7 @@ export class ImageProcessor {
1826
): Promise<cv.Mat> {
1927
const mat = await new cv.Mat(img.data, img.height, img.width, cv.CV_8UC4).cvtColorAsync(cv.COLOR_BGRA2BGR);
2028
if (roi) {
21-
return mat.getRegion(new cv.Rect(roi.left, roi.top, roi.width, roi.height));
29+
return mat.getRegion(determineROI(img, roi));
2230
} else {
2331
return mat;
2432
}
@@ -39,7 +47,7 @@ export class ImageProcessor {
3947
): Promise<cv.Mat> {
4048
const mat = new cv.Mat(img.data, img.height, img.width, cv.CV_8UC3);
4149
if (roi) {
42-
return mat.getRegion(new cv.Rect(roi.left, roi.top, roi.width, roi.height));
50+
return mat.getRegion(determineROI(img, roi));
4351
} else {
4452
return mat;
4553
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ export class TemplateMatchingFinder implements FinderInterface {
103103

104104
private static async debugResult(image: cv.Mat, result: MatchResult, filename: string, suffix?: string) {
105105
const roiRect = new cv.Rect(
106-
result.location.left,
107-
result.location.top,
108-
result.location.width,
109-
result.location.height);
106+
Math.min(Math.max(result.location.left, 0), image.cols),
107+
Math.min(Math.max(result.location.top, 0), image.rows),
108+
Math.min(result.location.width, image.cols - result.location.left),
109+
Math.min(result.location.height, image.rows - result.location.top));
110110
this.debugImage(image.getRegion(roiRect), filename, suffix);
111111
}
112112

0 commit comments

Comments
 (0)