Skip to content

Instantly share code, notes, and snippets.

@tstellanova
Created October 24, 2013 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstellanova/7131798 to your computer and use it in GitHub Desktop.
Save tstellanova/7131798 to your computer and use it in GitHub Desktop.
opencv cv2 BackgroundSubtractor
import cv2
 
backsub = cv2.BackgroundSubtractorMOG()
 
capture = cv2.VideoCapture("Balcony4_Vis.mpg")
 
if capture:
    while True:
        ret, frame = capture.read()
        if ret:
            fgmask = backsub.apply(frame, None, 0.01)
            contours, hierarchy = cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL,
                                               cv2.CHAIN_APPROX_NONE)
            try: hierarchy = hierarchy[0]
            except: hierarchy = []
            for contour, hier in zip(contours, hierarchy):
                (x,y,w,h) = cv2.boundingRect(contour)
                if w > 10 and h > 10:
                    # figure out id
                    best_id = 1
                    .....
 
                    cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2)
                    cv2.putText(frame, str(best_id), (x,y-5), cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (255, 0, 0), 2)
            cv2.imshow("Track", frame)
 
            key = cv2.waitKey(10)
            if key == ord('q'):
                break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment