-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracker.m
232 lines (204 loc) · 8.03 KB
/
tracker.m
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
% Particle tracking by Dan Shuldman and Patrick Oare
% Version 2.2
% Struct array "particles" contains all the particles with field "pos" + "match"
% pos : x, y, z
% match : 1/0
dist_thresh = 15E-6; %TODO work with this; diameter (in meters) in which to search for particle in next frame
appear_thresh = 8; % remove particles that apear for less than this number of frames
blink_keep = 10; % number of frames to search for missing/blinking particle
smoothparam = [0.003 0.003 0.0005]; % Spline Smoothing parameter between 0 and 1; Zero being a straight line.
tic
scale = 15; %15, weights z axis less than x and y when looking for nearest neighbor
multiWaitbar('closeall');
clear xyzLocTracked
try
%Just a list of particle locations? will get rescaled to xyzLocScaled
xyzLocScaled = xyzLoc;
catch
xyzLocScaled = xyzLocCentroid;
end
for i = 1:length(xyzLocScaled)
% xyzLocScaled(i).time(:,1:3) = xyzLocScaled(i).time(:,1:3)/(scale);
xyzLocScaled(i).time(:,3) = xyzLocScaled(i).time(:,3)/(scale); %Scale 3rd column (z coord) why not x and y?
end
%Initialize struct array, with all particles in frame 1 + 2
particles = struct([]);
init = xyzLocScaled(1).time; %Init is an array of tuples at first time t = 1: [(x, y, z)]
initIndex = 1;
partIndex = 1;
while initIndex <= size(init, 1)
p = init(initIndex, :);
initIndex = initIndex + 1;
if isnan(p)
continue;
end
particles(partIndex).pos(1, :) = p;
particles(partIndex).match(1, 1) = 0; %Set it to 0 so that we can set it to 1 if we find it
partIndex = partIndex + 1;
end
notFound = struct([]);
%Will have fields pos (of [x, y, z]), frame (frame lost in), and index (in particles)
multiWaitbar('Tracking Particles...',0);
numFrames = length(xyzLocScaled);
for frame = 2:numFrames
cur_particles = xyzLocScaled(frame).time; %Current list of particle locations
[n, ~] = size(cur_particles);
%Set up particle matrix to turn into kd tree
particle_mat = nan(length(particles), 4); %3 space dims + 1 index dim
% multiWaitbar('Creating Particle Matrix...',0);
for j = 1:length(particles)
particle_mat(j, 4) = j; %Store index
end
for i = 1:length(particles)
positions = particles(i).pos; %Particle positions
last_pos = positions(frame - 1, :);
particles(i).pos(frame, :) = nan(1, 3);
particles(i).match(frame - 1, 1) = 0;
if ~isnan(last_pos)
particle_mat(i, 1:3) = last_pos;
end
end
pmat = strip_nans(particle_mat, 4);
tree = const_tree(pmat);
noMatch = nan(length(particles), 3);
noMatchIndex = 1;
% multiWaitbar('Searching for Nearest Neighbors...',0);
%Loop through all particles in current frame
for i = 1:n
part = cur_particles(i, :); %current particle
if isnan(part)
continue;
end
node = nearest_neighbor(tree, part);
nn = node.val;
index = node.index;
dist = sqrt(sum((nn-part).^2));
if dist < dist_thresh
%found particle
particles(index).pos(frame, :) = part;
particles(index).match(frame-1, 1) = 1;
else
%check for blinking particles
noMatch(noMatchIndex, :) = part;
noMatchIndex = noMatchIndex + 1;
end
% multiWaitbar('Searching for Nearest Neighbors...',i / n);
end
%BEGIN BLINKING
noMatch = strip_nans(noMatch, 3);
for i = 1:length(particles)
if isnan(particles(i).pos(frame - 1, :)) | particles(i).match(frame-1, 1) %Then particles have a match
continue;
end
notFound(end + 1).pos = particles(i).pos(frame - 1, :);
notFound(end).frame = frame - 1;
notFound(end).index = i;
end
if isempty(notFound)
continue;
end
points = nan(size(notFound, 2), 4);
k = 1;
while k <= size(notFound, 2)
if frame - notFound(k).frame > blink_keep
notFound(k) = [];
else
points(k, 1:3) = notFound(k).pos;
points(k, 4) = k; %Store notFound index
k = k + 1;
end
end
if isnan(points)
continue;
end
nfTree = const_tree(points);
toDel = nan(1, length(notFound));
toDelIndex = 1;
%Iterate through noMatch and find nearest neighbor
for i = 1:size(noMatch, 1)
part = noMatch(i, :);
node = nearest_neighbor(nfTree, part);
nn = node.val;
index = node.index;
d = sqrt(sum((nn-part).^2));
k = frame - notFound(index).frame; %Number of frames back
new_thresh = k*dist_thresh;
if d < new_thresh
pIndex = notFound(index).index;
particles(pIndex).pos(frame, :) = part;
toDel(1, toDelIndex) = index;
toDelIndex = toDelIndex + 1;
%TODO delete particle from k-d tree. Implement remove
else
%add particle to particles
particles(end + 1).pos(frame, :) = part;
particles(end).pos(1:frame - 1, :) = nan(frame - 1, 3);
particles(end).match(1:frame - 1, :) = zeros(frame - 1, 1);
end
end
%sort toDelIndex from largest to smallest and delete from nfIndex
sortedDel = reverseSort(toDel);
for i = 1:length(sortedDel)
notFound(sortedDel(i)) = [];
end
%END BLINKING
multiWaitbar('Tracking Particles...',frame/numFrames);
end
%Remove particles that appear less than appear_thresh
multiWaitbar('Removing Neglegible Particles and Correcting Data for Blinking Particles...',0);
index = 1;
partLength = length(particles);
for i = 1:partLength
if sum(particles(index).match) < appear_thresh
particles(index) = [];
else
%fills missing particles with linear approx
matches = find(particles(index).match(:,1));
for j = 1:length(matches) - 1
if matches(j) + 1 ~= matches(j+1)
skips = matches(j+1) - matches(j);
delpos = particles(index).pos(matches(j+1),1:3) - particles(index).pos(matches(j),1:3);
for k = matches(j)+1 : matches(j+1)-1
particles(index).pos(k,1:3) = particles(index).pos(matches(j),1:3) ...
+ delpos*(k-matches(j))/skips;
end
end
end
index = index + 1;
end
multiWaitbar('Removing Neglegible Particles and Correcting Data for Blinking Particles...',i/partLength);
end
%Store particles as fn of time (to plot)
numofframes = length(xyzLocScaled);
multiWaitbar('Reformatting Particle Data...',0);
for frame = 1:length(particles)
particles(frame).pos(:,3) = particles(frame).pos(:,3)*scale;
end
for frame = 1:numofframes
index = 1;
xyzLocTracked(frame).time = [];
for Lb = 1:length(particles)
if ~isempty(particles(Lb).pos) && ~isnan(particles(Lb).pos(frame,1))
xyzLocTracked(frame).time(index,1:3) = particles(Lb).pos(frame,1:3);
xyzLocTracked(frame).time(index,4) = Lb;
index = index + 1;
end
end
multiWaitbar('Reformatting Particle Data...',frame/numofframes);
end
%% Smooth Tracked Motion with a Spline Smoothing Function
[ xyzLocSmoothed, xyzLocTrackedMat, xyzLocSmoothedMat, numofparticles, FrameOfLastParticle ] = smoothtracks(xyzLocTracked, smoothparam);
%% Compute Velocity information and store in particles variable
particles = velocity(particles);
%% Save Work
multiWaitbar('closeall');
if ~exist('particlefilename','var')
particlefilename = 'temp';
warning('File name not specified. Saving to temp_Tracked.mat');
end
try
save([OutputPathStr,particlefilename,'_Track_DT',num2str(dist_thresh*1E6),'AT',num2str(appear_thresh),'BK',num2str(blink_keep),'.mat'], 'xyzLocTracked', 'xyzLocSmoothed', 'particles', 'dist_thresh', 'appear_thresh', 'blink_keep' )
catch
save([particlefilename,'_Track_DT',num2str(dist_thresh*1E6),'AT',num2str(appear_thresh),'BK',num2str(blink_keep),'.mat'], 'xyzLocTracked', 'xyzLocSmoothed', 'particles', 'dist_thresh', 'appear_thresh', 'blink_keep' )
end
toc2