This repository was archived by the owner on Jul 26, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ interface Computed {
33
33
fillRatio : number ;
34
34
internalIDs : number [ ] ;
35
35
feret : Feret ;
36
+ centroid : Point ;
36
37
}
37
38
export class Roi {
38
39
/**
@@ -328,6 +329,29 @@ export class Roi {
328
329
} ;
329
330
}
330
331
332
+ get centroid ( ) {
333
+ return this . #getComputed( 'centroid' , ( ) => {
334
+ const roiMap = this . getMap ( ) ;
335
+ const data = roiMap . data ;
336
+ let sumColumn = 0 ;
337
+ let sumRow = 0 ;
338
+ for ( let column = 0 ; column < this . width ; column ++ ) {
339
+ for ( let row = 0 ; row < this . height ; row ++ ) {
340
+ let target = this . computeIndex ( row , column ) ;
341
+ if ( data [ target ] === this . id ) {
342
+ sumColumn += column ;
343
+ sumRow += row ;
344
+ }
345
+ }
346
+ }
347
+
348
+ return {
349
+ column : sumColumn / this . surface + this . origin . column ,
350
+ row : sumRow / this . surface + this . origin . row ,
351
+ } ;
352
+ } ) ;
353
+ }
354
+
331
355
#getComputed< T extends keyof Computed > (
332
356
property : T ,
333
357
callback : ( ) => Computed [ T ] ,
Original file line number Diff line number Diff line change
1
+ import { fromMask } from '../..' ;
2
+
3
+ test ( 'centroid property 4x4' , ( ) => {
4
+ const mask = testUtils . createMask ( [
5
+ [ 0 , 1 , 1 , 1 ] ,
6
+ [ 0 , 1 , 0 , 1 ] ,
7
+ [ 0 , 1 , 1 , 1 ] ,
8
+ [ 0 , 0 , 0 , 0 ] ,
9
+ ] ) ;
10
+ const roiMapManager = fromMask ( mask ) ;
11
+ const rois = roiMapManager . getRois ( ) ;
12
+ expect ( rois [ 0 ] . centroid ) . toStrictEqual ( { row : 1 , column : 2 } ) ;
13
+ } ) ;
14
+
15
+ test ( 'border lengths property 5x5' , ( ) => {
16
+ const mask = testUtils . createMask ( [
17
+ [ 1 , 0 , 0 , 1 , 0 ] ,
18
+ [ 1 , 1 , 1 , 1 , 0 ] ,
19
+ [ 1 , 0 , 0 , 0 , 0 ] ,
20
+ [ 1 , 1 , 1 , 1 , 0 ] ,
21
+ [ 1 , 1 , 1 , 0 , 0 ] ,
22
+ ] ) ;
23
+ const roiMapManager = fromMask ( mask ) ;
24
+ const rois = roiMapManager . getRois ( ) ;
25
+ expect ( rois [ 0 ] . centroid . row ) . toBeCloseTo ( 2.14286 ) ;
26
+ expect ( rois [ 0 ] . centroid . column ) . toBeCloseTo ( 1.28571 ) ;
27
+ } ) ;
28
+
29
+ test ( 'border lengths property 5x5' , ( ) => {
30
+ const mask = testUtils . createMask ( [
31
+ [ 1 , 1 , 1 ] ,
32
+ [ 0 , 1 , 0 ] ,
33
+ [ 1 , 1 , 1 ] ,
34
+ ] ) ;
35
+ const roiMapManager = fromMask ( mask ) ;
36
+ const rois = roiMapManager . getRois ( ) ;
37
+ expect ( rois [ 0 ] . centroid . row ) . toBeCloseTo ( 1 ) ;
38
+ expect ( rois [ 0 ] . centroid . column ) . toBeCloseTo ( 1 ) ;
39
+ } ) ;
You can’t perform that action at this time.
0 commit comments