-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dense Model.py
103 lines (82 loc) · 2.55 KB
/
Dense Model.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Importing the required Modules
"""
import cv2
import numpy as np
"""
Get a VideoCapture object from video and store it in cap
"""
cap = cv2.VideoCapture("Hallway 2.avi")
"""
Read first frame, then Scale and resize image
"""
ret, first_frame = cap.read()
resize_dim = 600
max_dim = max(first_frame.shape)
scale = resize_dim/max_dim
first_frame = cv2.resize(first_frame, None, fx=scale, fy=scale)
"""
Convert to gray scale and Create mask
"""
prev_gray = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)
mask = np.zeros_like(first_frame)
"""
Sets image saturation to maximum
"""
mask[..., 1] = 255
out = cv2.VideoWriter('video.mp4',-1,1,(600, 600))
while(cap.isOpened()):
"""
Frame-by-frame reading of the loaded video
"""
ret, frame = cap.read()
"""
Convert new frame format`s to gray scale and resize gray frame obtained
"""
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.resize(gray, None, fx=scale, fy=scale)
"""
Calculate dense optical flow by Farneback method
Ref: https://docs.opencv.org/3.0-beta/modules/video/doc/motion_analysis_and_object_tracking.html#calcopticalflowfarneback
"""
flow = cv2.calcOpticalFlowFarneback(prev_gray, gray, None, pyr_scale = 0.5, levels = 5, winsize = 11, iterations = 5, poly_n = 5, poly_sigma = 1.1, flags = 0)
"""
Compute the magnitude and angle of the 2D vectors
"""
magnitude, angle = cv2.cartToPolar(flow[..., 0], flow[..., 1])
"""
Setting image hue according to the optical flow direction
"""
mask[..., 0] = angle * 180 / np.pi / 2
"""
Setting image value according to the optical flow magnitude (normalized)
"""
mask[..., 2] = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX)
"""
Convert HSV to RGB (BGR) color representation
"""
rgb = cv2.cvtColor(mask, cv2.COLOR_HSV2BGR)
"""
Resize frame size to match dimensions
"""
frame = cv2.resize(frame, None, fx=scale, fy=scale)
"""
Open a new window and displays the output frame
"""
dense_flow = cv2.addWeighted(frame, 1,rgb, 2, 0)
cv2.imshow("Dense Model of optical flow", dense_flow)
out.write(dense_flow)
"""
Update previous frame
"""
prev_gray = gray
"""
Frame are read by intervals of 1 millisecond. The programs breaks out of the while loop when the user presses the 'q' key
"""
if cv2.waitKey(10) & 0xFF == ord('q'):
break
"""
Releasing the Cap and destroying all windows
"""
cap.release()
cv2.destroyAllWindows()