-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
264 lines (190 loc) · 7.9 KB
/
utils.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import numpy as np
from skimage import color
from scipy.ndimage import binary_erosion, distance_transform_edt, maximum_filter
from scipy.ndimage import label as label_cc
from scipy.optimize import linear_sum_assignment
from skimage.filters import threshold_otsu
from skimage.measure import label as relabel_cc
from skimage.segmentation import watershed, relabel_sequential
def create_lut(labels: np.ndarray) -> np.ndarray:
"""Utility function to view labels as rgb lut with matplotlib.
# eg plt.imshow(create_lut(labels))
"""
max_label = np.max(labels)
lut = np.random.randint(
low=0,
high=255,
size=(int(max_label + 1), 3),
dtype=np.uint8)
lut = np.append(
lut,
np.zeros(
(int(max_label + 1), 1),
dtype=np.uint8) + 255,
axis=1)
lut[0] = 0
colored_labels = lut[labels]
return colored_labels
def erode(labels: np.ndarray, iterations: int, border_value: int):
"""Function to erode boundary pixels.
We fill an array with zeros, iterate over our labels, erode them
and then write them back into our empty array
"""
# copy labels to memory, create border array
labels = np.copy(labels)
# create zeros array for foreground
foreground = np.zeros_like(labels, dtype=bool)
# loop through unique labels
for label in np.unique(labels):
# skip background
if label == 0:
continue
# mask to label
label_mask = labels == label
# erode labels
eroded_mask = binary_erosion(
label_mask,
iterations=iterations,
border_value=border_value)
# get foreground
foreground = np.logical_or(eroded_mask, foreground)
# and background...
background = np.logical_not(foreground)
# set eroded pixels to zero
labels[background] = 0
return labels
def erode_border(labels, iterations, border_value):
"""Function to erode boundary pixels for mask and border."""
# copy labels to memory, create border array
labels = np.copy(labels)
border = np.array(labels)
# create zeros array for foreground
foreground = np.zeros_like(labels, dtype=bool)
# loop through unique labels
for label in np.unique(labels):
# skip background
if label == 0:
continue
# mask to label
label_mask = labels == label
# erode labels
eroded_mask = binary_erosion(
label_mask,
iterations=iterations,
border_value=border_value)
# get foreground
foreground = np.logical_or(eroded_mask, foreground)
# and background...
background = np.logical_not(foreground)
# set eroded pixels to zero
labels[background] = 0
# get eroded pixels
border = labels - border
return labels, border
def compute_sdt(labels: np.ndarray, constant: float = 0.5, scale: int = 5):
"""Function to compute a signed distance transform."""
inner = distance_transform_edt(binary_erosion(labels))
outer = distance_transform_edt(np.logical_not(labels))
distance = (inner - outer) + constant
distance = np.tanh(distance / scale)
return distance
# utility function to compute edge affinities
def compute_affinities(seg: np.ndarray, nhood: list):
nhood = np.array(nhood)
shape = seg.shape
nEdge = nhood.shape[0]
dims = nhood.shape[1]
aff = np.zeros((nEdge,) + shape, dtype=np.int32)
for e in range(nEdge):
aff[e, \
max(0,-nhood[e,0]):min(shape[0],shape[0]-nhood[e,0]), \
max(0,-nhood[e,1]):min(shape[1],shape[1]-nhood[e,1])] = \
(seg[max(0,-nhood[e,0]):min(shape[0],shape[0]-nhood[e,0]), \
max(0,-nhood[e,1]):min(shape[1],shape[1]-nhood[e,1])] == \
seg[max(0,nhood[e,0]):min(shape[0],shape[0]+nhood[e,0]), \
max(0,nhood[e,1]):min(shape[1],shape[1]+nhood[e,1])] ) \
* ( seg[max(0,-nhood[e,0]):min(shape[0],shape[0]-nhood[e,0]), \
max(0,-nhood[e,1]):min(shape[1],shape[1]-nhood[e,1])] > 0 ) \
* ( seg[max(0,nhood[e,0]):min(shape[0],shape[0]+nhood[e,0]), \
max(0,nhood[e,1]):min(shape[1],shape[1]+nhood[e,1])] > 0 )
return aff
def watershed_from_boundary_distance(
boundary_distances: np.ndarray,
boundary_mask: np.ndarray,
id_offset: float = 0,
min_seed_distance: int = 10
):
"""Function to compute a watershed from boundary distances."""
# get our seeds
max_filtered = maximum_filter(boundary_distances, min_seed_distance)
maxima = max_filtered==boundary_distances
seeds, n = label_cc(maxima)
if n == 0:
return np.zeros(boundary_distances.shape, dtype=np.uint64), id_offset
seeds[seeds!=0] += id_offset
# calculate our segmentation
segmentation = watershed(
boundary_distances.max() - boundary_distances,
seeds,
mask=boundary_mask)
return segmentation
def get_boundary_mask(pred, prediction_type, thresh=None):
if prediction_type == 'sdt' or prediction_type == 'two_class':
# simple threshold
boundary_mask = pred > thresh
elif prediction_type == 'three_class':
# fg = prediction greater than / equal to threshold
boundary_mask = pred[1] > thresh
elif prediction_type == 'affs':
# take mean of combined affs then threshold
boundary_mask = 0.5 * (pred[0] + pred[1]) > thresh
else:
raise Exception('Choose from one of the following prediction types: two_class, three_class, sdt, affs')
return boundary_mask
def evaluate(gt_labels: np.ndarray, pred_labels: np.ndarray, th: float = 0.5):
"""Function to evaluate a segmentation."""
pred_labels_rel, _, _ = relabel_sequential(pred_labels)
gt_labels_rel, _, _ = relabel_sequential(gt_labels)
overlay = np.array([pred_labels_rel.flatten(),
gt_labels_rel.flatten()])
# get overlaying cells and the size of the overlap
overlay_labels, overlay_labels_counts = np.unique(
overlay, return_counts=True, axis=1)
overlay_labels = np.transpose(overlay_labels)
# get gt cell ids and the size of the corresponding cell
gt_labels_list, gt_counts = np.unique(gt_labels_rel, return_counts=True)
gt_labels_count_dict = {}
for (l, c) in zip(gt_labels_list, gt_counts):
gt_labels_count_dict[l] = c
# get pred cell ids
pred_labels_list, pred_counts = np.unique(pred_labels_rel,
return_counts=True)
pred_labels_count_dict = {}
for (l, c) in zip(pred_labels_list, pred_counts):
pred_labels_count_dict[l] = c
num_pred_labels = int(np.max(pred_labels_rel))
num_gt_labels = int(np.max(gt_labels_rel))
num_matches = min(num_gt_labels, num_pred_labels)
# create iou table
iouMat = np.zeros((num_gt_labels+1, num_pred_labels+1),
dtype=np.float32)
for (u, v), c in zip(overlay_labels, overlay_labels_counts):
iou = c / (gt_labels_count_dict[v] + pred_labels_count_dict[u] - c)
iouMat[int(v), int(u)] = iou
# remove background
iouMat = iouMat[1:, 1:]
# use IoU threshold th
if num_matches > 0 and np.max(iouMat) > th:
costs = -(iouMat > th).astype(float) - iouMat / (2*num_matches)
gt_ind, pred_ind = linear_sum_assignment(costs)
assert num_matches == len(gt_ind) == len(pred_ind)
match_ok = iouMat[gt_ind, pred_ind] > th
tp = np.count_nonzero(match_ok)
else:
tp = 0
fp = num_pred_labels - tp
fn = num_gt_labels - tp
ap = tp / max(1, tp + fn + fp)
precision = tp / max(1, tp + fp)
recall = tp / max(1, tp + fn)
return ap, precision, recall, tp, fp, fn