1
1
import os
2
2
import time
3
-
3
+ import csv
4
4
import cv2
5
5
import numpy as np
6
6
import tensorflow as tf
@@ -15,7 +15,6 @@ def run_net():
15
15
16
16
with tf .name_scope ('Data' ):
17
17
image_seq_fw , filename_seq_fw , image_seq_bw , filename_seq_bw = data_provider .get_sequence (params .seq_length )
18
- image_seq_fw_bw = [tf .concat ([fw , bw ], axis = 0 ) for fw , bw in zip (image_seq_fw , image_seq_bw [::- 1 ])]
19
18
filename_seq_bw = filename_seq_bw [::- 1 ]
20
19
# Build Network Graph
21
20
net_fw = BiGRUNetwork ()
@@ -106,34 +105,83 @@ def run_net():
106
105
coord .request_stop ()
107
106
coord .join (threads )
108
107
loop = False
108
+ isbi_out_dict = {}
109
109
if not params .dry_run :
110
110
out_dir = params .experiment_out_dir
111
+ max_id = 0
112
+ base_out_fname = os .path .join (params .experiment_isbi_out , 'man_seg{time:03d}.tif' )
111
113
for t , file_name in enumerate (all_filenames ):
112
114
fw_logits = np .load (os .path .join (params .experiment_tmp_fw_dir , file_name ))
113
115
bw_logits = np .load (os .path .join (params .experiment_tmp_bw_dir , file_name ))
114
116
feed_dict = {bw_ph : bw_logits , fw_ph : fw_logits }
115
117
seg_out = sess .run (final_out , feed_dict )
116
- seg_cell = np .argmax (seg_out , axis = 2 ) == 1
117
- cc_out = cv2 .connectedComponentsWithStats (seg_cell , 8 , cv2 .CV_32S )
118
+ seg_cell = (seg_out [:, :, 1 ] > 0.7 ).astype (
119
+ np .float32 ) # (np.argmax(seg_out, axis=2) == 1).astype(np.float16)
120
+ cc_out = cv2 .connectedComponentsWithStats (seg_cell .astype (np .uint8 ), 8 , cv2 .CV_32S )
118
121
num_cells = cc_out [0 ]
119
122
labels = cc_out [1 ]
120
123
stats = cc_out [2 ]
121
- centroids = cc_out [ 3 ]
124
+
122
125
if t == 0 :
123
- num_cells_prev = cc_out [ 0 ]
126
+
124
127
labels_prev = cc_out [1 ]
125
- stats_prev = cc_out [ 2 ]
126
- centroids_prev = cc_out [ 3 ]
128
+ max_id = num_cells
129
+ isbi_out_dict = { n : [ n , 0 , 0 , 0 ] for n in range ( 1 , 1 + num_cells )}
127
130
continue
131
+ matched_labels = np .zeros_like (labels )
132
+ unmatched_indexes = list (range (1 , 1 + num_cells ))
133
+ continued_ids = []
134
+ ids_to_split = set ()
135
+ split_dict = {}
136
+ for p in np .arange (1 , 1 + num_cells ):
137
+ matching_mask = labels_prev [labels == p ]
138
+ matching_candidates = np .unique (matching_mask )
139
+ for candidate in matching_candidates :
140
+ if candidate == 0 :
141
+ continue
142
+ intersection = np .count_nonzero (matching_mask .flatten () == candidate )
143
+ area = stats [p , cv2 .CC_STAT_AREA ].astype (np .float32 )
144
+ if intersection / area > 0.5 :
145
+ # Keep score of matched_indexes labels in order to know which are new cells
146
+ unmatched_indexes .remove (p )
147
+ if candidate not in continued_ids :
148
+ # Keep score of matched_indexes labels in order to know which tracks to stop
149
+ continued_ids .append [candidate ]
150
+ split_dict [candidate ] = [p ] # Keep for mitosis detection
151
+ else :
152
+ # Keep score of matched_indexes labels in order to know which tracks to stop
153
+ split_dict [candidate ].append [p ]
154
+ ids_to_split .add [candidate ]
155
+ continued_ids .remove (candidate )
156
+ matched_labels [labels == p ] = candidate
157
+ continue
158
+ for cont_id in continued_ids :
159
+ isbi_out_dict [cont_id ][2 ] += 1
160
+
161
+ out_labels = matched_labels .copy ()
162
+ for parent_id , idxs in split_dict .items ():
163
+ if len (idxs ) > 1 :
164
+ for idx in idxs :
165
+ max_id += 1
166
+ out_labels [labels == idx ] = max_id
167
+ isbi_out_dict [max_id ] = [max_id , t , t , parent_id ]
168
+
169
+ for unmatched in unmatched_indexes :
170
+ max_id += 1
171
+ out_labels [labels == unmatched ] = max_id
172
+ isbi_out_dict [max_id ] = [max_id , t , t , 0 ]
173
+ out_fname = base_out_fname .format (time = t )
174
+ cv2 .imwrite (filename = out_fname , img = out_labels )
175
+
176
+
177
+ labels_prev = out_labels
178
+
179
+ csv_file_name = os .path .join (params .experiment_isbi_out , 'man_track.txt' )
180
+
181
+ with open (csv_file_name ) as f :
182
+ writer = csv .writer (f , delimiter = ' ' )
183
+ writer .writerows (isbi_out_dict .values ())
128
184
129
- num_cells_prev = cc_out [0 ]
130
- labels_prev = cc_out [1 ]
131
- stats_prev = cc_out [2 ]
132
- centroids_prev = cc_out [3 ]
133
-
134
-
135
-
136
- #
137
185
# seg_out = (seg_out*255).astype(np.uint8)
138
186
# scipy.misc.toimage(seg_out, cmin=0.0,
139
187
# cmax=255.).save(os.path.join(out_dir, os.path.basename(file_name[:-4])))
0 commit comments