protected void markMergeRegions ( FastQueue < float [ ] > regionColor , FastQueue < Point2D_I32 > modeLocation , GrayS32 pixelToRegion ) { for ( int targetId = 0 ; targetId < modeLocation . size && ! stopRequested ; targetId ++ ) { float [ ] color = regionColor . get ( targetId ) ; Point2D_I32 location = modeLocation . get ( targetId ) ; int x0 = location . x - searchRadius ; int x1 = location . x + searchRadius + 1 ; int y0 = location . y - searchRadius ; int y1 = location . y + searchRadius + 1 ; // ensure that all pixels it examines are inside the image if ( x0 < 0 ) x0 = 0 ; if ( x1 > pixelToRegion . width ) x1 = pixelToRegion . width ; if ( y0 < 0 ) y0 = 0 ; if ( y1 > pixelToRegion . height ) y1 = pixelToRegion . height ; // look at the local neighborhood for ( int y = y0 ; y < y1 ; y ++ ) { for ( int x = x0 ; x < x1 ; x ++ ) { int candidateId = pixelToRegion . unsafe_get ( x , y ) ; // see if it is the same region if ( candidateId == targetId ) continue ; // see if the mode is near by Point2D_I32 p = modeLocation . get ( candidateId ) ; if ( p . distance2 ( location ) <= maxSpacialDistanceSq ) { // see if the color is similar float [ ] candidateColor = regionColor . get ( candidateId ) ; float colorDistance = SegmentMeanShiftSearch . distanceSq ( color , candidateColor ) ; if ( colorDistance <= maxColorDistanceSq ) { // mark the two regions as merged markMerge ( targetId , candidateId ) ; } } } } } }
Takes the mode of a region and searches the local area around it for other regions . If the region s mode is also within the local area its color is checked to see if it s similar enough . If the color is similar enough then the two regions are marked for merger .