Skip to content

Commit a4f0170

Browse files
committed
(#131) Refactored image-processor.class.ts to export functions instead of class
1 parent 8d25b9f commit a4f0170

File tree

2 files changed

+101
-103
lines changed

2 files changed

+101
-103
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,82 @@
1-
import { resolve } from "path";
2-
import { Region } from "../../region.class";
3-
import { ImageProcessor } from "./image-processor.class";
4-
import { ImageReader } from "./image-reader.class";
1+
import {resolve} from "path";
2+
import {Region} from "../../region.class";
3+
import {ImageReader} from "./image-reader.class";
4+
import {fromImageWithAlphaChannel, fromImageWithoutAlphaChannel} from "./image-processor.class";
55

66
describe("ImageProcessor", () => {
7-
it("should allow to create a cv.Mat from an Image with alpha channel, alpha channel is dropped", async () => {
8-
// GIVEN
9-
const imageReader = new ImageReader();
10-
const imagePath = resolve(__dirname, "./__mocks__/alpha_channel.png");
11-
const image = await imageReader.load(imagePath);
7+
it("should allow to create a cv.Mat from an Image with alpha channel, alpha channel is dropped", async () => {
8+
// GIVEN
9+
const imageReader = new ImageReader();
10+
const imagePath = resolve(__dirname, "./__mocks__/alpha_channel.png");
11+
const image = await imageReader.load(imagePath);
1212

13-
// WHEN
14-
const mat = await ImageProcessor.fromImageWithAlphaChannel(image);
13+
// WHEN
14+
const mat = await fromImageWithAlphaChannel(image);
1515

16-
// THEN
17-
expect(image.hasAlphaChannel).toBeTruthy();
18-
expect(mat.channels).toEqual(3);
19-
expect(mat.rows).toEqual(image.height);
20-
expect(mat.cols).toEqual(image.width);
21-
expect(mat.empty).toBeFalsy();
22-
});
16+
// THEN
17+
expect(image.hasAlphaChannel).toBeTruthy();
18+
expect(mat.channels).toEqual(3);
19+
expect(mat.rows).toEqual(image.height);
20+
expect(mat.cols).toEqual(image.width);
21+
expect(mat.empty).toBeFalsy();
22+
});
2323

24-
it("should allow to create a cv.Mat from an Image without alpha channel", async () => {
25-
// GIVEN
26-
const imageReader = new ImageReader();
27-
const imagePath = resolve(__dirname, "./__mocks__/mouse.png");
28-
const image = await imageReader.load(imagePath);
24+
it("should allow to create a cv.Mat from an Image without alpha channel", async () => {
25+
// GIVEN
26+
const imageReader = new ImageReader();
27+
const imagePath = resolve(__dirname, "./__mocks__/mouse.png");
28+
const image = await imageReader.load(imagePath);
2929

30-
// WHEN
31-
const mat = await ImageProcessor.fromImageWithoutAlphaChannel(image);
30+
// WHEN
31+
const mat = await fromImageWithoutAlphaChannel(image);
3232

33-
// THEN
34-
expect(image.hasAlphaChannel).toBeFalsy();
35-
expect(mat.channels).toEqual(3);
36-
expect(mat.rows).toEqual(image.height);
37-
expect(mat.cols).toEqual(image.width);
38-
expect(mat.empty).toBeFalsy();
39-
});
33+
// THEN
34+
expect(image.hasAlphaChannel).toBeFalsy();
35+
expect(mat.channels).toEqual(3);
36+
expect(mat.rows).toEqual(image.height);
37+
expect(mat.cols).toEqual(image.width);
38+
expect(mat.empty).toBeFalsy();
39+
});
4040
});
4141

4242
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);
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);
4848

49-
// WHEN
50-
const mat = await ImageProcessor.fromImageWithoutAlphaChannel(
51-
image,
52-
new Region(-100, -100, 10, 10)
53-
);
49+
// WHEN
50+
const mat = await fromImageWithoutAlphaChannel(
51+
image,
52+
new Region(-100, -100, 10, 10)
53+
);
5454

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-
});
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+
});
6262

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);
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);
6868

69-
// WHEN
70-
const mat = await ImageProcessor.fromImageWithoutAlphaChannel(
71-
image,
72-
new Region(10, 10, image.width * 2, image.height * 2)
73-
);
69+
// WHEN
70+
const mat = await fromImageWithoutAlphaChannel(
71+
image,
72+
new Region(10, 10, image.width * 2, image.height * 2)
73+
);
7474

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-
});
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+
});
8282
});
+35-37
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,53 @@
11
import * as cv from "opencv4nodejs-prebuilt";
2-
import { Image } from "../../image.class";
3-
import { Region } from "../../region.class";
2+
import {Image} from "../../image.class";
3+
import {Region} from "../../region.class";
44

55
function 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));
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));
1111
}
1212

13-
export class ImageProcessor {
14-
/**
15-
* fromImageWithAlphaChannel should provide a way to create a library specific
16-
* image with alpha channel from an abstract Image object holding raw data and image dimension
17-
*
18-
* @param {Image} img The input Image
19-
* @param {Region} [roi] An optional Region to specify a ROI
20-
* @returns {Promise<any>} An image
21-
* @memberof VisionProviderInterface
22-
*/
23-
public static async fromImageWithAlphaChannel(
13+
/**
14+
* fromImageWithAlphaChannel should provide a way to create a library specific
15+
* image with alpha channel from an abstract Image object holding raw data and image dimension
16+
*
17+
* @param {Image} img The input Image
18+
* @param {Region} [roi] An optional Region to specify a ROI
19+
* @returns {Promise<any>} An image
20+
* @memberof VisionProviderInterface
21+
*/
22+
export const fromImageWithAlphaChannel = async (
2423
img: Image,
2524
roi?: Region,
26-
): Promise<cv.Mat> {
25+
): Promise<cv.Mat> => {
2726
const mat = await new cv.Mat(img.data, img.height, img.width, cv.CV_8UC4).cvtColorAsync(cv.COLOR_BGRA2BGR);
2827
if (roi) {
29-
return mat.getRegion(determineROI(img, roi));
28+
return mat.getRegion(determineROI(img, roi));
3029
} else {
31-
return mat;
30+
return mat;
3231
}
33-
}
32+
};
3433

35-
/**
36-
* fromImageWithoutAlphaChannel should provide a way to create a library specific
37-
* image without alpha channel from an abstract Image object holding raw data and image dimension
38-
*
39-
* @param {Image} img The input Image
40-
* @param {Region} [roi] An optional Region to specify a ROI
41-
* @returns {Promise<any>} An image
42-
* @memberof VisionProviderInterface
43-
*/
44-
public static async fromImageWithoutAlphaChannel(
34+
/**
35+
* fromImageWithoutAlphaChannel should provide a way to create a library specific
36+
* image without alpha channel from an abstract Image object holding raw data and image dimension
37+
*
38+
* @param {Image} img The input Image
39+
* @param {Region} [roi] An optional Region to specify a ROI
40+
* @returns {Promise<any>} An image
41+
* @memberof VisionProviderInterface
42+
*/
43+
export const fromImageWithoutAlphaChannel = async (
4544
img: Image,
4645
roi?: Region,
47-
): Promise<cv.Mat> {
46+
): Promise<cv.Mat> => {
4847
const mat = new cv.Mat(img.data, img.height, img.width, cv.CV_8UC3);
4948
if (roi) {
50-
return mat.getRegion(determineROI(img, roi));
49+
return mat.getRegion(determineROI(img, roi));
5150
} else {
52-
return mat;
51+
return mat;
5352
}
54-
}
55-
}
53+
};

0 commit comments

Comments
 (0)