-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare.py
More file actions
289 lines (253 loc) · 9.1 KB
/
prepare.py
File metadata and controls
289 lines (253 loc) · 9.1 KB
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
import os
import shutil
import numpy as np
from copy import deepcopy as copy
import cv2
import math
import datetime
import time
from scipy import ndimage as ndi
from scipy.io import loadmat
import numpy as npS
import scipy
from scipy.ndimage.interpolation import zoom
from glob import glob
from skimage import measure, morphology
import SimpleITK as sitk
from scipy.ndimage.morphology import binary_dilation, generate_binary_structure, distance_transform_edt
from skimage.morphology import convex_hull_image, disk, binary_closing
from multiprocessing import Pool
from functools import partial
import sys
sys.path.append('preprocessing')
# from preprocessing.step1b import step1_python
from preprocessing.step1 import step1_python,load_itk_image
# from preprocessing.step1 import load_itk_image
import warnings
import pydicom as dicom
def load_scan(path):
"""
:param path: input CT path, dicom format
:return: CT image
"""
#slices = [dicom.read_file(path + '/' + s, force = True) for s in os.listdir(path)]
slices = []
for s in os.listdir(path):
if s.endswith('.dcm'):
slices.append(dicom.read_file(path + '/' + s, force=True))
slices.sort(key=lambda x: float(x.ImagePositionPatient[2]))
if slices[0].ImagePositionPatient[2] == slices[1].ImagePositionPatient[2]:
sec_num = 2
while slices[0].ImagePositionPatient[2] == slices[sec_num].ImagePositionPatient[2]:
sec_num = sec_num + 1
slice_num = int(len(slices) / sec_num)
slices.sort(key=lambda x:float(x.InstanceNumber))
slices = slices[0:slice_num]
slices.sort(key=lambda x:float(x.ImagePositionPatient[2]))
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
except:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
def resample(imgs, spacing, new_spacing, order=1):
"""
:param imgs: CT image
:param spacing: CT original voxel spacing
:param new_spacing: target voxel spacing
:param order: interpolation choice
:return: CT image with new voxel spacing, new voxel spacing
"""
if len(imgs.shape) == 3:
new_shape = np.round(imgs.shape * spacing / new_spacing)
true_spacing = spacing * imgs.shape / new_shape
resize_factor = new_shape / imgs.shape
imgs = zoom(imgs, resize_factor, mode='nearest', order=order)
return imgs, true_spacing
elif len(imgs.shape) == 4:
n = imgs.shape[-1]
newimg = []
for i in range(n):
slice = imgs[:,:,:,i]
newslice, true_spacing = resample(slice, spacing, new_spacing)
newimg.append(newslice)
newimg = np.transpose(np.array(newimg), [1, 2, 3, 0])
return newimg, true_spacing
else:
raise ValueError('wrong shape')
def process_mask(mask):
"""
:param mask: input lung mask
:return: convex hull processing on lung mask to avoid over-segmentation
"""
convex_mask = np.copy(mask)
for i_layer in range(convex_mask.shape[0]):
mask1 = np.ascontiguousarray(mask[i_layer])
if np.sum(mask1) > 0:
mask2 = convex_hull_image(mask1)
if np.sum(mask2) > 1.5*np.sum(mask1):
mask2 = mask1
else:
mask2 = mask1
convex_mask[i_layer] = mask2
struct = generate_binary_structure(3, 1)
dilatedMask = binary_dilation(convex_mask, structure=struct, iterations=10)
return dilatedMask
def lumTrans_hu(img):
"""
:param img: CT image
:return: Hounsfield Unit window clipped and normalized
"""
lungwin = np.array([-1000.,400.])
newimg = (img-lungwin[0])/(lungwin[1]-lungwin[0])
newimg[newimg < 0] = 0
newimg[newimg > 1] = 1
newimg = (newimg*255).astype('uint8')
return newimg
def save_itk(image, origin, spacing, filename):
"""
:param image: images to be saved
:param origin: CT origin
:param spacing: CT spacing
:param filename: save name
:return: None
"""
if type(origin) != tuple:
if type(origin) == list:
origin = tuple(reversed(origin))
else:
origin = tuple(reversed(origin.tolist()))
if type(spacing) != tuple:
if type(spacing) == list:
spacing = tuple(reversed(spacing))
else:
spacing = tuple(reversed(spacing.tolist()))
itkimage = sitk.GetImageFromArray(image, isVector=False)
itkimage.SetSpacing(spacing)
itkimage.SetOrigin(origin)
sitk.WriteImage(itkimage, filename, True)
def savenpy(data_path, prep_folder):
"""
:param data_path: input CT data path
:param prep_folder:
:return: None
"""
resolution = np.array([1, 1, 1])
#windows系统添加的是反斜杠\
name = data_path.split('\\')[-1].split('.nii')[0]#获得文件名
# name = data_path.split('/')[-1].split('.nii')[0]
assert (os.path.exists(data_path) is True)
im, m1, m2, mtotal, origin, spacing = step1_python(data_path)
# print('Origin: ', origin, ' Spacing: ', spacing, 'img shape: ', im.shape)
Mask = m1 + m2
xx, yy, zz = np.where(Mask)
box = np.array([[np.min(xx), np.max(xx)], [np.min(yy), np.max(yy)], [np.min(zz),np.max(zz)]])
margin = 5
box = np.vstack([np.max([[0, 0, 0], box[:, 0] - margin], 0), np.min([np.array(Mask.shape), box[:, 1] + margin], axis=0).T]).T
# save the lung mask
data_savepath = os.path.join(prep_folder, name+'_lung_mask.nii.gz')
Mask_crop = Mask[box[0, 0]:box[0, 1],
box[1,0]:box[1,1],
box[2,0]:box[2,1]]
save_itk(Mask_crop.astype(dtype='uint8'), origin, spacing, data_savepath)
# convex_mask = m1
# dm1 = process_mask(m1)
# dm2 = process_mask(m2)
# dilatedMask = (dm1 + dm2) | mtotal
# Mask = m1+m2
# dilatedMask = dilatedMask.astype('uint8')
# Mask = Mask.astype('uint8')
# save the CT image
im[np.isnan(im)] = -2000
sliceim_hu = lumTrans_hu(im)
shapeorg = sliceim_hu.shape
box_shape = np.array([[0, shapeorg[0]], [0, shapeorg[1]], [0, shapeorg[2]]])
sliceim2_hu = sliceim_hu[box[0, 0]:box[0, 1],
box[1,0]:box[1,1],
box[2,0]:box[2,1]]
# save box (image original shape and cropped window region)
box = np.concatenate([box, box_shape], axis=0)
np.save(os.path.join(prep_folder, name+'_box.npy'), box)
# save processed image
data_savepath = os.path.join(prep_folder, name+'_clean_hu.nii.gz')
# data_savepath = os.path.join(prep_folder, name + '_clean_hu.nii.gz')
save_itk(sliceim2_hu.astype(dtype='uint8'), origin, spacing, data_savepath)
print(name)
return
def preprocess_CT(inputpath=None, savepath=None):
"""
Preprocess the CT images in the input path to extract lung field
Save the processed images in the save path
:param inputpath: input data path
:param savepath: output save path
:return: save directory path
"""
warnings.filterwarnings("ignore")
warnings.simplefilter(action='ignore', category=FutureWarning)
if not os.path.exists(savepath):
os.mkdir(savepath)
filelist = glob(os.path.join(inputpath, '*.nii*')) # default nifty format
print(inputpath, filelist)
for curfilepath in filelist:
start_time = time.time()
print('starting preprocessing lung CT')
savenpy(data_path=curfilepath, prep_folder=savepath)
end_time = time.time()
print('end preprocessing lung CT, time %d seconds'%(end_time-start_time))
return savepath
def preprocess_CTlabel(labelInputpath,boxpath,savepath):
"""
将CT图像的标签图像裁剪为clean_hu文件大小
"""
labelfilelist = glob(os.path.join(labelInputpath,'*.gz'))
boxfilelist = glob(os.path.join(boxpath,'*.npy'))
i=0;
for curfilepath in labelfilelist:
curboxpath = boxfilelist[i]
boxnum = curboxpath.split('_')[-3]
labelnum = curfilepath.split('_')[-2]
if boxnum == labelnum:
box = np.load(curboxpath)
print(box)
labelImg,labelOrigin,labelShape = load_itk_image(curfilepath)
labelImg1 = labelImg[box[0, 0]:box[0, 1],
box[1,0]:box[1,1],
box[2,0]:box[2,1]]
# labelName = savepath + '/'+curfilepath.split('/')[-1]
labelName = savepath + '\\' + curfilepath.split('\\')[-1]
temp = labelName.split('.nii')
labelName1 = temp[0]+'_label.nii'+temp[1]
save_itk(labelImg1,labelOrigin,labelShape,labelName1)
else:
for curboxpath in boxfilelist:
boxnum = curboxpath.split('_')[-3]
if boxnum == labelnum:
box = np.load(curboxpath)
print(box)
labelImg, labelOrigin, labelShape = load_itk_image(curfilepath)
labelImg1 = labelImg[box[0, 0]:box[0, 1],
box[1, 0]:box[1, 1],
box[2, 0]:box[2, 1]]
# labelName = savepath +'/'+ curfilepath.split('/')[-1]
labelName = savepath + '\\' + curfilepath.split('\\')[-1]
temp = labelName.split('.nii')
labelName1 = temp[0] + '_label.nii' + temp[1]
save_itk(labelImg1, labelOrigin, labelShape, labelName1)
break
i = i+1
print(labelnum )
if __name__ == '__main__':
# inputpath ="/home/xy/pyProjects/code/code/airway_segmentation/testdata/imagesTr";
# inputpath = "../../airway_segmentation/airway_segmentation/testdata/imagesTr1";
# inputpath = "../../airway_segmentation/airway_segmentation/testdata/labelsTr1";
# inputpath = "../../airway_segmentation/airway_segmentation/testdata";
# inputpath1 = "G:/WorkSpace/DataSets/airway seg/ATM22/TrainBatch1/imagesTr118"#/////
#
# assert (os.path.exists(inputpath1))
savepath = "./result_lung682703";
# preprocess_CT(inputpath1,savepath);
boxpath = "./result_lung"
inputpath2 = "G:\\WorkSpace\\DataSets\\airway seg\\ATM22\\TrainBatch2\\labelsTr\\682703"
boxpath = 'G:\\WorkSpace\\DataSets\\airway seg\\ATM22\\segLungRegion\\ATM2\\682703'
preprocess_CTlabel(inputpath2,boxpath, savepath)