Using a MOG background subtractor

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 ...

Get Learning OpenCV 4 Computer Vision with Python 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.