@ Override public void process ( T image ) { // initialize data structures this . image = image ; this . stopRequested = false ; modeLocation . reset ( ) ; modeColor . reset ( ) ; modeMemberCount . reset ( ) ; interpolate . setImage ( image ) ; pixelToMode . reshape ( image . width , image . height ) ; quickMode . reshape ( image . width , image . height ) ; // mark as -1 so it knows which pixels have been assigned a mode already and can skip them ImageMiscOps . fill ( pixelToMode , - 1 ) ; // mark all pixels are not being a mode ImageMiscOps . fill ( quickMode , - 1 ) ; // use mean shift to find the peak of each pixel in the image int indexImg = 0 ; for ( int y = 0 ; y < image . height && ! stopRequested ; y ++ ) { for ( int x = 0 ; x < image . width ; x ++ , indexImg ++ ) { if ( pixelToMode . data [ indexImg ] != - 1 ) { int peakIndex = pixelToMode . data [ indexImg ] ; modeMemberCount . data [ peakIndex ] ++ ; continue ; } float meanColor = interpolate . get ( x , y ) ; findPeak ( x , y , meanColor ) ; // convert mean-shift location into pixel index int modeX = ( int ) ( this . modeX + 0.5f ) ; int modeY = ( int ) ( this . modeY + 0.5f ) ; int modePixelIndex = modeY * image . width + modeX ; // get index in the list of peaks int modeIndex = quickMode . data [ modePixelIndex ] ; // If the mode is new add it to the list if ( modeIndex < 0 ) { modeIndex = this . modeLocation . size ( ) ; this . modeLocation . grow ( ) . set ( modeX , modeY ) ; // Save the peak's color modeColor . grow ( ) [ 0 ] = meanGray ; // Mark the mode in the segment image quickMode . data [ modePixelIndex ] = modeIndex ; // Set the initial count to zero. This will be incremented when it is traversed later on modeMemberCount . add ( 0 ) ; } // add this pixel to the membership list modeMemberCount . data [ modeIndex ] ++ ; // Add all pixels it traversed through to the membership of this mode // This is an approximate of mean-shift for ( int i = 0 ; i < history . size ; i ++ ) { Point2D_F32 p = history . get ( i ) ; int px = ( int ) ( p . x + 0.5f ) ; int py = ( int ) ( p . y + 0.5f ) ; int index = pixelToMode . getIndex ( px , py ) ; if ( pixelToMode . data [ index ] == - 1 ) { pixelToMode . data [ index ] = modeIndex ; } } } } }
Performs mean - shift clustering on the input image