-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathutils.py
1123 lines (980 loc) · 45 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
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division
import numpy as np
import nibabel as nib
import copy
import time
import configparser
from skimage.transform import resize
from scipy.ndimage import measurements
import tensorflow as tf
from glob import glob
import re
import SimpleITK as sitk
import random
from keras_preprocessing.image import *
import cv2 as cv
import colorsys
from skimage.measure import find_contours
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from collections import Counter
# construct a iterator for batch generation
class BatchGenerator(Iterator):
'''
get an iteratator for generating(batch_x, batch_y)
'''
def __init__(
self,
batch_size,
shuffle,
seed,
volume_path,
modalities,
resize_r,
rename_map,
patch_dim,
augmentation):
self.batch_size = batch_size
self.volume_path = volume_path
self.modalities = modalities
self.resize_ratio = resize_r
self.rename_map = rename_map
self.file_list = self._get_img_info()
self.total_num = len(self.file_list)
self.patch_dim = patch_dim
# self.rot_flag = rot_flag
self.augmentation = augmentation
self.image_shape = (patch_dim, patch_dim, patch_dim) + (modalities,)
self.label_shape = (patch_dim, patch_dim, patch_dim)
super(
BatchGenerator,
self).__init__(
n=self.total_num,
batch_size=batch_size,
shuffle=shuffle,
seed=seed)
def _get_img_info(self):
'''
this function read all files of specific directory, get the path list
:return:path list of all the volume files
'''
file_list = []
categories = os.listdir(self.volume_path)
for category in categories:
category_path = os.path.join(self.volume_path, category)
dir_list = os.listdir(category_path)
for dire in dir_list:
dire_lower = dire.lower()
if not dire_lower.startswith('brats'):
raise Exception("volume file exception!")
file_abs_path = os.path.join(category_path, dire)
single_file = {"path": file_abs_path, "category": category}
file_list.append(single_file)
return file_list
def _get_batches_of_transformed_samples(self, index_array):
batch_x = np.zeros(
(len(index_array),
) + self.image_shape,
dtype='float32')
batch_x2 = np.zeros(
(len(index_array),
) + self.label_shape+(1,),
dtype='float32')
batch_y = np.zeros(
(len(index_array),
) + self.label_shape,
dtype='int32')
batch_y_stage2 = np.zeros(
(len(index_array),
) + self.label_shape,
dtype='int32')
batch_y_stage3 = np.zeros(
(len(index_array),
) + self.label_shape,
dtype='int32')
for i, j in enumerate(index_array):
# data directory of a patient
single_dir_path = self.file_list[j]["path"]
img_data, img_data2, stage1_label_data, stage2_label, \
stage3_label, _ = self.load_volumes_label(single_dir_path, True)
rand_num = np.random.randint(self.total_num - 1, size=self.total_num)
matching_index = rand_num[0] if rand_num[0] != j else rand_num[-1]
# ready for histogram matching
img_data_matching, img_data_matching2, _, _, _, _ = self.load_volumes_label(
self.file_list[matching_index]["path"], True)
img_data_matching_cast = img_data_matching.astype("float32")
img_data_matching_cast2 = img_data_matching2.astype("float32")
# data augmentation
volume_list = [img_data[...,0], img_data[...,1], np.squeeze(img_data2, axis=-1),
stage1_label_data, stage2_label, stage3_label]
img_data_0, img_data_1, img_data2, stage1_label_data, \
stage2_label, stage3_label = self.data_augment_volume(*volume_list,
augmentation=self.augmentation)
img_data = np.stack((img_data_0,img_data_1), axis=-1)
img_data2 = np.expand_dims(img_data2, axis=-1)
# reduce background region
regions = get_brain_region(np.squeeze(img_data2, axis=-1))
img_data = img_data[regions[0]:regions[1], regions[2]:regions[3], regions[4]:regions[5], :]
img_data2 = img_data2[regions[0]:regions[1], regions[2]:regions[3], regions[4]:regions[5], :]
stage1_label_data = stage1_label_data[regions[0]:regions[1], regions[2]:regions[3], regions[4]:regions[5]]
stage2_label= stage2_label[regions[0]:regions[1], regions[2]:regions[3],
regions[4]:regions[5]]
stage3_label = stage3_label[regions[0]:regions[1], regions[2]:regions[3],
regions[4]:regions[5]]
# test whether using the histogram matching data augmentation method.(deprecated)
augment = False
if augment:
# histogram matching data augmentation
img_hist_match = Preprocessing.hist_match(
img_data.astype("float32"), img_data_matching_cast)
img_hist_match2 = Preprocessing.hist_match(img_data2.astype("float32"), img_data_matching_cast2)
# using B-spine interpolation for deformation (just like V-net did)
numcontrolpoints = 2
sigma = 15
else:
img_hist_match = img_data
img_hist_match2 = img_data2
# resize
resize_dim = (np.array(stage1_label_data.shape) * self.resize_ratio).astype('int')
img_data_resize = resize(img_hist_match.astype("float32"), resize_dim, order=1, preserve_range=True)
img_data2_resize = resize(img_hist_match2.astype("float32"), resize_dim, order=1, preserve_range=True)
stage1_label_resize = resize(stage1_label_data, resize_dim, order=0, preserve_range=True)
stage2_label_resize = resize(stage2_label, resize_dim, order=0, preserve_range=True)
stage3_label_resize = resize(stage3_label, resize_dim, order=0, preserve_range=True)
img_data_cast = img_data_resize.astype("float32")
img_data_cast2 = img_data2_resize.astype("float32")
label_data_cast = stage1_label_resize.astype('int32')
stage2_label_cast = stage2_label_resize.astype("int32")
stage3_label_cast = stage3_label_resize.astype("int32")
# normalization
img_norm = Preprocessing.Normalization(img_data_cast, axis=(0, 1, 2))
img_norm2 = Preprocessing.Normalization(img_data_cast2)
# randomly select a box anchor
l, w, h = label_data_cast.shape
l_rand = np.arange(l - self.patch_dim) # get a start point
w_rand = np.arange(w - self.patch_dim)
h_rand = np.arange(h - self.patch_dim)
np.random.shuffle(l_rand) # shuffle the start point series
np.random.shuffle(w_rand)
np.random.shuffle(h_rand)
pos = np.array([l_rand[0], w_rand[0], h_rand[0]]) # get the start point
# crop the volume to get the same size for the network
img_temp = copy.deepcopy(img_norm[pos[0]:pos[0] +
self.patch_dim, pos[1]:pos[1] +
self.patch_dim, pos[2]:pos[2] +
self.patch_dim, :])
img_temp2 = copy.deepcopy(img_norm2[pos[0]:pos[0] +
self.patch_dim, pos[1]:pos[1] +
self.patch_dim, pos[2]:pos[2] +
self.patch_dim, :])
# crop the label just like the volume data
label_temp = copy.deepcopy(
label_data_cast[pos[0]:pos[0] + self.patch_dim, pos[1]:pos[1] + self.patch_dim, pos[2]:pos[2] + self.patch_dim])
stage2_label_temp = copy.deepcopy(stage2_label_cast[pos[0]:pos[0] + self.patch_dim, pos[1]:pos[1] + self.patch_dim, pos[2]:pos[2] + self.patch_dim])
stage3_label_temp = copy.deepcopy(stage3_label_cast[pos[0]:pos[0] + self.patch_dim, pos[1]:pos[1] + self.patch_dim, pos[2]:pos[2] + self.patch_dim])
# get the batch data
batch_x[i, :, :, :, :] = img_temp
batch_x2[i, :, :, :, :] = img_temp2
batch_y[i, :, :, :] = label_temp
batch_y_stage2[i,:,:,:] = stage2_label_temp
batch_y_stage3[i,:,:,:] = stage3_label_temp
return batch_x, batch_x2, batch_y, batch_y_stage2, batch_y_stage3
# load volumes and the GT
def load_volumes_label(self, src_path, rename_map_flag):
'''
this function get the volume data and gt from the giving path
:param src_path: directory path of a patient
:return: GT and the volume data(width,height, slice, modality)
'''
# rename_map = [0, 1, 2, 4]
volume_list, seg_dict = self.data_dict_construct(src_path)
# assert len(volume_list) == 4
# assert seg_dict["mod"] == "seg"
if seg_dict["mod"] == "seg":
label_nib_data = nib.load(seg_dict["path"])
label = label_nib_data.get_data().copy()
# label = nib.load(seg_dict["path"]).get_data().copy()
# resolve the issue from resizing label, we first undertake binarization and then resize
stage1_label_data = np.zeros(label.shape, dtype='int32')
stage2_label_data = np.zeros(label.shape, dtype='int32')
stage3_label_data = np.zeros(label.shape, dtype='int32')
if rename_map_flag:
for i in range(len(self.rename_map)):
if i > 0:
stage1_label_data[label == self.rename_map[i]] = 1
else:
continue
# Cascaded structure,stage2,stage3 label prepare
stage2_label_data[label == 1] = 1
stage2_label_data[label == 4] = 1
stage3_label_data[label == 1] = 1
else:
stage1_label_data = copy.deepcopy(label).astype('int16')
stage2_label_data = copy.deepcopy(label).astype('int16')
stage3_label_data = copy.deepcopy(label).astype('int16')
else:
stage1_label_data = []
stage2_label_data = []
stage3_label_data = []
label_nib_data = []
img_all_modality = []
# order of the sequences [flair, T1, T1ce, T2]
for i in range(len(volume_list)):
volume = nib.load(volume_list[i]["path"])
img = volume.get_data().copy()
# resized_img = resize(img, resize_dim, order=1, preserve_range=True)
img_all_modality.append(img)
# choose different modalities for the network
if self.modalities == 4:
# all the modalities
img_data = img_all_modality
elif self.modalities == 3:
# select T1ce T1 Flair modalities
img_data = [img_all_modality[0], img_all_modality[2], img_all_modality[3]]
elif self.modalities == 2:
# two modalities
# choose T2 and Flair
img_data = [img_all_modality[0], img_all_modality[3]]
else:
# one modality
img_data = img_all_modality[0]
img_data = np.expand_dims(img_data, axis=0)
# input volume data
img_data2 = np.expand_dims(img_all_modality[2], axis=0)
img_array2 = np.array(img_data2, "float32").transpose((1,2,3,0))
# list to ndarray
img_array = np.array(img_data, "float32").transpose((1, 2, 3, 0))
return img_array, img_array2, stage1_label_data, stage2_label_data, stage3_label_data, volume
# construct data dict
def data_dict_construct(self, path):
'''
this function get the list of dictionary of the patients
:param path: path of the patient data
:return: list of dictionary including the path and the modality
'''
# list the image volumes and GT
files = os.listdir(path)
nii_list = sorted(glob('{}/*.nii.gz'.format(path)))
re_style = r'[\-\_\.]+'
volumn_list = []
seg_dict = {"mod": "None"}
for count, nii in enumerate(nii_list):
# modality mapping [seg, flair, T1, T1ce, T2]
mapping = [0, 1, 2, 3, 4]
file = os.path.basename(nii)
split_text = re.split(re_style, file)
modality = split_text[-3]
assert modality in ["flair", "seg", "t1", "t2", "t1ce"]
if modality == "seg":
data_dict = {"mod": modality, "path": nii, "count": mapping[0]}
elif modality == "flair":
data_dict = {"mod": modality, "path": nii, "count": mapping[1]}
elif modality == "t1":
data_dict = {"mod": modality, "path": nii, "count": mapping[2]}
elif modality == "t1ce":
data_dict = {"mod": modality, "path": nii, "count": mapping[3]}
else:
data_dict = {"mod": modality, "path": nii, "count": mapping[4]}
if data_dict["mod"] != "seg":
volumn_list.append(data_dict)
else:
seg_dict = {"mod": modality, "path": nii, "count": mapping[0]}
# sort the modalites in the list
volumn_list.sort(key=lambda x: x["count"])
return volumn_list, seg_dict
def data_augment_volume(self, *datalist , augmentation):
# first get the volume data from the data list
image1, image2, image3, mask1, mask2, mask3 = datalist
# Augmentation
# This requires the imgaug lib (https://github.com/aleju/imgaug)
if augmentation:
import imgaug
# Augmenters that are safe to apply to masks
# Some, such as Affine, have settings that make them unsafe, so always
# test your augmentation on masks
MASK_AUGMENTERS = ["Sequential", "SomeOf", "OneOf", "Sometimes",
"Fliplr", "Flipud", "CropAndPad",
"Affine", "PiecewiseAffine"]
def hook(images, augmenter, parents, default):
"""Determines which augmenters to apply to masks."""
return augmenter.__class__.__name__ in MASK_AUGMENTERS
# Store shapes before augmentation to compare
image1_shape = image1.shape
mask1_shape = mask1.shape
image2_shape = image2.shape
mask2_shape = mask2.shape
image3_shape = image3.shape
mask3_shape = mask3.shape
# Make augmenters deterministic to apply similarly to images and masks
det = augmentation.to_deterministic()
# image should be uint8!!
image1 = det.augment_image(image1)
image2 = det.augment_image(image2)
image3 = det.augment_image(image3)
# Change mask to np.uint8 because imgaug doesn't support np.bool
mask1 = det.augment_image(mask1.astype(np.uint8),
hooks=imgaug.HooksImages(activator=hook))
mask2 = det.augment_image(mask2.astype(np.uint8),
hooks=imgaug.HooksImages(activator=hook))
mask3 = det.augment_image(mask3.astype(np.uint8),
hooks=imgaug.HooksImages(activator=hook))
# Verify that shapes didn't change
assert image1.shape == image1_shape, "Augmentation shouldn't change image size"
assert mask1.shape == mask1_shape, "Augmentation shouldn't change mask size"
assert image2.shape == image2_shape, "Augmentation shouldn't change image size"
assert mask2.shape == mask2_shape, "Augmentation shouldn't change mask size"
assert image3.shape == image3_shape, "Augmentation shouldn't change image size"
assert mask3.shape == mask3_shape, "Augmentation shouldn't change mask size"
# Change mask back to bool
# masks = masks.astype(np.bool)
return image1,image2, image3, mask1, mask2, mask3
def data_augment(self, image, mask, augmentation):
# Augmentation
# This requires the imgaug lib (https://github.com/aleju/imgaug)
if augmentation:
import imgaug
# Augmenters that are safe to apply to masks
# Some, such as Affine, have settings that make them unsafe, so always
# test your augmentation on masks
MASK_AUGMENTERS = ["Sequential", "SomeOf", "OneOf", "Sometimes",
"Fliplr", "Flipud", "CropAndPad",
"Affine", "PiecewiseAffine"]
def hook(images, augmenter, parents, default):
"""Determines which augmenters to apply to masks."""
return augmenter.__class__.__name__ in MASK_AUGMENTERS
# Store shapes before augmentation to compare
image_shape = image.shape
mask_shape = mask.shape
# Make augmenters deterministic to apply similarly to images and masks
det = augmentation.to_deterministic()
# image should be uint8!!
images = det.augment_image(image)
# Change mask to np.uint8 because imgaug doesn't support np.bool
masks = det.augment_image(mask.astype(np.uint8),
hooks=imgaug.HooksImages(activator=hook))
# Verify that shapes didn't change
assert images.shape == image_shape, "Augmentation shouldn't change image size"
assert masks.shape == mask_shape, "Augmentation shouldn't change mask size"
# Change mask back to bool
# masks = masks.astype(np.bool)
return image, mask
def get_brain_region(volume_data):
# volume = nib.load(volume_path)
# volume_data = volume.get_data()
# get the brain region
indice_list = np.where(volume_data > 0)
# calculate the min and max of the indice, here volume have 3 channels
channel_0_min = min(indice_list[0])
channel_0_max = max(indice_list[0])
channel_1_min = min(indice_list[1])
channel_1_max = max(indice_list[1])
channel_2_min = min(indice_list[2])
channel_2_max = max(indice_list[2])
brain_volume = volume_data[channel_0_min:channel_0_max, channel_1_min:channel_1_max,channel_2_min:channel_2_max]
return (channel_0_min, channel_0_max, channel_1_min, channel_1_max, channel_2_min, channel_2_max)
class Preprocessing(object):
def __init__(self):
pass
# N4 Bias Field Correction by simpleITK
@staticmethod
def N4BiasFieldCorrection(src_path, dst_path):
'''
This function carry out BiasFieldCorrection for the files in a specific directory
:param src_path: path of the source file
:param dst_path: path of the target file
:return:
'''
print("N4 bias correction runs.")
inputImage = sitk.ReadImage(src_path)
maskImage = sitk.OtsuThreshold(inputImage, 0, 1, 200)
sitk.WriteImage(maskImage, dst_path)
inputImage = sitk.Cast(inputImage, sitk.sitkFloat32)
corrector = sitk.N4BiasFieldCorrectionImageFilter()
# corrector.SetMaximumNumberOfIterations(10)
output = corrector.Execute(inputImage, maskImage)
sitk.WriteImage(output, dst_path)
print("Finished N4 Bias Field Correction.....")
# normalize the data(zero mean and unit variance)
@staticmethod
def Normalization(volume, axis=None):
mean = np.mean(volume, axis=axis)
std = np.std(volume, axis=axis)
norm_volume = (volume - mean) / std
return norm_volume
# data augmentation by histogram matching
@staticmethod
def hist_match(source, template):
"""
Adjust the pixel values of a grayscale image such that its histogram
matches that of a target image
Arguments:
-----------
source: np.ndarray
Image to transform; the histogram is computed over the flattened
array
template: np.ndarray
Template image; can have different dimensions to source(randomly choose from the training dataset)
Returns:
-----------
matched: np.ndarray
The transformed output image
"""
oldshape = source.shape
source = source.ravel()
template = template.ravel()
# get the set of unique pixel values and their corresponding indices and
# counts
s_values, bin_idx, s_counts = np.unique(source, return_inverse=True,
return_counts=True)
t_values, t_counts = np.unique(template, return_counts=True)
# take the cumsum of the counts and normalize by the number of pixels to
# get the empirical cumulative distribution functions for the source and
# template images (maps pixel value --> quantile)
s_quantiles = np.cumsum(s_counts).astype(np.float64)
s_quantiles /= s_quantiles[-1]
t_quantiles = np.cumsum(t_counts).astype(np.float64)
t_quantiles /= t_quantiles[-1]
# interpolate linearly to find the pixel values in the template image
# that correspond most closely to the quantiles in the source image
# interp_t_values = np.zeros_like(source,dtype=float)
interp_t_values = np.interp(s_quantiles, t_quantiles, t_values)
return interp_t_values[bin_idx].reshape(oldshape)
# data augmentation by deforming
@staticmethod
def produceRandomlyDeformedImage(image, label, numcontrolpoints, stdDef, seed=1):
'''
This function comes from V-net,deform a image by B-spine interpolation
:param image: images ,numpy array
:param label: labels,numpy array
:param numcontrolpoints: control point,B-spine interpolation parameters,take 2 for default
:param stdDef: Deviation,B-spine interpolation parameters,take 15 for default
:return: Deformed images and GT in numpy array
'''
sitkImage = sitk.GetImageFromArray(image, isVector=False)
sitklabel = sitk.GetImageFromArray(label, isVector=False)
transfromDomainMeshSize = [numcontrolpoints] * sitkImage.GetDimension()
tx = sitk.BSplineTransformInitializer(
sitkImage, transfromDomainMeshSize)
params = tx.GetParameters()
paramsNp = np.asarray(params, dtype=float)
# 设置种子值,确保多通道时两个通道变换程度一样
np.random.seed(seed)
paramsNp = paramsNp + np.random.randn(paramsNp.shape[0]) * stdDef
# remove z deformations! The resolution in z is too bad
paramsNp[0:int(len(params) / 3)] = 0
params = tuple(paramsNp)
tx.SetParameters(params)
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(sitkImage)
resampler.SetInterpolator(sitk.sitkLinear)
resampler.SetDefaultPixelValue(0)
resampler.SetTransform(tx)
resampler.SetDefaultPixelValue(0)
outimgsitk = resampler.Execute(sitkImage)
outlabsitk = resampler.Execute(sitklabel)
outimg = sitk.GetArrayFromImage(outimgsitk)
outimg = outimg.astype(dtype=np.float32)
outlbl = sitk.GetArrayFromImage(outlabsitk)
# outlbl = (outlbl > 0.5).astype(dtype=np.float32)
return outimg, outlbl
class Evaluation(object):
def __init__(self):
pass
# save 3d volume as slices
def save_slice_img(self, volume_path, output_path):
file_name = os.path.basename(volume_path)
output_dir = os.path.join(output_path, file_name)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
else:
pass
input_volume = nib.load(volume_path).get_data()
# mapping to 0-1
vol_max = np.max(input_volume)
vol_min = np.min(input_volume)
input_unit = (input_volume-vol_min)/(vol_max - vol_min)
width, height, depth= input_unit.shape
for i in range(0, depth):
slice_path = os.path.join(output_dir, str(i)+'.png')
img_i = input_unit[:, :, i]
# normalize to 0-255
img_i = (img_i*255).astype('uint8')
# cv.imwrite(slice_path, img_i)
return input_unit
def save_slice_img_label(self, img_volume, pre_volume, gt_volume,
output_path, file_name, show_mask=False, show_gt = False):
assert img_volume.shape == pre_volume.shape
if show_gt:
assert img_volume.shape == gt_volume.shape
width, height, depth = img_volume.shape
# gray value mapping from MRI value to pixel value(0-255)
volume_max = np.max(img_volume)
volume_min = np.min(img_volume)
volum_mapped = (img_volume-volume_min)/(volume_max-volume_min)
volum_mapped = (255*volum_mapped).astype('uint8')
# construct a directory for each volume to save slices
dir_volume = os.path.join(output_path, file_name)
if not os.path.exists(dir_volume):
os.makedirs(dir_volume)
else:
pass
for i in range(depth):
img_slice = volum_mapped[:, :, i]
pre_slice = pre_volume[:, :, i]
if show_gt:
gt_slice = gt_volume[:, :, i]
else:
gt_slice = []
self.save_contour_label(img=img_slice, pre=pre_slice, gt=gt_slice,
save_path=dir_volume, file_name=i,show_mask=show_mask,show_gt=show_gt)
def apply_mask(self, image, mask, color, alpha=0.5):
"""Apply the given mask to the image.
"""
for c in range(image.shape[-1]):
image[:, :, c] = np.where(mask == 1,
image[:, :, c] *
(1 - alpha) + alpha * color[c] * 255,
image[:, :, c])
return image
def random_colors(self, N, bright=True):
"""
Generate random colors.
To get visually distinct colors, generate them in HSV space then
convert to RGB.
"""
brightness = 1.0 if bright else 0.7
hsv = [(i / N, 1, brightness) for i in range(N)]
colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
random.shuffle(colors)
return colors
def save_contour_label(self, img, pre, gt=None, save_path='', file_name=None, show_mask=False, show_gt = False):
# single channel to multi-channel
img = np.expand_dims(img, axis=-1)
img = np.tile(img, (1, 1, 3))
height, width = img.shape[:2]
_, ax = plt.subplots(1, figsize=(height, width))
# Generate random colors
# colors = self.random_colors(4)
# Prediction result is illustrated as red and the groundtruth is illustrated as blue
colors = [[1.0, 0, 0], [0, 0, 1.0]]
# Show area outside image boundaries.
# ax.set_ylim(height + 10, -10)
# ax.set_xlim(-10, width + 10)
ax.set_ylim(height + 0, 0)
ax.set_xlim(0, width + 0)
ax.axis('off')
# ax.set_title("volume mask")
masked_image = img.astype(np.uint32).copy()
if show_mask:
masked_image = self.apply_mask(masked_image, pre, colors[0])
if show_gt:
masked_image = self.apply_mask(masked_image, gt, colors[1])
# Mask Polygon
# Pad to ensure proper polygons for masks that touch image edges.
padded_mask_pre = np.zeros(
(pre.shape[0] + 2, pre.shape[1] + 2), dtype=np.uint8)
padded_mask_pre[1:-1, 1:-1] = pre
contours = find_contours(padded_mask_pre, 0.5)
for verts in contours:
# reduce padding and flipping from (y, x) to (x, y)
verts = np.fliplr(verts) - 1
p = Polygon(verts, facecolor="none", edgecolor=colors[0], linewidth=1)
ax.add_patch(p)
if show_gt:
padded_mask_gt = np.zeros((gt.shape[0] + 2, gt.shape[1] + 2), dtype=np.uint8)
padded_mask_gt[1:-1, 1:-1] = gt
contours_gt = find_contours(padded_mask_gt, 0.5)
for contour in contours_gt:
contour = np.fliplr(contour) -1
p_gt = Polygon(contour, facecolor="none", edgecolor=colors[1], linewidth=1)
ax.add_patch(p_gt)
# reduce the blank part generated by plt and keep the original resolution
fig = plt.gcf()
fig.set_size_inches(height/37.5, width/37.5)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
ax.imshow(masked_image.astype(np.uint8))
# plt.show()
fig.savefig('{}/{}.png'.format(save_path, file_name))
# clear the image after saving
plt.cla()
plt.close(fig)
def save_slice_volume(volume, save_path):
'''
the function save volume data to slices in the specific directory
:param volume: input volume data
:param save_path:
:return:
'''
shape = volume.shape
# translate intensity to 0-255
v_max = np.max(volume)
v_min = np.min(volume)
volume_norm = (volume - v_min) / (v_max - v_min)
volume_norm = (volume_norm * 255).astype("int")
if not os.path.exists(save_path):
os.makedirs(save_path)
for i in range(shape[-1]):
abs_path = os.path.join(save_path, str(i)+".png")
cv.imwrite(abs_path, volume_norm[..., i])
# calculate the cube information
def fit_cube_param(vol_dim, cube_size, ita):
dim = np.asarray(vol_dim)
fold = dim / cube_size + ita
ovlap = np.ceil(
np.true_divide(
(fold * cube_size - dim),
(fold - 1))) # dim+ita*cubesize-dim
ovlap = ovlap.astype('int')
# print( "ovlap:", str( ovlap ) )#[62 62 86]
fold = np.ceil(np.true_divide((dim + (fold - 1) * ovlap), cube_size))
fold = fold.astype('int')
# print( "fold:", str( fold) ) fold: [8 8 6]
return fold, ovlap
# decompose volume into list of cubes
def decompose_vol2cube_brain(vol_data, cube_size, n_chn, ita):
cube_list = []
fold, ovlap = fit_cube_param(vol_data.shape[0:3], cube_size, ita)
dim = np.asarray(vol_data.shape[0:3]) # [307, 307, 143]
# decompose
for R in range(0, fold[0]):
r_s = R * cube_size - R * ovlap[0]
r_e = r_s + cube_size
if r_e >= dim[0]: # see if exceed the boundry
r_s = dim[0] - cube_size
r_e = r_s + cube_size
for C in range(0, fold[1]):
c_s = C * cube_size - C * ovlap[1]
c_e = c_s + cube_size
if c_e >= dim[1]:
c_s = dim[1] - cube_size
c_e = c_s + cube_size
for H in range(0, fold[2]):
h_s = H * cube_size - H * ovlap[2]
h_e = h_s + cube_size
if h_e >= dim[2]:
h_s = dim[2] - cube_size
h_e = h_s + cube_size
# partition multiple channels
cube_temp = vol_data[r_s:r_e, c_s:c_e, h_s:h_e, :]
# By default batch_size = 1
cube_batch = np.zeros(
[1, cube_size, cube_size, cube_size, n_chn]).astype('float32')
cube_batch[0, :, :, :, :] = copy.deepcopy(cube_temp)
# save
cube_list.append(cube_batch)
return cube_list
# compose list of label cubes into a label volume
def compose_label_cube2vol(cube_list, vol_dim, cube_size, ita, class_n):
# get parameters for compose
fold, ovlap = fit_cube_param(vol_dim, cube_size, ita)
# create label volume for all classes
label_classes_mat = (
np.zeros([vol_dim[0], vol_dim[1], vol_dim[2], class_n])).astype('int32')
idx_classes_mat = (
np.zeros([cube_size, cube_size, cube_size, class_n])).astype('int32')
p_count = 0
for R in range(0, fold[0]):
r_s = R * cube_size - R * ovlap[0]
r_e = r_s + cube_size
if r_e >= vol_dim[0]:
r_s = vol_dim[0] - cube_size
r_e = r_s + cube_size
for C in range(0, fold[1]):
c_s = C * cube_size - C * ovlap[1]
c_e = c_s + cube_size
if c_e >= vol_dim[1]:
c_s = vol_dim[1] - cube_size
c_e = c_s + cube_size
for H in range(0, fold[2]):
h_s = H * cube_size - H * ovlap[2]
h_e = h_s + cube_size
if h_e >= vol_dim[2]:
h_s = vol_dim[2] - cube_size
h_e = h_s + cube_size
# histogram for voting (one-hot)
for k in range(class_n):
idx_classes_mat[:, :, :, k] = (cube_list[p_count] == k)
# accumulation
label_classes_mat[r_s:r_e,
c_s:c_e,
h_s:h_e,
:] = label_classes_mat[r_s:r_e,
c_s:c_e,
h_s:h_e,
:] + idx_classes_mat
p_count += 1
# print 'label mat unique:'
# print np.unique(label_mat)
compose_vol = np.argmax(label_classes_mat, axis=3)
# print np.unique(label_mat)
return compose_vol
# compose list of probability cubes into a probability volumes
def compose_prob_cube2vol(cube_list, vol_dim, cube_size, ita, class_n):
# get parameters for compose
fold, ovlap = fit_cube_param(vol_dim, cube_size, ita)
# create label volume for all classes
map_classes_mat = (
np.zeros([vol_dim[0], vol_dim[1], vol_dim[2], class_n])).astype('float32')
cnt_classes_mat = (
np.zeros([vol_dim[0], vol_dim[1], vol_dim[2], class_n])).astype('float32')
p_count = 0
for R in range(0, fold[0]):
r_s = R * cube_size - R * ovlap[0]
r_e = r_s + cube_size
if r_e >= vol_dim[0]:
r_s = vol_dim[0] - cube_size
r_e = r_s + cube_size
for C in range(0, fold[1]):
c_s = C * cube_size - C * ovlap[1]
c_e = c_s + cube_size
if c_e >= vol_dim[1]:
c_s = vol_dim[1] - cube_size
c_e = c_s + cube_size
for H in range(0, fold[2]):
h_s = H * cube_size - H * ovlap[2]
h_e = h_s + cube_size
if h_e >= vol_dim[2]:
h_s = vol_dim[2] - cube_size
h_e = h_s + cube_size
# accumulation
map_classes_mat[r_s:r_e,
c_s:c_e,
h_s:h_e,
:] = map_classes_mat[r_s:r_e,
c_s:c_e,
h_s:h_e,
:] + cube_list[p_count]
cnt_classes_mat[r_s:r_e,
c_s:c_e,
h_s:h_e,
:] = cnt_classes_mat[r_s:r_e,
c_s:c_e,
h_s:h_e,
:] + 1.0
p_count += 1
# elinimate NaN
nan_idx = (cnt_classes_mat == 0)
cnt_classes_mat[nan_idx] = 1.0
# average
compose_vol = map_classes_mat / cnt_classes_mat
return compose_vol
# Remove small connected components
def remove_minor_cc(vol_data, rej_ratio, rename_map):
"""Remove small connected components refer to rejection ratio"""
"""Usage
# rename_map = [0, 205, 420, 500, 550, 600, 820, 850]
# nii_path = '/home/xinyang/project_xy/mmwhs2017/dataset/ct_output/test/test_4.nii'
# vol_file = nib.load(nii_path)
# vol_data = vol_file.get_data().copy()
# ref_affine = vol_file.affine
# rem_vol = remove_minor_cc(vol_data, rej_ratio=0.2, class_n=8, rename_map=rename_map)
# # save
# rem_path = 'rem_cc.nii'
# rem_vol_file = nib.Nifti1Image(rem_vol, ref_affine)
# nib.save(rem_vol_file, rem_path)
#===# possible be parallel in future
"""
rem_vol = copy.deepcopy(vol_data)
class_n = len(rename_map)
# retrieve all classes
for c in range(1, class_n):
print('processing class %d...' % c)
class_idx = (vol_data == rename_map[c]) * 1
class_vol = np.sum(class_idx)
labeled_cc, num_cc = measurements.label(class_idx)
# retrieve all connected components in this class
for cc in range(1, num_cc + 1):
single_cc = ((labeled_cc == cc) * 1)
single_vol = np.sum(single_cc)
# remove if too small
if single_vol / (class_vol * 1.0) < rej_ratio:
rem_vol[labeled_cc == cc] = 0
return rem_vol
def background_num_to_save(input_gt, fg_ratio, bg_ratio):
background_num = tf.reduce_sum(input_gt[:, :, :, :, 0])
total_num = tf.reduce_sum(input_gt)
foreground_num = total_num - background_num
# save_back_ground_num = tf.reduce_max(
# [2 * foreground_num, background_num / 32]) # set the number of background samples to reserve
save_back_ground_num = tf.reduce_max(
[fg_ratio * foreground_num, background_num / bg_ratio]) # set the number of background samples to reserve
save_back_ground_num = tf.clip_by_value(
save_back_ground_num, 0, background_num)
return save_back_ground_num
def no_background(input_gt):
return input_gt
def exist_background(input_gt, pred, save_back_ground_num):
batch, in_depth, in_height, in_width, in_channels = [
int(d) for d in input_gt.get_shape()]
pred_data = pred[:, :, :, :, 0]
gt_backgound_data = 1 - input_gt[:, :, :, :, 0]
pred_back_ground_data = tf.reshape(
pred_data, (batch, in_depth * in_height * in_width))
gt_back_ground_data = tf.reshape(
gt_backgound_data,
(batch,
in_depth *
in_height *
in_width))
new_pred_data = pred_back_ground_data + gt_back_ground_data
mask = []
for i in range(batch):
gti = -1 * new_pred_data[i, :]
max_k_number, index = tf.nn.top_k(
gti, save_back_ground_num)
max_k = tf.reduce_min(max_k_number)
one = tf.ones_like(gti) # all 1 mask
zero = tf.zeros_like(gti) # all 0 mask
mask_slice = tf.where(gti < max_k, x=zero, y=one)
mask_slice = tf.reshape(mask_slice, [in_depth, in_height, in_width])
mask.append(mask_slice)
mask = tf.expand_dims(mask, -1)
other_mask = tf.ones([batch,
in_depth,
in_height,
in_width,
in_channels - 1],
tf.float32)
full_mask = tf.concat([mask, other_mask], 4)
input_gt = full_mask * input_gt
return input_gt
# Get a background mask for the groundtruth so that we can
# discard the unnecessary background information
def produce_mask_background(input_gt, pred, fg_ratio, bg_ratio):
save_back_ground_num = background_num_to_save(
input_gt, fg_ratio, bg_ratio) # Get the background numbers to reserve from groundtruth
save_back_ground_num = tf.cast(
save_back_ground_num,
dtype=tf.int32)
product = tf.cond(
save_back_ground_num < 5,
lambda: no_background(input_gt),
lambda: exist_background(
input_gt,
pred,
save_back_ground_num))
return product
def fillhole(input_image):
'''
input gray binary image get the filled image by floodfill method
Note: only holes surrounded in the connected regions will be filled.
:param input_image:
:return:
'''
im_flood_fill = input_image.copy()
h, w = input_image.shape[:2]
mask = np.zeros((h + 2, w + 2), np.uint8)
im_flood_fill = im_flood_fill.astype("uint8")
cv.floodFill(im_flood_fill, mask, (0, 0), 255)
im_flood_fill_inv = cv.bitwise_not(im_flood_fill)