-
Notifications
You must be signed in to change notification settings - Fork 4
/
optical_flow.py
53 lines (40 loc) · 1.46 KB
/
optical_flow.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
"""
THis simple program allows to select a point, then track its motion/optical flow. Press escape to stop.
"""
import cv2
import numpy as np
cap = cv2.VideoCapture("C:/Users/Home/Downloads/vid.mp4") # just change the path
_, old_frame = cap.read()
old_frame_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
lk_params = dict(
winSize = (10, 10),
maxLevel = 4,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)
)
point_selected = False
mask = np.zeros_like(old_frame) # empty mask for drawing line
# to return point to track from
def point(event,x,y,flags,params):
global old_points, points, point_selected
if event == cv2.EVENT_LBUTTONDOWN:
points = (x,y)
point_selected = True
old_points = np.array([[x,y]], dtype=np.float32)
cv2.namedWindow("Select point")
cv2.setMouseCallback("Select point", point)
while 1:
_ , frame = cap.read()
new_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if point_selected:
current_points, status, error = cv2.calcOpticalFlowPyrLK(old_frame_gray, new_frame, old_points, None, **lk_params)
x, y = current_points.ravel()
mask = cv2.line(mask, points, (x,y), [0,0,255], 1)
old_frame_gray = new_frame.copy()
old_points = current_points
frame = cv2.add(frame, mask) # add to original frame
cv2.imshow("Select point", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()