-
Notifications
You must be signed in to change notification settings - Fork 4
/
VideoDiff.py
40 lines (26 loc) · 969 Bytes
/
VideoDiff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
__author__ = 'dfrysing'
import cv2
import numpy
import time
cv2.namedWindow("My Window")
video = cv2.VideoCapture('P1_4.m4v')
successFlag, frame = video.read()
lastFrame = frame.copy()
h, w = frame.shape[:2]
motionHistory = numpy.zeros((h, w), numpy.float32)
while 1:
successFlag, frame = video.read()
if not successFlag:
break
frameDiff = cv2.absdiff(lastFrame, frame)
greyDiff = cv2.cvtColor(frameDiff, code=cv2.COLOR_BGR2GRAY)
retval, motionMask = cv2.threshold(greyDiff,10,1,cv2.THRESH_BINARY)
timestamp = time.clock()
cv2.updateMotionHistory(motionMask, motionHistory, timestamp, 0.5)
mg_mask, mg_orient = cv2.calcMotionGradient( motionHistory, 0.25, 0.05, apertureSize=5 )
seg_mask, seg_bounds = cv2.segmentMotion(motionHistory, timestamp, 0.25)
total = sum(sum(motionHistory))/8
print "movement: ", total
cv2.imshow("My Window", motionHistory)
cv2.waitKey(1)
lastFrame = frame.copy()