
The image frameForeground then marks candidate foreground objects as 255 and background pixels
as 0. We need to clean up small noise areas as discussed earlier; we might do this with cv::erode()
followed by cv::dilate()or by using connected components. For color images, we could use the same
code for each color channel and then combine the channels with the cv::max() function. This method is
much too simple for most applications other than merely indicating regions of motion. For a more effective
background model we need to keep some statistics about the means and average differences of pixels in the
scene. You can look ahead to the section entitled “
XA Quick X” to see examples of frame differencing in
X
Figure 9-6
X and X
Figure 9-7X.
3BAveraging Background Method
The averaging method basically learns the average and standard deviation (or similarly, but
computationally faster, the average difference) of each pixel as its model of the background.
Consider the pixel line from
XFigure 9-1X. Instead of plotting one sequence of values for each frame (as we
did in that figure), we can represent the variations of each pixel throughout the video in terms of an average
value and a pixel’s associated average differences (
XFigure 9-2X). In the same video, a foreground object