-
Notifications
You must be signed in to change notification settings - Fork 0
/
morph.py
66 lines (62 loc) · 2.68 KB
/
morph.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
import numpy as np
import scipy.ndimage.morphology as morph
import scipy.ndimage as ndimage
import loader
import SimpleITK as sitk
import os
def create_circle(radius=3):
return create_ellipse(radius, radius)
def create_ellipse(a, b):
#n1, n2 = a*2 + 1, b*2+1
#y,x = np.ogrid[-a:n1-a, -b:n2-b]
y,x = np.ogrid[-a:a, -b:b]
mask = ((x*x) / (a*a) + (y*y)/(b*b)) <= 1
return mask.astype(int)
def morph_segment(image, structure):
labels = []
for i in range(image.shape[0]):
segmentation = image[i] > 0
segmentation = morph.binary_opening(segmentation, structure=structure)
segmentation = morph.binary_fill_holes(segmentation, structure=structure)
labels.append(segmentation)
return np.array(labels)
if __name__ == "__main__":
input_path = "./Data/normalized/"
output_path = "./Data/output/"
classes = 0
images, slice_image, t2images, labels, names, dsos = loader.load_input(input_path, classes, channels=False)
print(names[0])
for image4d, Name, dso in zip(images, names, dsos):
for size in [1, 2, 3, 4, 5]:
labels = []
for image3d in image4d:
segmentation = morph_segment(image3d, create_circle(size))
label_mask, no_features = ndimage.label(segmentation, structure=ndimage.generate_binary_structure(3,2))
max_mask = 0
max_i = 0
for i in range(1,no_features+1):
count = np.count_nonzero(label_mask==i)
if count > max_i:
max_i = count
max_mask = i
label = np.zeros_like(label_mask)
if max_mask > 0:
label[label_mask==max_mask] = 256.0
#labels.append(label)
#label = np.argmax(labels, axis=3)*256.
#if no_features > 0:
# label = label_mask*256/no_features
#else:
# label = label_mask*256.0
label = label[:, :, :, np.newaxis]
#print(label.shape, no_features, np.max(label))
labels.append(label)
Label = sitk.JoinSeries([sitk.GetImageFromArray(label) for label in labels])
Label.SetDirection(dso[0])
Label.SetSpacing(dso[1])
Label.SetOrigin(dso[2])
if True or np.max(label) > 0:
if(not os.path.exists(output_path)):
os.mkdir(output_path)
sitk.WriteImage(Label, os.path.join(output_path, Name + "-morph." + str(size) + ".label.mhd"))
print("written output: " + os.path.join(output_path, Name + "-morph." + str(size) + ".label.mhd"))