Skip to content

Commit 4a5a6f6

Browse files
committed
Created image-processor
1 parent e1287ca commit 4a5a6f6

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { resolve } from "path";
2+
import { ImageProcessor } from "./image-processor.class";
3+
import { ImageReader } from "./image-reader.class";
4+
5+
describe("ImageProcessor", () => {
6+
it("should allow to create a cv.Mat from an Image with alpha channel, alpha channel is dropped", async () => {
7+
// GIVEN
8+
const imageReader = new ImageReader();
9+
const imagePath = resolve(__dirname, "./__mocks__/alpha_channel.png");
10+
const image = await imageReader.load(imagePath);
11+
12+
// WHEN
13+
const mat = await ImageProcessor.fromImageWithAlphaChannel(image);
14+
15+
// THEN
16+
expect(image.hasAlphaChannel).toBeTruthy();
17+
expect(mat.channels).toEqual(3);
18+
expect(mat.rows).toEqual(image.height);
19+
expect(mat.cols).toEqual(image.width);
20+
expect(mat.empty).toBeFalsy();
21+
});
22+
23+
it("should allow to create a cv.Mat from an Image without alpha channel", async () => {
24+
// GIVEN
25+
const imageReader = new ImageReader();
26+
const imagePath = resolve(__dirname, "./__mocks__/mouse.png");
27+
const image = await imageReader.load(imagePath);
28+
29+
// WHEN
30+
const mat = await ImageProcessor.fromImageWithoutAlphaChannel(image);
31+
32+
// THEN
33+
expect(image.hasAlphaChannel).toBeFalsy();
34+
expect(mat.channels).toEqual(3);
35+
expect(mat.rows).toEqual(image.height);
36+
expect(mat.cols).toEqual(image.width);
37+
expect(mat.empty).toBeFalsy();
38+
});
39+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as cv from "opencv4nodejs";
2+
import { Image } from "../../image.class";
3+
import { Region } from "../../region.class";
4+
5+
export class ImageProcessor {
6+
/**
7+
* fromImageWithAlphaChannel should provide a way to create a library specific
8+
* image with alpha channel from an abstract Image object holding raw data and image dimension
9+
*
10+
* @param {Image} img The input Image
11+
* @param {Region} [roi] An optional Region to specify a ROI
12+
* @returns {Promise<any>} An image
13+
* @memberof VisionProviderInterface
14+
*/
15+
public static async fromImageWithAlphaChannel(
16+
img: Image,
17+
roi?: Region,
18+
): Promise<cv.Mat> {
19+
const mat = await new cv.Mat(img.data, img.height, img.width, cv.CV_8UC4).cvtColorAsync(cv.COLOR_BGRA2BGR);
20+
if (roi) {
21+
return mat.getRegion(new cv.Rect(roi.left, roi.top, roi.width, roi.height));
22+
} else {
23+
return mat;
24+
}
25+
}
26+
27+
/**
28+
* fromImageWithoutAlphaChannel should provide a way to create a library specific
29+
* image without alpha channel from an abstract Image object holding raw data and image dimension
30+
*
31+
* @param {Image} img The input Image
32+
* @param {Region} [roi] An optional Region to specify a ROI
33+
* @returns {Promise<any>} An image
34+
* @memberof VisionProviderInterface
35+
*/
36+
public static async fromImageWithoutAlphaChannel(
37+
img: Image,
38+
roi?: Region,
39+
): Promise<cv.Mat> {
40+
const mat = new cv.Mat(img.data, img.height, img.width, cv.CV_8UC3);
41+
if (roi) {
42+
return mat.getRegion(new cv.Rect(roi.left, roi.top, roi.width, roi.height));
43+
} else {
44+
return mat;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)