From fac530ceee7bfeb1f9465e0af279241ad5050c11 Mon Sep 17 00:00:00 2001 From: Joe Minichino Date: Tue, 2 Jun 2015 19:10:05 +0100 Subject: [PATCH] mog --- chapter8/basic_motion_detection.py | 2 -- chapter8/knn.py | 2 +- chapter8/mog.py | 12 +++++++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/chapter8/basic_motion_detection.py b/chapter8/basic_motion_detection.py index 7c9a466..36ee626 100644 --- a/chapter8/basic_motion_detection.py +++ b/chapter8/basic_motion_detection.py @@ -16,11 +16,9 @@ gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray_frame = cv2.GaussianBlur(gray_frame, (21, 21), 0) - diff = cv2.absdiff(background, gray_frame) diff = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1] diff = cv2.dilate(diff, es, iterations = 2) - image, cnts, hierarchy = cv2.findContours(diff.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in cnts: diff --git a/chapter8/knn.py b/chapter8/knn.py index 015fa3a..22c6671 100644 --- a/chapter8/knn.py +++ b/chapter8/knn.py @@ -1,7 +1,7 @@ import cv2 import numpy as np -knn = cv2.createBackgroundSubtractorKNN(detectShadows=False) +knn = cv2.createBackgroundSubtractorKNN(detectShadows = True) es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (20,12)) camera = cv2.VideoCapture("/home/d3athmast3r/Downloads/traffic.flv") diff --git a/chapter8/mog.py b/chapter8/mog.py index b9aa7aa..4566cf3 100644 --- a/chapter8/mog.py +++ b/chapter8/mog.py @@ -1,15 +1,25 @@ import cv2 import numpy as np -bs = cv2.createBackgroundSubtractorKNN() +bs = cv2.createBackgroundSubtractorKNN(detectShadows = True) camera = cv2.VideoCapture("/home/d3athmast3r/Downloads/traffic.flv") while True: ret, frame = camera.read() fgmask = bs.apply(frame) + th = cv2.threshold(fgmask.copy(), 244, 255, cv2.THRESH_BINARY)[1] + dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3)), iterations = 2) + image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + for c in contours: + if cv2.contourArea(c) > 1600: + (x,y,w,h) = cv2.boundingRect(c) + cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 255, 0), 2) + cv2.imshow("mog", fgmask) + cv2.imshow("thresh", th) cv2.imshow("diff", frame & cv2.cvtColor(fgmask, cv2.COLOR_GRAY2BGR)) + cv2.imshow("detection", frame) k = cv2.waitKey(30) & 0xff if k == 27: break