public void assignLabelsToPixels ( GrayS32 pixelToRegions , GrowQueue_I32 regionMemberCount , FastQueue < float [ ] > regionColor ) { regionColor . reset ( ) ; for ( int i = 0 ; i < clusters . size ( ) ; i ++ ) { float [ ] r = regionColor . grow ( ) ; float [ ] c = clusters . get ( i ) . color ; for ( int j = 0 ; j < numBands ; j ++ ) { r [ j ] = c [ j ] ; } } regionMemberCount . resize ( clusters . size ( ) ) ; regionMemberCount . fill ( 0 ) ; int indexPixel = 0 ; for ( int y = 0 ; y < pixelToRegions . height ; y ++ ) { int indexOutput = pixelToRegions . startIndex + y * pixelToRegions . stride ; for ( int x = 0 ; x < pixelToRegions . width ; x ++ , indexPixel ++ , indexOutput ++ ) { Pixel p = pixels . data [ indexPixel ] ; // It is possible for a pixel to be unassigned if all the means move too far away from it // Default to a non-existant cluster if that's the case int best = - 1 ; float bestDistance = Float . MAX_VALUE ; // find the region/cluster which it is closest to for ( int j = 0 ; j < p . clusters . size ; j ++ ) { ClusterDistance d = p . clusters . data [ j ] ; if ( d . distance < bestDistance ) { bestDistance = d . distance ; best = d . cluster . id ; } } if ( best == - 1 ) { regionColor . grow ( ) ; best = regionMemberCount . size ( ) ; regionMemberCount . add ( 0 ) ; } pixelToRegions . data [ indexOutput ] = best ; regionMemberCount . data [ best ] ++ ; } } }
Selects which region each pixel belongs to based on which cluster it is the closest to