@@ -2,6 +2,14 @@ import * as cv from "opencv4nodejs-prebuilt";
2
2
import { Image } from "../../image.class" ;
3
3
import { Region } from "../../region.class" ;
4
4
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
+
5
13
export class ImageProcessor {
6
14
/**
7
15
* fromImageWithAlphaChannel should provide a way to create a library specific
@@ -18,7 +26,7 @@ export class ImageProcessor {
18
26
) : Promise < cv . Mat > {
19
27
const mat = await new cv . Mat ( img . data , img . height , img . width , cv . CV_8UC4 ) . cvtColorAsync ( cv . COLOR_BGRA2BGR ) ;
20
28
if ( roi ) {
21
- return mat . getRegion ( new cv . Rect ( roi . left , roi . top , roi . width , roi . height ) ) ;
29
+ return mat . getRegion ( determineROI ( img , roi ) ) ;
22
30
} else {
23
31
return mat ;
24
32
}
@@ -39,7 +47,7 @@ export class ImageProcessor {
39
47
) : Promise < cv . Mat > {
40
48
const mat = new cv . Mat ( img . data , img . height , img . width , cv . CV_8UC3 ) ;
41
49
if ( roi ) {
42
- return mat . getRegion ( new cv . Rect ( roi . left , roi . top , roi . width , roi . height ) ) ;
50
+ return mat . getRegion ( determineROI ( img , roi ) ) ;
43
51
} else {
44
52
return mat ;
45
53
}
0 commit comments