BackgroundSubtractionMOG2 + Mean-Shift Tracking

asked 2017-04-25 00:13:15 -0500

Marciano Ng gravatar image

I have searched multiple tutorials on how to track objects coming in and out of the frame but so far i have only been able to

  1. BackgroundSubtractionMOG2
  2. Draw rectangles on the white parts

However i cant seem to make the program to track the the object instead of drawing a new rectangle every new frame in need it to recognize it from the previous frame so i have decided to use Mean-Shift but i cant seem to understand how to use it with MOG2.

Please help me

I am currently using Python 3.4.0, OpenCV 3.1 and Numpy
This is my current code is below:

import sys
import numpy as np
import cv2
import imutils
from imutils import contours
import datetime
import time

cap = cv2.VideoCapture("/home/pi/Desktop/Proj/VideoTestSample.mp4")
cap = cv2.VideoCapture(0)

fgbg = cv2.createBackgroundSubtractorMOG2()

while (cap.isOpened()):
    (grabbed, frame) =
    text = " "

    if not grabbed:

    frame = imutils.resize(frame, width=500)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.medianBlur(gray, 5)

    fgmask = fgbg.apply(gray)
    thresh = cv2.erode(fgmask, None, iterations=2)
    (_,cnts,hierarchy) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for (i,c) in enumerate(cnts):
        if cv2.contourArea(c) < 300:

        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 2)
        cv2.putText(frame, "#{}".format(i + 1), (x, y - 15),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)        
        text = "REC"

    cv2.putText(frame, "{}". format(text), (10,20),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

    cv2.putText(frame,"%A %d %B %Y %I:%M:%S%p"),
                (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35,(0,0,255), 1)

    cv2.imshow('gray', gray)
    cv2.imshow('fgmask', fgmask)

    if cv2.waitKey(1) & 0xFF == ord('q'):

edit retag flag offensive close merge delete