-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlucas_kanade_2d.py
140 lines (105 loc) · 3.81 KB
/
lucas_kanade_2d.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import sys
import cv2
import numpy as np
from numba import jit
import ic_utils as ic
@jit
def compute_derivatives(T):
Tx = np.zeros_like(T)
Ty = np.zeros_like(T)
TxTx = np.zeros_like(T)
TyTy = np.zeros_like(T)
TxTy = np.zeros_like(T)
H = np.zeros((2, 2), dtype=T.dtype)
theight, twidth = T.shape
for j in range(1, theight - 1):
for i in range(1, twidth - 1):
Tx[j, i] = (T[j, i + 1] - T[j, i - 1]) / 2
Ty[j, i] = (T[j + 1, i] - T[j - 1, i]) / 2
TxTx[j, i] = Tx[j, i] * Tx[j, i]
TyTy[j, i] = Ty[j, i] * Ty[j, i]
TxTy[j, i] = Tx[j, i] * Ty[j, i]
H[0, 0] += TxTx[j, i]
H[1, 1] += TyTy[j, i]
H[0, 1] += TxTy[j, i]
H[1, 0] = H[0, 1]
return Tx, Ty, H
@jit
def compute_Jt_err(Ip, T, Tx, Ty):
err = np.zeros_like(T)
Tx_err = np.zeros_like(T)
Ty_err = np.zeros_like(T)
Jt_err = np.zeros((2,), dtype=T.dtype)
theight, twidth = T.shape
for j in range(1, theight - 1):
for i in range(1, twidth - 1):
err[j, i] = Ip[j, i] - T[j, i]
Tx_err[j, i] = Tx[j, i] * err[j, i]
Ty_err[j, i] = Ty[j, i] * err[j, i]
Jt_err[0] += Tx_err[j, i]
Jt_err[1] += Ty_err[j, i]
return Jt_err
def match_template_lk(image, current_center, T, Tx, Ty, JtJ, max_iter=50):
theight, twidth = T.shape
for iter in range(max_iter):
Ip = cv2.getRectSubPix(image, (twidth, theight), current_center)
Ip = np.float32(Ip)
Jt_err = compute_Jt_err(Ip, T, Tx, Ty)
dp = np.linalg.solve(JtJ, Jt_err)
current_center = (current_center[0] - dp[0], current_center[1] - dp[1])
if np.linalg.norm(dp) < 0.1:
break
return current_center
def main():
cap = ic.select_capture_source(sys.argv)
mstate = {
'selection': 'invalid',
'xybegin': (-1, -1),
'xyend': (-1, -1),
}
cv2.namedWindow('track')
cv2.setMouseCallback('track', ic.on_mouse_rect, mstate)
target = None
while True:
grabbed, frame_color = cap.read()
if not grabbed:
break
frame = cv2.cvtColor(frame_color, cv2.COLOR_BGR2GRAY)
if mstate['selection'] == 'valid' and target is None:
## initialization requested
xybegin = np.array(mstate['xybegin'])
xyend = np.array(mstate['xyend'])
current_center = np.int16((xybegin + xyend) / 2)
tsize = xyend - xybegin ## == (width, height)
target = cv2.getRectSubPix(frame, tsize, current_center)
T = np.float32(target)
Tx, Ty, JtJ = compute_derivatives(T)
cv2.imshow('template', target)
if mstate['selection'] == 'valid':
assert target is not None
assert 'current_center' in locals()
assert 'T' in locals()
assert 'Tx' in locals()
assert 'Ty' in locals()
assert 'JtJ' in locals()
assert 'tsize' in locals()
current_center = match_template_lk(frame, current_center,
T, Tx, Ty, JtJ)
cxybegin = np.int16(current_center - tsize / 2)
cxyend = np.int16(current_center + tsize / 2)
cv2.rectangle(frame_color, cxybegin, cxyend, (0, 0, 255), 3)
elif mstate['selection'] == 'ongoing':
target = None
xybegin = mstate['xybegin']
xyend = mstate['xyend']
cv2.rectangle(frame_color, xybegin, xyend, (255, 0, 0), 2)
elif mstate['selection'] == 'invalid':
target = None
cv2.imshow('track', frame_color)
key = cv2.waitKey(30)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()