Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning OpenCV 3 Computer Vision with Python (Update)

You're reading from   Learning OpenCV 3 Computer Vision with Python (Update) Unleash the power of computer vision with Python using OpenCV

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781785283840
Length 266 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Setting Up OpenCV FREE CHAPTER 2. Handling Files, Cameras, and GUIs 3. Processing Images with OpenCV 3 4. Depth Estimation and Segmentation 5. Detecting and Recognizing Faces 6. Retrieving Images and Searching Using Image Descriptors 7. Detecting and Recognizing Objects 8. Tracking Objects 9. Neural Networks with OpenCV – an Introduction Index

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)
    cv2.imshow('frame',fgmask)
    if cv2.waitKey(30) & 0xff:
        break

cap.release()
cv2.destroyAllWindows()

Let's go through this in order. First of all, let's talk about the background subtractor object. There are three background...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime