protected int computeNearestCentroid ( double [ ] descriptor ) { int centroidIndex = - 1 ; double minDistance = Double . MAX_VALUE ; for ( int i = 0 ; i < numCentroids ; i ++ ) { double distance = 0 ; for ( int j = 0 ; j < descriptorLength ; j ++ ) { distance += ( codebook [ i ] [ j ] - descriptor [ j ] ) * ( codebook [ i ] [ j ] - descriptor [ j ] ) ; // when distance becomes greater than minDistance // break the inner loop and check the next centroid!!! if ( distance >= minDistance ) { break ; } } if ( distance < minDistance ) { minDistance = distance ; centroidIndex = i ; } } return centroidIndex ; }
Returns the index of the centroid which is closer to the given descriptor .