Skip to content

Commit

Permalink
mog
Browse files Browse the repository at this point in the history
  • Loading branch information
techfort committed Jun 2, 2015
1 parent d6b3886 commit fac530c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 0 additions & 2 deletions chapter8/basic_motion_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion chapter8/knn.py
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
12 changes: 11 additions & 1 deletion chapter8/mog.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit fac530c

Please sign in to comment.