forked from booz-allen-hamilton/DSB2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.py
799 lines (651 loc) · 23.8 KB
/
segment.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
import cv2
import numpy as np
import dicom
import json
import os
import random
import re
import shutil
import sys
from matplotlib import image
from scipy.ndimage import label
from scipy.ndimage.morphology import binary_erosion
from scipy.fftpack import fftn, ifftn
from scipy.signal import argrelmin, correlate
from scipy.spatial.distance import euclidean
from scipy.stats import linregress
from scipy.optimize import curve_fit
from scipy.interpolate import UnivariateSpline
#
# PARAMETERS
#
# number of bins to use in histogram for gaussian regression
NUM_BINS = 100
# number of standard deviations past which we will consider a pixel an outlier
STD_MULTIPLIER = 2
# number of points of our interpolated dataset to consider when searching for
# a threshold value; the function by default is interpolated over 1000 points,
# so 250 will look at the half of the points that is centered around the known
# myocardium pixel
THRESHOLD_AREA = 250
# number of pixels on the line within which to search for a connected component
# in a thresholded image, increase this to look for components further away
COMPONENT_INDEX_TOLERANCE = 20
# number of angles to search when looking for the correct orientation
ANGLE_SLICES = 36
#
# FUNCTIONS
#
def log(msg, lvl):
string = ""
for i in range(lvl):
string += " "
string += msg
print string
def auto_segment_all_datasets():
d = sys.argv[1]
studies = next(os.walk(os.path.join(d, "train")))[1] + next(
os.walk(os.path.join(d, "validate")))[1]
labels = np.loadtxt(os.path.join(d, "train.csv"), delimiter=",",
skiprows=1)
label_map = {}
for l in labels:
label_map[l[0]] = (l[2], l[1])
num_samples = None
if len(sys.argv) > 2:
num_samples = int(sys.argv[2])
studies = random.sample(studies, num_samples)
if os.path.exists("output"):
shutil.rmtree("output")
os.mkdir("output")
accuracy_csv = open("accuracy.csv", "w")
accuracy_csv.write("Dataset,Actual EDV,Actual ESV,Predicted EDV,"
"Predicted ESV\n")
submit_csv = open("submit.csv", "w")
submit_csv.write("Id,")
for i in range(0, 600):
submit_csv.write("P%d" % i)
if i != 599:
submit_csv.write(",")
else:
submit_csv.write("\n")
for s in studies:
if int(s) <= 500:
full_path = os.path.join(d, "train", s)
else:
full_path = os.path.join(d, "validate", s)
dset = Dataset(full_path, s)
print "Processing dataset %s..." % dset.name
p_edv = 0
p_esv = 0
try:
dset.load()
segment_dataset(dset)
if dset.edv >= 600 or dset.esv >= 600:
raise Exception("Prediction too large")
p_edv = dset.edv
p_esv = dset.esv
except Exception as e:
log("***ERROR***: Exception %s thrown by dataset %s" % (str(e), dset.name), 0)
submit_csv.write("%d_systolic," % int(dset.name))
for i in range(0, 600):
if i < p_esv:
submit_csv.write("0.0")
else:
submit_csv.write("1.0")
if i == 599:
submit_csv.write("\n")
else:
submit_csv.write(",")
submit_csv.write("%d_diastolic," % int(dset.name))
for i in range(0, 600):
if i < p_edv:
submit_csv.write("0.0")
else:
submit_csv.write("1.0")
if i == 599:
submit_csv.write("\n")
else:
submit_csv.write(",")
(edv, esv) = label_map.get(int(dset.name), (None, None))
if edv is not None:
accuracy_csv.write("%s,%f,%f,%f,%f\n" % (dset.name, edv, esv, p_edv, p_esv))
accuracy_csv.close()
submit_csv.close()
class Dataset(object):
dataset_count = 0
def __init__(self, directory, subdir):
# deal with any intervening directories
while True:
subdirs = next(os.walk(directory))[1]
if len(subdirs) == 1:
directory = os.path.join(directory, subdirs[0])
else:
break
slices = []
for s in subdirs:
m = re.match("sax_(\d+)", s)
if m is not None:
slices.append(int(m.group(1)))
slices_map = {}
first = True
times = []
for s in slices:
files = next(os.walk(os.path.join(directory, "sax_%d" % s)))[2]
offset = None
for f in files:
m = re.match("IM-(\d{4,})-(\d{4})\.dcm", f)
if m is not None:
if first:
times.append(int(m.group(2)))
if offset is None:
offset = int(m.group(1))
first = False
slices_map[s] = offset
self.directory = directory
self.time = sorted(times)
self.slices = sorted(slices)
self.slices_map = slices_map
Dataset.dataset_count += 1
self.name = subdir
def _filename(self, s, t):
return os.path.join(self.directory,"sax_%d" % s, "IM-%04d-%04d.dcm" % (self.slices_map[s], t))
def _read_dicom_image(self, filename):
d = dicom.read_file(filename)
img = d.pixel_array
return np.array(img)
def _read_all_dicom_images(self):
f1 = self._filename(self.slices[0], self.time[0])
d1 = dicom.read_file(f1)
(x, y) = d1.PixelSpacing
(x, y) = (float(x), float(y))
f2 = self._filename(self.slices[1], self.time[0])
d2 = dicom.read_file(f2)
# try a couple of things to measure distance between slices
try:
dist = np.abs(d2.SliceLocation - d1.SliceLocation)
except AttributeError:
try:
dist = d1.SliceThickness
except AttributeError:
dist = 8 # better than nothing...
self.images = np.array([[self._read_dicom_image(self._filename(d, i))
for i in self.time]
for d in self.slices])
self.dist = dist
self.area_multiplier = x * y
def load(self):
self._read_all_dicom_images()
# assumes dataset is loaded, call dataset.load()
def segment_dataset(dataset):
images = dataset.images
dist = dataset.dist
areaMultiplier = dataset.area_multiplier
# shape: num slices, num snapshots, rows, columns
log("Calculating rois...", 1)
rois, circles = calc_rois(images)
log("Calculating areas...", 1)
all_masks, all_areas = calc_all_areas(images, rois, circles)
log("Calculating volumes...", 1)
area_totals = [calc_total_volume(a, areaMultiplier, dist)
for a in all_areas]
log("Calculating ef...", 1)
edv = max(area_totals)
esv = min(area_totals)
ef = (edv - esv) / edv
log("Done, ef is %f" % ef, 1)
save_masks_to_dir(dataset, all_masks)
output = {}
output["edv"] = edv
output["esv"] = esv
output["ef"] = ef
output["areas"] = all_areas.tolist()
f = open("output/%s/output.json" % dataset.name, "w")
json.dump(output, f, indent=2)
f.close()
dataset.edv = edv
dataset.esv = esv
dataset.ef = ef
def save_masks_to_dir(dataset, all_masks):
os.mkdir("output/%s" % dataset.name)
for t in range(len(dataset.time)):
os.mkdir("output/%s/time%02d" % (dataset.name, t))
for s in range(len(dataset.slices)):
mask = all_masks[t][s]
image.imsave("output/%s/time%02d/slice%02d_mask.png" %
(dataset.name, t, s), mask)
eroded = binary_erosion(mask)
hollow_mask = np.where(eroded, 0, mask)
colorimg = cv2.cvtColor(dataset.images[s][t],
cv2.COLOR_GRAY2RGB)
colorimg = colorimg.astype(np.uint8)
colorimg[hollow_mask != 0] = [255, 0, 255]
image.imsave("output/%s/time%02d/slice%02d_color.png" %
(dataset.name, t, s), colorimg)
def calc_rois(images):
(num_slices, _, _, _) = images.shape
log("Calculating mean...", 2)
dc = np.mean(images, 1)
def get_H1(i):
log("Fourier transforming on slice %d..." % i, 3)
ff = fftn(images[i])
first_harmonic = ff[1, :, :]
log("Inverse Fourier transforming on slice %d..." % i, 3)
result = np.absolute(ifftn(first_harmonic))
log("Performing Gaussian blur on slice %d..." % i, 3)
result = cv2.GaussianBlur(result, (5, 5), 0)
return result
log("Performing Fourier transforms...", 2)
h1s = np.array([get_H1(i) for i in range(num_slices)])
m = np.max(h1s) * 0.05
h1s[h1s < m] = 0
log("Applying regression filter...", 2)
regress_h1s = regression_filter(h1s)
log("Post-processing filtered images...", 2)
proc_regress_h1s, coords = post_process_regression(regress_h1s)
log("Determining ROIs...", 2)
rois, circles = get_ROIs(dc, proc_regress_h1s, coords)
return rois, circles
def calc_all_areas(images, rois, circles):
closest_slice = get_closest_slice(rois)
(_, times, _, _) = images.shape
def calc_areas(time):
log("Calculating areas at time %d..." % time, 2)
mask, mean = locate_lv_blood_pool(images, rois, circles, closest_slice,
time)
masks, areas = propagate_segments(images, rois, mask, mean,
closest_slice, time)
return (masks, areas)
result = np.transpose(map(calc_areas, range(times)))
all_masks = result[0]
all_areas = result[1]
return all_masks, all_areas
def calc_total_volume(areas, area_multiplier, dist):
slices = np.array(sorted(areas.keys()))
modified = [areas[i] * area_multiplier for i in slices]
vol = 0
for i in slices[:-1]:
a, b = modified[i], modified[i+1]
subvol = (dist/3.0) * (a + np.sqrt(a*b) + b)
vol += subvol / 1000.0 # conversion to mL
return vol
def get_centroid(img):
nz = np.nonzero(img)
pxls = np.transpose(nz)
weights = img[nz]
avg = np.average(pxls, axis=0, weights=weights)
return avg
def regress_centroids(cs):
num_slices = len(cs)
y_centroids = cs[:, 0]
x_centroids = cs[:, 1]
z_values = np.array(range(num_slices))
(xslope, xintercept, _, _, _) = linregress(z_values, x_centroids)
(yslope, yintercept, _, _, _) = linregress(z_values, y_centroids)
return (xslope, xintercept, yslope, yintercept)
def get_weighted_distances(imgs, coords, xs, xi, ys, yi):
a = np.array([0, yi, xi])
n = np.array([1, ys, xs])
zeros = np.zeros(3)
def dist(p):
to_line = (a - p) - (np.dot((a - p), n) * n)
d = euclidean(zeros, to_line)
return d
def weight(p):
(z, y, x) = p
return imgs[z, y, x]
dists = np.array([dist(c) for c in coords])
weights = np.array([weight(c) for c in coords])
return (coords, dists, weights)
def gaussian_fit(dists, weights):
# based on http://stackoverflow.com/questions/11507028/fit-a-gaussian-function
(x, y) = histogram_transform(dists, weights)
fivep = int(len(x) * 0.05)
xtmp = x
ytmp = y
fromFront = False
while True:
if len(xtmp) == 0 and len(ytmp) == 0:
if fromFront:
# well we failed
idx = np.argmax(y)
xmax = x[idx]
p0 = [max(y), xmax, xmax]
(A, mu, sigma) = p0
return mu, sigma, lambda x: gauss(x, A, mu, sigma)
else:
fromFront = True
xtmp = x
ytmp = y
idx = np.argmax(ytmp)
xmax = xtmp[idx]
def gauss(x, *p):
A, mu, sigma = p
return A*np.exp(-(x-mu)**2/(2.*sigma**2))
p0 = [max(ytmp), xmax, xmax]
try:
coeff, var_matrix = curve_fit(gauss, xtmp, ytmp, p0=p0)
(A, mu, sigma) = coeff
return (mu, sigma, lambda x: gauss(x, A, mu, sigma))
except RuntimeError:
if fromFront:
xtmp = xtmp[fivep:]
ytmp = ytmp[fivep:]
else:
xtmp = xtmp[:-fivep]
ytmp = ytmp[:-fivep]
def histogram_transform(values, weights):
hist, bins = np.histogram(values, bins=NUM_BINS, weights=weights)
bin_width = bins[1] - bins[0]
bin_centers = bins[:-1] + (bin_width / 2)
return (bin_centers, hist)
def get_outliers(coords, dists, weights):
fivep = int(len(weights) * 0.05)
ctr = 1
while True:
(mean, std, fn) = gaussian_fit(dists, weights)
low_values = dists < (mean - STD_MULTIPLIER*np.abs(std))
high_values = dists > (mean + STD_MULTIPLIER*np.abs(std))
outliers = np.logical_or(low_values, high_values)
if len(coords[outliers]) == len(coords):
weights[-fivep*ctr:] = 0
ctr += 1
else:
return coords[outliers]
def regress_and_filter_distant(imgs):
centroids = np.array([get_centroid(img) for img in imgs])
raw_coords = np.transpose(np.nonzero(imgs))
(xslope, xintercept, yslope, yintercept) = regress_centroids(centroids)
(coords, dists, weights) = get_weighted_distances(imgs, raw_coords, xslope,
xintercept, yslope,
yintercept)
outliers = get_outliers(coords, dists, weights)
imgs_cpy = np.copy(imgs)
for c in outliers:
(z, x, y) = c
imgs_cpy[z, x, y] = 0
return imgs_cpy
def regression_filter(imgs):
condition = True
iternum = 0
while(condition):
log("Beginning iteration %d of regression..." % iternum, 3)
iternum += 1
imgs_filtered = regress_and_filter_distant(imgs)
c1 = get_centroid(imgs)
c2 = get_centroid(imgs_filtered)
dc = np.linalg.norm(c1 - c2)
imgs = imgs_filtered
condition = (dc > 1.0) # because python has no do-while loops
return imgs
def post_process_regression(imgs):
(numimgs, _, _) = imgs.shape
centroids = np.array([get_centroid(img) for img in imgs])
log("Performing final centroid regression...", 3)
(xslope, xintercept, yslope, yintercept) = regress_centroids(centroids)
imgs_cpy = np.copy(imgs)
def filter_one_img(zlvl):
points_on_zlvl = np.transpose(imgs[zlvl].nonzero())
points_on_zlvl = np.insert(points_on_zlvl, 0, zlvl, axis=1)
(coords, dists, weights) = get_weighted_distances(imgs, points_on_zlvl,
xslope, xintercept,
yslope, yintercept)
outliers = get_outliers(coords, dists, weights)
for c in outliers:
(z, x, y) = c
imgs_cpy[z, x, y] = 0
log("Final image filtering...", 3)
for z in range(numimgs):
log("Filtering image %d of %d..." % (z+1, numimgs), 4)
filter_one_img(z)
return (imgs_cpy, (xslope, xintercept, yslope, yintercept))
def floats_draw_circle(img, center, r, color, thickness):
(x, y) = center
x, y = int(np.round(x)), int(np.round(y))
r = int(np.round(r))
cv2.circle(img, center=(x, y), radius=r, color=color, thickness=thickness)
def filled_ratio_of_circle(img, center, r):
mask = np.zeros_like(img)
floats_draw_circle(mask, center, r, 1, -1)
masked = mask * img
(x, _) = np.nonzero(mask)
(x2, _) = np.nonzero(masked)
if x.size == 0:
return 0
return float(x2.size) / x.size
def circle_smart_radius(img, center):
domain = np.arange(1, 100)
(xintercept, yintercept) = center
def ratio(r):
return filled_ratio_of_circle(img, (xintercept, yintercept), r)*r
y = np.array([ratio(d) for d in domain])
most = np.argmax(y)
return domain[most]
def get_ROIs(originals, h1s, regression_params):
(xslope, xintercept, yslope, yintercept) = regression_params
(num_slices, _, _) = h1s.shape
results = []
circles = []
for i in range(num_slices):
log("Getting ROI in slice %d..." % i, 3)
o = originals[i]
h = h1s[i]
ctr = (xintercept + xslope * i, yintercept + yslope * i)
r = circle_smart_radius(h, ctr)
tmp = np.zeros_like(o)
floats_draw_circle(tmp, ctr, r, 1, -1)
results.append(tmp * o)
circles.append((ctr, r))
return (np.array(results), np.array(circles))
def bresenham(x0, x1, y0, y1, fn):
# using some pseudocode from
# https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm
# and also https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
steep = abs(y1-y0) > abs(x1-x0)
if steep:
x0, x1, y0, y1 = y0, y1, x0, x1
if x0 > x1:
x0, x1, y0, y1 = x1, x0, y1, y0
def plot(x, y):
if steep:
fn(y, x)
else:
fn(x, y)
dx = x1 - x0
dy = y1 - y0
D = 2*np.abs(dy) - dx
plot(x0, y0)
y = y0
for x in range(x0+1, x1+1): # x0+1 to x1
D = D + 2*np.abs(dy)
if D > 0:
y += np.sign(dy)
D -= 2*dx
plot(x, y)
def line_thru(bounds, center, theta):
(xmin, xmax, ymin, ymax) = bounds
(cx, cy) = center
if np.cos(theta) == 0:
return (cx, ymin, cx, ymax)
slope = np.tan(theta)
x0 = xmin
y0 = cy - (cx - xmin) * slope
if y0 < ymin:
y0 = ymin
x0 = max(xmin, cx - ((cy - ymin) / slope))
elif y0 > ymax:
y0 = ymax
x0 = max(xmin, cx - ((cy - ymax) / slope))
x1 = xmax
y1 = cy + (xmax - cx) * slope
if y1 < ymin:
y1 = ymin
x1 = min(xmax, cx + ((ymin - cy) / slope))
elif y1 > ymax:
y1 = ymax
x1 = min(xmax, cx + ((ymax - cy) / slope))
return (x0, x1, y0, y1)
def get_line_coords(w, h, cx, cy, theta):
coords = np.floor(np.array(line_thru((0, w-1, 0, h-1), (cx, cy), theta)))
return coords.astype(np.int)
def trim_zeros_indices(has_zeros):
first = 0
for i in has_zeros:
if i == 0:
first += 1
else:
break
last = len(has_zeros)
for i in has_zeros[::-1]:
if i == 0:
last -= 1
else:
break
return first, last
def get_line(roi, cx, cy, theta):
(h, w) = roi.shape
(x0, x1, y0, y1) = get_line_coords(w, h, cx, cy, theta)
intensities = []
coords = []
def collect(x, y):
if y < 0 or y >= h or x < 0 or x >= w:
return
intensities.append(roi[y, x])
coords.append((x, y))
bresenham(x0, x1, y0, y1, collect)
def geti(idx):
return intensities[idx]
getiv = np.vectorize(geti)
x = np.arange(0, len(intensities))
y = getiv(x)
first, last = trim_zeros_indices(y)
trimy = y[first:last]
trimcoords = coords[first:last]
trimx = np.arange(0, trimy.size)
return (trimx, trimy, trimcoords)
def find_best_angle(roi, circ):
((cx, cy), r) = circ
results = np.zeros(ANGLE_SLICES)
fns = [None for i in range(ANGLE_SLICES)]
COS_MATCHED_FILTER_FREQ = 2.5
def score_matched(trimx, trimy):
# first, normalize this data
newtrimx = np.linspace(0.0, 1.0, np.size(trimx))
minimum = np.min(trimy)
maximum = np.max(trimy) - minimum
newtrimy = (trimy - minimum) / maximum
filt = 1 - ((np.cos(COS_MATCHED_FILTER_FREQ*2*np.pi*newtrimx)) /
2 + (0.5))
cr = correlate(newtrimy, filt, mode="same")
return np.max(cr)
for i in range(ANGLE_SLICES):
trimx, trimy, trimcoords = get_line(roi, cx, cy, np.pi*i/ANGLE_SLICES)
score2 = score_matched(trimx, trimy)
results[i] = score2
fns[i] = (UnivariateSpline(trimx, trimy), trimx, trimcoords)
best = np.argmax(results)
return (best * np.pi / ANGLE_SLICES, fns[best])
def find_threshold_point(best, best_fn):
fn, trimx, trim_coords = best_fn
dom = np.linspace(np.min(trimx), np.max(trimx), 1000)
f = fn(dom)
mins = argrelmin(f)
closest_min = -1
closest_dist = -1
for m in np.nditer(mins):
dist = np.abs(500 - m)
if closest_min == -1 or closest_dist > dist:
closest_min = m
closest_dist = dist
fnprime = fn.derivative()
restrict = dom[np.max(closest_min-THRESHOLD_AREA, 0):
closest_min+THRESHOLD_AREA]
f2 = fnprime(restrict)
m1 = restrict[np.argmax(f2)]
mean = fn(m1)
idx = np.min([int(np.floor(m1))+1, len(trim_coords)-1])
return (mean, trim_coords, idx)
def get_closest_slice(rois):
ctrd = get_centroid(rois)
closest_slice = int(np.round(ctrd[0]))
return closest_slice
def locate_lv_blood_pool(images, rois, circles, closest_slice, time):
best, best_fn = find_best_angle(rois[closest_slice],
circles[closest_slice])
mean, coords, idx = find_threshold_point(best, best_fn)
thresh, img_bin = cv2.threshold(images[closest_slice,
time].astype(np.float32),
mean, 255.0, cv2.THRESH_BINARY)
labeled, num = label(img_bin)
x, y = coords[idx]
count = 0
# Look along the line for a component. If one isn't found within a certain
# number of indices, just spit out the original coordinate.
while labeled[y][x] == 0:
idx += 1
count += 1
x, y = coords[idx]
if count > COMPONENT_INDEX_TOLERANCE:
idx -= count
x, y = coords[idx]
break
if count <= COMPONENT_INDEX_TOLERANCE:
component = np.transpose(np.nonzero(labeled == labeled[y][x]))
else:
component = np.array([[y, x]])
hull = cv2.convexHull(component)
squeezed = hull
if count <= COMPONENT_INDEX_TOLERANCE:
squeezed = np.squeeze(squeezed)
hull = np.fliplr(squeezed)
mask = np.zeros_like(labeled)
cv2.drawContours(mask, [hull], 0, 255, thickness=-1)
return mask, mean
def propagate_segments(images, rois, base_mask, mean, closest_slice, time):
def propagate_segment(i, mask):
thresh, img_bin = cv2.threshold(images[i,
time].astype(np.float32),
mean, 255.0, cv2.THRESH_BINARY)
labeled, features = label(img_bin)
region1 = mask == 255
max_similar = -1
max_region = 0
for j in range(1, features+1):
region2 = labeled == j
intersect = np.count_nonzero(np.logical_and(region1, region2))
union = np.count_nonzero(np.logical_or(region1, region2))
similar = float(intersect) / union
if max_similar == -1 or max_similar < similar:
max_similar = similar
max_region = j
if max_similar == 0:
component = np.transpose(np.nonzero(mask))
else:
component = np.transpose(np.nonzero(labeled == max_region))
hull = cv2.convexHull(component)
hull = np.squeeze(hull)
if hull.shape == (2L,):
hull = np.array([hull])
hull = np.fliplr(hull)
newmask = np.zeros_like(img_bin)
cv2.drawContours(newmask, [hull], 0, 255, thickness=-1)
return newmask
(rois_depth, _, _) = rois.shape
newmask = base_mask
masks = {}
areas = {}
masks[closest_slice] = base_mask
areas[closest_slice] = np.count_nonzero(base_mask)
for i in range(closest_slice-1, -1, -1):
newmask = propagate_segment(i, newmask)
masks[i] = newmask
areas[i] = np.count_nonzero(newmask)
newmask = base_mask
for i in range(closest_slice+1, rois_depth):
newmask = propagate_segment(i, newmask)
masks[i] = newmask
areas[i] = np.count_nonzero(newmask)
return masks, areas
if __name__ == "__main__":
random.seed()
auto_segment_all_datasets()