-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateTracksGUI.m
executable file
·217 lines (154 loc) · 7.89 KB
/
generateTracksGUI.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
function [tracks, display_tracks] = generateTracksGUI(L, cell_area, cellPairs)
% Note, this is the same as 'generateTracksMinimal', except that it
% requires that cellPairs be passed in as an argument
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2: find non-border cells
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nonborder_cells = findNonBorderCells(L);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3: Generate cell array to store tracks. Before creating tracks, we need to
% populate it with the indices of cells in the final time point
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get dimensions of the image stack
x_resolution = size(L,1);
y_resolution = size(L,2);
t_resolution = size(L,3);
% initialize cell array to store tracks
tracking_array = cell(zeros,t_resolution);
% populate the last cell of tracks with the indices of cells from the last
% time point
for ii = 1:length(nonborder_cells{t_resolution})
tracking_array{t_resolution}(ii) = nonborder_cells{t_resolution}(ii);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 4: Create tracks from cellPairs. Tracks will be represented as indices
% within the tracking_array. If cells are successfully mapped between
% adjacent time points (by cellPairs), then their unique indices (from
% bwlabel) will be stored in the same track-index. However, if they are
% not mapped by cellPairs, then a new track will be generated.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iterate through time
for t = 1:t_resolution-1
% store current time as an easy-to-read variable
current_time = t_resolution-t;
% create a counter to keep track of how many cells we'll be indexing -
% this is specifically for adding cells to the tracking_array when
% there is no match in cellPairs - we'll start by taking the number of
% unique tracks from the previous time point (t+1)
current_length = length(tracking_array{current_time+1});
previous_tracks = tracking_array{current_time+1};
% iterate through cells of current time point
for ii = 1:length(nonborder_cells{current_time})
% store index of current cell
current_index = nonborder_cells{current_time}(ii);
% store indices mapped from t+1 to current time point by cellPairs
mapped_indices = cellPairs{current_time+1};
% look for match between current index and indices mapped from t+1
% by cellPairs
test_match = ismember(current_index, mapped_indices);
% if current index was successfully mapped by cellPairs
if test_match ~= 0
% find the index of the current cell's mapped partner from
% the t+1 timepoint in cellPairs. this is the index of the
% current cell in the t+1 time point.
partner_index = find(mapped_indices == current_index);
% find the location of the partner index (representing the t+1
% index corresponding to the current_index) in tracks from the
% previous time point, which we vectorized in the
% inner-time-loop
tracking_partner_index = find(previous_tracks == partner_index);
% insert the current_index into the appropriate track. note
% that tracks are represented by indices within tracking_array
tracking_array{current_time}(tracking_partner_index) = current_index;
end
% if current index IS NOT mapped in cellPairs
if test_match == 0
% update the counter to signify creation of a new track
current_length = current_length+1;
% create new track in tracking_array (counter 'current_length'
% should be a greater than the largest entry in tracking_array
% for the current time point)
tracking_array{current_time}(current_length) = current_index;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 5: Pull tracks out of cell array and store in matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%
% First, let's fine the largest number of cells we need to account for
%%%%%
% initialize counter that records the largest number of tracks from
% tracking_array
max_cells = 0;
% iterate through time
for ii = 1:t_resolution
% store the number of tracks from the current time point of
% tracking_array
current_length = length(tracking_array{ii});
% if the current number of tracks is greater than the previous largest
% number of tracks..
if current_length > max_cells
% then update max_cells to the current number of tracks
max_cells = current_length;
end
end
% initialize matrix to store tracks. it will be the greatest number of
% tracks x time
tracks = zeros(max_cells, t_resolution); % (tracks x time)
%%%%%
% now let's populate the matrix with tracks from the remaining time points
%%%%%
% loop through time
for ii = 1:t_resolution
% store list of current cell indices
current_cell_list = tracking_array{ii};
% loop through list of current indices
for iii = 1:length(current_cell_list)
% insert current index from tracking_array into the current
% position within tracks
tracks(iii,ii) = current_cell_list(iii);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 6: Create a matrix to visualize the tracks. This will be achieved by
% uniquely coloring each track with random numbers.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% initialize matrix to display tracks
display_tracks = zeros(x_resolution, y_resolution, 3, t_resolution);
% create an RGB random color vector to uniquely color each track
random_colors = rand(length(tracks(:,1)),3);
% iterate through time
for t = 1:t_resolution
% iterate through tracks
for ii = 1:length(tracks(:,t))
% store current index (unique index in L for cell at current time
% in the current rack
current_cell_index = tracks(ii,t);
% check to make sure a cell actually exists at this time point
if current_cell_index ~= 0
% if it does, then find the pixels indices representing its
% area
x_pixels = cell_area{t}{current_cell_index}(:,1);
y_pixels = cell_area{t}{current_cell_index}(:,2);
% iterate through this pixel-area indices
for iii = 1:length(x_pixels)
% color in the cell area based on the random-color vector
display_tracks(x_pixels(iii),y_pixels(iii),1,t) = random_colors(ii,1);
display_tracks(x_pixels(iii),y_pixels(iii),2,t) = random_colors(ii,2);
display_tracks(x_pixels(iii),y_pixels(iii),3,t) = random_colors(ii,3);
end
end
end
end
end