-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptical_flow_sparse2.py
76 lines (43 loc) · 1.65 KB
/
optical_flow_sparse2.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
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ok, frame = cap.read()
frame_gray_init = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
parameters_lucas_kanade = dict(winSize=(15, 15), maxLevel=4,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
def select_point(event, x, y, flags, params):
global point, selected_point, old_points
if event == cv2.EVENT_LBUTTONDOWN:
point = (x, y)
selected_point = True
old_points = np.array([[x, y]], dtype=np.float32)
cv2.namedWindow('Frame')
cv2.setMouseCallback('Frame', select_point)
selected_point = False
point = ()
old_points = np.array([[]])
mask = np.zeros_like(frame)
while True:
ok, frame = cap.read()
if not ok:
break
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if selected_point is True:
cv2.circle(frame, point, 5, (0, 0, 255), 2)
new_points, status, errors = cv2.calcOpticalFlowPyrLK(frame_gray_init, frame_gray,
old_points, None,
**parameters_lucas_kanade)
frame_gray_init = frame_gray.copy()
old_points = new_points
x, y = new_points.ravel()
j, k = old_points.ravel()
mask = cv2.line(mask, (int(x), int(y)), (int(j), int(k)), (0, 255, 255), 2)
frame = cv2.circle(frame, (int(x), int(y)), 5, (0, 255, 0), -1)
img = cv2.add(frame, mask)
cv2.imshow("Frame", img)
cv2.imshow("Frame 2", mask)
key = cv2.waitKey(1)
if key == 27: # esc
break
cap.release()
cv2.destroyAllWindows()