O'Reilly logo

Learning OpenCV 3 Computer Vision with Python - Second Edition by Joe Minichino

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Background subtractors – KNN, MOG2, and GMG

OpenCV provides a class called BackgroundSubtractor, which is a handy way to operate foreground and background segmentation.

This works similarly to the GrabCut algorithm we analyzed in Chapter 3, Processing Images with OpenCV 3, however, BackgroundSubtractor is a fully fledged class with a plethora of methods that not only perform background subtraction, but also improve background detection in time through machine learning and lets you save the classifier to a file.

To familiarize ourselves with BackgroundSubtractor, let's look at a basic example:

import numpy as np import cv2 cap = cv2.VideoCapture') mog = cv2.createBackgroundSubtractorMOG2() while(1): ret, frame = cap.read() fgmask = mog.apply(frame) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required