-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.py
153 lines (118 loc) · 6.75 KB
/
post.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
# -*- coding: utf-8 -*-
# @Time : 2021/11/24 0024 12:14
# @Author : Xiaofeng
# @FileName: torch[post]
# @Software: Pycharm
# @Usages :
import copy
import os
import SimpleITK as sitk
import numpy as np
import skimage.measure as measure
import skimage.morphology as morphology
import skimage.io
def pathExist(path):
if not os.path.exists(path):
os.makedirs(path)
return path
root_dir = r'G:\Public_Dataset\Radiology\3Dircadb\EE-Net\data_resize\seg'
out_dir = r'G:\Public_Dataset\Radiology\3Dircadb\EE-Net\data_resize\seg_post'
pathExist(out_dir)
for idx, file in enumerate(sorted(os.listdir(root_dir))):
if idx >= 0:
print('processing label file------------')
seg = sitk.ReadImage(os.path.join(root_dir, file), sitk.sitkUInt8)
seg_array = sitk.GetArrayFromImage(seg)
ss = np.zeros_like(seg_array)
seg_copy = copy.deepcopy(seg_array)
seg1_array = np.zeros_like(seg_array)
seg2_array = np.zeros_like(seg_array)
seg1_array[seg_array == 1] = 1
seg2_array[seg_array == 2] = 1
# 对门静脉进行后处理
seg1_array = morphology.binary_dilation(seg1_array)
seg1_array = morphology.binary_erosion(seg1_array)
seg1_array = morphology.binary_dilation(seg1_array)
seg1_array = morphology.binary_erosion(seg1_array)
seg1_array = morphology.binary_dilation(seg1_array)
new_seg1 = np.zeros_like(seg1_array)
# 分别对三个轴面移除细小区域
for i in range(int(seg1_array.shape[0])):
# print(i)
seg_slice = seg1_array[i, :, :]
new_seg1[i, :, :] = skimage.morphology.remove_small_objects(seg_slice, min_size=5,
connectivity=1).astype(seg1_array.dtype)
new_seg1[i, :, :] = skimage.morphology.remove_small_holes(seg_slice, area_threshold=200,
connectivity=1).astype(seg1_array.dtype)
for i in range(int(seg1_array.shape[1])):
# print(i)
seg_slice = seg1_array[:, i, :]
new_seg1[:, i, :] = skimage.morphology.remove_small_objects(seg_slice, min_size=5,
connectivity=1).astype(seg1_array.dtype)
new_seg1[:, i, :] = skimage.morphology.remove_small_holes(seg_slice, area_threshold=200,
connectivity=1).astype(seg1_array.dtype)
for i in range(int(seg1_array.shape[2])):
# print(i)
seg_slice = seg1_array[:, :, i]
new_seg1[:, :, i] = skimage.morphology.remove_small_objects(seg_slice, min_size=5,
connectivity=1).astype(seg1_array.dtype)
new_seg1[:, :, i] = skimage.morphology.remove_small_holes(seg_slice, area_threshold=200,
connectivity=1).astype(seg1_array.dtype)
# save_seg = save_seg * seg_copy
# 对肝静脉进行后处理
seg2_array = morphology.binary_dilation(seg2_array)
seg2_array = morphology.binary_erosion(seg2_array)
seg2_array = morphology.binary_dilation(seg2_array)
seg2_array = morphology.binary_erosion(seg2_array)
new_seg2 = np.zeros_like(seg2_array)
# 分别对三个轴面移除细小区域
for i in range(int(seg2_array.shape[0])):
# print(i)
seg_slice = seg2_array[i, :, :]
new_seg2[i, :, :] = skimage.morphology.remove_small_objects(seg_slice, min_size=5,
connectivity=1).astype(seg2_array.dtype)
new_seg2[i, :, :] = skimage.morphology.remove_small_holes(seg_slice, area_threshold=200,
connectivity=1).astype(seg2_array.dtype)
for i in range(int(seg2_array.shape[1])):
# print(i)
seg_slice = seg2_array[:, i, :]
new_seg2[:, i, :] = skimage.morphology.remove_small_objects(seg_slice, min_size=5,
connectivity=1).astype(seg2_array.dtype)
new_seg2[:, i, :] = skimage.morphology.remove_small_holes(seg_slice, area_threshold=200,
connectivity=1).astype(seg2_array.dtype)
for i in range(int(seg2_array.shape[2])):
# print(i)
seg_slice = seg2_array[:, :, i]
new_seg2[:, :, i] = skimage.morphology.remove_small_objects(seg_slice, min_size=5,
connectivity=1).astype(seg2_array.dtype)
new_seg2[:, :, i] = skimage.morphology.remove_small_holes(seg_slice, area_threshold=200,
connectivity=1).astype(seg2_array.dtype)
liver_seg1 = morphology.remove_small_objects(new_seg1, 10000, connectivity=1, in_place=True)
liver_seg1 = morphology.binary_dilation(liver_seg1)
liver_seg1 = morphology.binary_erosion(liver_seg1)
liver_seg1 = morphology.binary_dilation(liver_seg1)
liver_seg1 = morphology.binary_erosion(liver_seg1)
liver_seg1 = morphology.binary_dilation(liver_seg1)
liver_seg1 = morphology.binary_erosion(liver_seg1)
liver_seg1 = morphology.binary_dilation(liver_seg1)
liver_seg1 = morphology.binary_erosion(liver_seg1)
liver_seg1 = morphology.remove_small_objects(liver_seg1, 1000, connectivity=1, in_place=True)
save_seg1 = liver_seg1.astype(np.uint8)
liver_seg2 = morphology.remove_small_objects(new_seg2, 1000, connectivity=1, in_place=True)
liver_seg2 = morphology.binary_dilation(liver_seg2)
liver_seg2 = morphology.binary_erosion(liver_seg2)
liver_seg2 = morphology.binary_dilation(liver_seg2)
liver_seg2 = morphology.binary_erosion(liver_seg2)
liver_seg2 = morphology.binary_dilation(liver_seg2)
liver_seg2 = morphology.binary_erosion(liver_seg2)
liver_seg2 = morphology.binary_dilation(liver_seg2)
liver_seg2 = morphology.binary_erosion(liver_seg2)
liver_seg2 = morphology.remove_small_objects(liver_seg2, 1000, connectivity=1, in_place=True)
save_seg2 = liver_seg2.astype(np.uint8)
ss[save_seg1 == 1] = 1
ss[save_seg2 == 1] = 2
save = sitk.GetImageFromArray(ss)
save.SetDirection(seg.GetDirection())
save.SetOrigin(seg.GetOrigin())
save.SetSpacing(seg.GetSpacing())
sitk.WriteImage(save, os.path.join(out_dir, file))