-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion_tracking.py
59 lines (35 loc) · 1.2 KB
/
motion_tracking.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import cv2
import numpy as np
def main():
w = 800
h = 600
cap = cv2.VideoCapture(0)
cap.set(3, w)
cap.set(4, h)
# print(cap.get(3))
# print(cap.get(4))
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
ret, frame1 = cap.read()
ret, frame2 = cap.read()
while ret:
d = cv2.absdiff(frame1, frame2)
grey = cv2.cvtColor(d, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(grey, (5, 5), 0)
ret, th = cv2.threshold( blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(th, np.ones((8, 8), np.uint8), iterations=1 )
eroded = cv2.erode(dilated, np.ones((3, 3), np.uint8), iterations=1 )
img, c, h = cv2.findContours(eroded, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame1, c, -1, (0, 0, 255), 2)
cv2.imshow("Original", frame2)
cv2.imshow("Output", frame1)
if cv2.waitKey(1) == 27: # exit on ESC
break
frame1 = frame2
ret, frame2 = cap.read()
cv2.destroyAllWindows()
cap.release()
if __name__ == "__main__":
main()