|
| 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