-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathimage-processor.class.ts
55 lines (52 loc) · 1.77 KB
/
image-processor.class.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as cv from "opencv4nodejs-prebuilt";
import { Image } from "../../image.class";
import { Region } from "../../region.class";
function determineROI(img: Image, roi: Region): cv.Rect {
return new cv.Rect(
Math.min(Math.max(roi.left, 0), img.width),
Math.min(Math.max(roi.top, 0), img.height),
Math.min(roi.width, img.width - roi.left),
Math.min(roi.height, img.height - roi.top));
}
export class ImageProcessor {
/**
* fromImageWithAlphaChannel should provide a way to create a library specific
* image with alpha channel from an abstract Image object holding raw data and image dimension
*
* @param {Image} img The input Image
* @param {Region} [roi] An optional Region to specify a ROI
* @returns {Promise<any>} An image
* @memberof VisionProviderInterface
*/
public static async fromImageWithAlphaChannel(
img: Image,
roi?: Region,
): Promise<cv.Mat> {
const mat = await new cv.Mat(img.data, img.height, img.width, cv.CV_8UC4).cvtColorAsync(cv.COLOR_BGRA2BGR);
if (roi) {
return mat.getRegion(determineROI(img, roi));
} else {
return mat;
}
}
/**
* fromImageWithoutAlphaChannel should provide a way to create a library specific
* image without alpha channel from an abstract Image object holding raw data and image dimension
*
* @param {Image} img The input Image
* @param {Region} [roi] An optional Region to specify a ROI
* @returns {Promise<any>} An image
* @memberof VisionProviderInterface
*/
public static async fromImageWithoutAlphaChannel(
img: Image,
roi?: Region,
): Promise<cv.Mat> {
const mat = new cv.Mat(img.data, img.height, img.width, cv.CV_8UC3);
if (roi) {
return mat.getRegion(determineROI(img, roi));
} else {
return mat;
}
}
}