
16BBackground differencing: Finding foreground objects
We’ve seen how to create a background codebook model and how to clear it of seldom-used entries. Next
we turn to background_diff(), where we use the learned model to segment foreground pixels from
the previously learned background:
// Given a pixel and a codebook, determine if the pixel is
// covered by the codebook
//
// NOTES:
// minMod and maxMod must have length numChannels,
// e.g. 3 channels => minMod[3], maxMod[3]. There is one min and
// one max threshold per channel.
//
uchar backgroundDiff( // return 0 => background, 255 => foreground
cv::Vec3b& p, // Pixel (YUV)
CodeBook& c, // Codebook
int numChannels, // Number of channels we are testing
int* minMod, // Add this (possibly negative) number onto max level
// when determining if new pixel is foreground
int* maxMod // Subtract this (possibly negative) number from min level
// when determining if new pixel is foreground
) {
int matchChannel;
// SEE IF THIS FITS AN EXISTING CODEWORD
//
for( int i=0; i<c.size(); i++ ) {
matchChannel = 0;
for( int n=0; n<numChannels; n++ ) {
if(
(c[i]->min[n] - minMod[n] <= p[n] ) && (p[n] <= c[i]->max[n] + maxMod[n])
) {
matchChannel++; // Found an entry for this channel
} else {
break;
}
}
if(matchChannel == numChannels) {
break; // Found an entry that matched all channels
}
}
if( i >= c.size() ) return ...