int width; // width of the image int height; // height of the image byte image[width*height]; // current image byte segmentationMap[width*height]; // classification result int numberOfSamples = 20; // number of samples per pixel int requiredMatches = 2; // #_min int distanceThreshold = 20; byte samples[width*height][numberOfSamples]; // background model for (int p = 0; p < width*height; p++) { int count=0, index=0, distance=0; // counts the matches and stops when requiredMatches are found while ( (count < requiredMatches) && (index < numberOfSamples) ) { distance = getDistanceBetween(image[p], samples[p][index]); if (distance < distanceThreshold) { count++; } index++; } // pixel classification if (count < requiredMatches) segmentationMap[p] = FOREGROUND_LABEL; else segmentationMap[p] = BACKGROUND_LABEL; }