OpenCV provides a class called cv2.BackgroundSubtractor, which has various subclasses that implement various background subtraction algorithms.
You may recall that we previously used OpenCV's implementation of the GrabCut algorithm to perform foreground/background segmentation in Chapter 4, Depth Estimation and Segmentation, specifically in the Foreground detection with the GrabCut algorithm section. Like cv2.grabCut, the various subclass implementations of cv2.BackgroundSubtractor can produce a mask that assigns different values to different segments of the image. Specifically, a background subtractor can mark foreground segments as white (that is, an 8-bit grayscale value of 255), background segments as ...