People Counter 4 – Background Susbtraction

This part of the tutorial is also very simple to do, thanks to OpenCV.

A background subtractor, as its name sugests, lets you identify the foreground and background of and image. A background is considered to be as anything constant in a series of images, anything that stays static. The foreground is everything that changes (moves).

Doing background substraction in OpenCV onlyt requires 2 lines:

import numpy as np
import cv2

cap = cv2.VideoCapture('peopleCounter.avi') #Open video file

fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = True) #Create the background substractor

while(cap.isOpened()):
    ret, frame = cap.read() #read a frame
    
    fgmask = fgbg.apply(frame) #Use the substractor
    
    try:        
        cv2.imshow('Frame',frame)
        cv2.imshow('Background Substraction',fgmask)
    except:
        #if there are no more frames to show...
        print('EOF')
        break
    
    #Abort and exit with 'Q' or ESC
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release() #release video file
cv2.destroyAllWindows() #close all openCV windows

 

Running this code:

bsubs

In the new image black represents the background, white are objects in the foreground and gray are shadows cast by those objects.

The good thing about using the MOG2 substractor in OpenCV is that the background is constantly being calculated, meaning that subtle changes in lighting (such as those caused by the Sun) won´t affect your calculations over time.

This is really the first step in making a people counter. Hope you like it.

Next, we’ll clean the image produced by the substractor to be able to use it in the actual counting.

25 thoughts on “People Counter 4 – Background Susbtraction”

      1. Thanks for your reply. I followed your step and I tried myself and works well. I believe you will update your post and I am very looking forward to it. Since waiting another post I just want to test my idea with your video file(People Counter 2-about board on bus). Would you kindly send me the video file to yoosh22@nate.com Many thanks.

  1. Hi! I have a problem with createBackgroundSubtractorMOG2 method. It shows me the black screen for threshold preview. No mask.

    Did It work for you out of the box just like that?

  2. When trying to run the example, I got this error message:

    Traceback (most recent call last):
    File “C:\python-2.7.12.amd64\mejia\backgroundSubstraction.py”, line 12, in
    fgmask = fgbg.apply(frame) #Use the substractor
    error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\python\src2\cv2.cpp:163: error: (-215) The data should normally be NULL! in function NumpyAllocator::allocate

    I found a way to solve (not the best one), inserting:

    cv2.ocl.setUseOpenCL(False)

    after “import cv2”, and it ran succesfully.

    Cuando intenté ejecutar el ejemplo, recibí este mensaje de error:

    Traceback (most recent call last):
    File “C:\python-2.7.12.amd64\mejia\backgroundSubstraction.py”, line 12, in
    fgmask = fgbg.apply(frame) #Use the substractor
    error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\python\src2\cv2.cpp:163: error: (-215) The data should normally be NULL! in function NumpyAllocator::allocate

    Encontré una forma de resolver (no la mejor), insertando:

    cv2.ocl.setUseOpenCL(False)

    después de “import cv2” y se ejecutó con éxito.

    Best regards / Saludos cordiales desde Chile

  3. Pingback: Buy viagra online
  4. Pingback: Cialis from canada
  5. Pingback: Buy generic viagra
  6. Pingback: Cialis 20 mg
  7. Pingback: Generic cialis
  8. Pingback: Discount cialis
  9. Pingback: Viagra uk

Leave a Reply