-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKalmanTracker.py
More file actions
213 lines (170 loc) · 7.18 KB
/
Copy pathKalmanTracker.py
File metadata and controls
213 lines (170 loc) · 7.18 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import numpy as np
import cv2
import os
import ReadCyanSystemsVideo as cyanSystem
import TrackingUtility as tracking
from KalmanFilter import KalmanFilter
#sequence_list = cyanSystem.GetSequenceList()
#sequence_list = cyanSystem.GetRandomSequence()
sequence_list = cyanSystem.GetSequence(2)
sequenceDir = '/data1/TwoColorTracking/sequences'
morphKernel = np.ones((5,5), np.uint8)
smoothingKernel = np.ones((3,3), np.float32)/9.
minValidRegionArea = 10
# Assume the constant velocity model
delta_t = 1;
F = np.array([[1, delta_t, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, delta_t, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]])
H = np.array([[1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]])
# noise variance
Q = .5
R = 2.0
varThresh1=32.0
varThresh2=32.0
gamma_lw=0.9
gamma_mw=0.01
print sequence_list
for sequence in sequence_list:
sequenceName = sequenceDir + '/' + sequence['name']
# create sequence directory
if not os.path.exists(str(sequence['ref'])):
os.makedirs(str(sequence['ref']))
lw, mw = cyanSystem.ReadCyanSystemsTwoColorVideo(sequenceName, crop=True)
lw_max = np.amax(lw)
mw_max = np.amax(mw)
lw_min = np.amin(lw)
mw_min = np.amin(mw)
lw_range = np.amax(lw) - np.amin(lw)
mw_range = np.amax(mw) - np.amin(mw)
sequenceLen = sequence['end_frame']
historyFrames = sequence['start_frame']-1
lw_bg, mw_bg = tracking.BuildBackgroundModels(lw, mw, historyFrames, \
varThresh1, varThresh2, \
False, gamma_lw, gamma_mw)
xhat_lw = np.zeros( (6, sequence['end_frame']-sequence['start_frame']+2) )
xhat_mw = np.zeros( (6, sequence['end_frame']-sequence['start_frame']+2) )
xhat_all = np.zeros( (6, sequence['end_frame']-sequence['start_frame']+2) )
xhat_lw[:,0] = [sequence['x_center'], 0.1, sequence['y_center'], 0.1, \
sequence['half_width']*2+1, sequence['half_height']*2+1]
Ppost_lw = np.zeros(F.shape)
xpred_lw = xhat_lw[:,0]
xhat_mw[:,0] = xhat_lw[:,0]
Ppost_mw = np.zeros(F.shape)
xpred_mw = xhat_mw[:,0]
xhat_all[:,0] = xhat_lw[:,0]
Ppost_all = np.zeros(F.shape)
xpred_all = xhat_all[:,0]
kalman = KalmanFilter(F, H, Q, R)
frameCounter = historyFrames
idx = 0
while True:
frameCounter = frameCounter + 1
idx = idx + 1
#print frameCounter
if frameCounter > sequenceLen:
break
lw_frame = cyanSystem.ScaleUint16To255(lw[frameCounter], \
lw_max, lw_min, gamma_lw)
mw_frame = cyanSystem.ScaleUint16To255(mw[frameCounter], \
mw_max, mw_min, gamma_mw)
lw_mask = tracking.GetBackgroundSubtraction(lw_frame, \
lw_bg, morphKernel)
mw_mask = tracking.GetBackgroundSubtraction(mw_frame, \
mw_bg, morphKernel)
lw_cts = tracking.GetContoursFromBackgroundMask(lw_mask.copy(), \
minValidRegionArea)
mw_cts = tracking.GetContoursFromBackgroundMask(mw_mask.copy(), \
minValidRegionArea)
#blah = np.asarray(np.dstack((lw_mask, lw_mask, lw_mask)),
#dtype=np.uint8)
#for c in lw_cts:
# rect = cv2.minAreaRect(c)
# box = np.int0(cv2.boxPoints(rect))
# cv2.drawContours(blah, [box], -1, (0, 255, 0), 2)
#print('Writing lw mask %d' % frameCounter)
#fileName = "%s/%05d_lw_mask.jpg" % (sequence['ref'], frameCounter)
#cv2.imwrite(fileName, blah)
#print('Begin filtering %d' % frameCounter)
#print('predicted')
# LW correspondent
lw_feat = tracking.GetTargetObservation(lw_cts, xpred_lw)
z_lw, z_lw_valid, err_lw = \
tracking.GetTargetCorrespondent(lw_feat, xpred_lw, 50., 20.)
# MW correspondent
mw_feat = tracking.GetTargetObservation(mw_cts, xpred_mw)
z_mw, z_mw_valid, error_mw = \
tracking.GetTargetCorrespondent(mw_feat, xpred_mw, 50., 20.)
# Two color
all_feat_lw = tracking.GetTargetObservation(lw_cts, xpred_all)
all_feat_mw = tracking.GetTargetObservation(mw_cts, xpred_all)
z_all, z_all_valid = \
tracking.GetTargetCorrespondentTwoColor(lw_feat, mw_feat, xpred_all)
if not z_all_valid:
print('Coasting %d, %d' % (frameCounter, z_all_valid))
#print z_lw_valid, z_mw_valid, z_valid
#print lw_z
#print xpred_all
#print mw_z
#print xpred_all
#print("Doing %d" % frameCounter)
#print("lw valid: %d" % (z_lw_valid))
#print z_lw
#print("mw valid: %d" % (z_mw_valid))
#print z_mw
#print("all valid: %d" % (z_all_valid))
#print z_all
#if not z_lw_valid and not z_mw_valid:
# print "Warning: invalid Z"
#print('z')
#print lw_z, mw_z
#break
#print xpred
#print Ppost
#print lw_z
xhat_lw[:,idx], Ppost_lw = \
kalman.filter(xpred_lw, Ppost_lw, z_lw, z_lw_valid)
xpred_lw = xhat_lw[:,idx]
xhat_mw[:,idx], Ppost_mw = \
kalman.filter(xpred_mw, Ppost_mw, z_mw, z_mw_valid)
xpred_mw = xhat_mw[:,idx]
xhat_all[:,idx], Ppost_all = \
kalman.filter(xpred_all, Ppost_all, z_all, z_all_valid)
xpred_all = xhat_all[:,idx]
#print xhat_lw[:,idx], xhat_mw[:,idx]
#rect = ((xpred[0] - xpred[4]*0.5, xpred[2] - xpred[5]*0.5), (xpred[4], xpred[5]), 0.0)
rect = ((xpred_all[0], xpred_all[2]), (xpred_all[4], xpred_all[5]), 0.0)
box = np.int0(cv2.boxPoints(rect))
rect_lw = ((xpred_lw[0], xpred_lw[2]), (xpred_lw[4], xpred_lw[5]), 0.0)
box_lw = np.int0(cv2.boxPoints(rect_lw))
rect_mw = ((xpred_mw[0], xpred_mw[2]), (xpred_mw[4], xpred_mw[5]), 0.0)
box_mw = np.int0(cv2.boxPoints(rect_mw))
lw_rgb = cyanSystem.to_rgb(lw_frame)
#print lw_rgb.shape, lw_rgb.dtype
cv2.drawContours(lw_rgb, [box], -1, (255, 0, 0), 1)
cv2.drawContours(lw_rgb, [box_lw], -1, (0, 255, 0), 1)
cv2.drawContours(lw_rgb, [box_mw], -1, (0, 0, 255), 1)
mw_rgb = cyanSystem.to_rgb(mw_frame)
cv2.drawContours(mw_rgb, [box], -1, (255, 0, 0), 1)
cv2.drawContours(mw_rgb, [box_lw], -1, (0, 255, 0), 1)
cv2.drawContours(mw_rgb, [box_mw], -1, (0, 0, 255), 1)
lw_mask_rgb = cyanSystem.to_rgb(lw_mask)
mw_mask_rgb = cyanSystem.to_rgb(mw_mask)
upper_frame = np.concatenate( (lw_rgb, mw_rgb), axis=1)
lower_frame = np.concatenate( (lw_mask_rgb, mw_mask_rgb), axis=1)
out_frame = np.concatenate( (upper_frame, lower_frame), axis=0 )
#rgb_frame = np.repeat(frame, 3, axis=1)
#rgb_frame = rgb_frame.reshape(frame.shape[0], frame.shape[1], 3)
fileName = "%s/%05d.jpg" % (sequence['ref'], frameCounter)
cv2.imwrite(fileName, lw_frame)
cv2.imshow('frame', out_frame)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cv2.destroyAllWindows()