public static List < List < Point2D_I32 > > labelToClusters ( GrayS32 labelImage , int numLabels , FastQueue < Point2D_I32 > queue ) { List < List < Point2D_I32 >> ret = new ArrayList <> ( ) ; for ( int i = 0 ; i < numLabels + 1 ; i ++ ) { ret . add ( new ArrayList < Point2D_I32 > ( ) ) ; } if ( queue == null ) { queue = new FastQueue <> ( numLabels , Point2D_I32 . class , true ) ; } else queue . reset ( ) ; for ( int y = 0 ; y < labelImage . height ; y ++ ) { int start = labelImage . startIndex + y * labelImage . stride ; int end = start + labelImage . width ; for ( int index = start ; index < end ; index ++ ) { int v = labelImage . data [ index ] ; if ( v > 0 ) { Point2D_I32 p = queue . grow ( ) ; p . set ( index - start , y ) ; ret . get ( v ) . add ( p ) ; } } } // first list is a place holder and should be empty if ( ret . get ( 0 ) . size ( ) != 0 ) throw new RuntimeException ( "BUG!" ) ; ret . remove ( 0 ) ; return ret ; }
Scans through the labeled image and adds the coordinate of each pixel that has been labeled to a list specific to its label .