|
a |
|
b/BraTs18Challege/dataprocess/data3dprepare.py |
|
|
1 |
from __future__ import print_function, division |
|
|
2 |
import numpy as np |
|
|
3 |
import SimpleITK as sitk |
|
|
4 |
from dataprocess.utils import file_name_path |
|
|
5 |
|
|
|
6 |
flair_name = "_flair.nii.gz" |
|
|
7 |
t1_name = "_t1.nii.gz" |
|
|
8 |
t1ce_name = "_t1ce.nii.gz" |
|
|
9 |
t2_name = "_t2.nii.gz" |
|
|
10 |
mask_name = "_seg.nii.gz" |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
def subimage_generator(image, mask, patch_block_size, numberxy, numberz): |
|
|
14 |
""" |
|
|
15 |
generate the sub images and masks with patch_block_size |
|
|
16 |
:param image: |
|
|
17 |
:param patch_block_size: |
|
|
18 |
:param stride: |
|
|
19 |
:return: |
|
|
20 |
""" |
|
|
21 |
width = np.shape(image)[1] |
|
|
22 |
height = np.shape(image)[2] |
|
|
23 |
imagez = np.shape(image)[0] |
|
|
24 |
block_width = np.array(patch_block_size)[1] |
|
|
25 |
block_height = np.array(patch_block_size)[2] |
|
|
26 |
blockz = np.array(patch_block_size)[0] |
|
|
27 |
stridewidth = (width - block_width) // numberxy |
|
|
28 |
strideheight = (height - block_height) // numberxy |
|
|
29 |
stridez = (imagez - blockz) // numberz |
|
|
30 |
# step 1:if stridez is bigger 1,return numberxy * numberxy * numberz samples |
|
|
31 |
if stridez >= 1 and stridewidth >= 1 and strideheight >= 1: |
|
|
32 |
step_width = width - (stridewidth * numberxy + block_width) |
|
|
33 |
step_width = step_width // 2 |
|
|
34 |
step_height = height - (strideheight * numberxy + block_height) |
|
|
35 |
step_height = step_height // 2 |
|
|
36 |
step_z = imagez - (stridez * numberz + blockz) |
|
|
37 |
step_z = step_z // 2 |
|
|
38 |
hr_samples_list = [] |
|
|
39 |
hr_mask_samples_list = [] |
|
|
40 |
for z in range(step_z, numberz * (stridez + 1) + step_z, numberz): |
|
|
41 |
for x in range(step_width, numberxy * (stridewidth + 1) + step_width, numberxy): |
|
|
42 |
for y in range(step_height, numberxy * (strideheight + 1) + step_height, numberxy): |
|
|
43 |
if np.max(mask[z:z + blockz, x:x + block_width, y:y + block_height]) != 0: |
|
|
44 |
hr_samples_list.append(image[z:z + blockz, x:x + block_width, y:y + block_height]) |
|
|
45 |
hr_mask_samples_list.append(mask[z:z + blockz, x:x + block_width, y:y + block_height]) |
|
|
46 |
hr_samples = np.array(hr_samples_list).reshape((len(hr_samples_list), blockz, block_width, block_height)) |
|
|
47 |
hr_mask_samples = np.array(hr_mask_samples_list).reshape( |
|
|
48 |
(len(hr_mask_samples_list), blockz, block_width, block_height)) |
|
|
49 |
return hr_samples, hr_mask_samples |
|
|
50 |
# step 2:other sutitation,return one samples |
|
|
51 |
else: |
|
|
52 |
nb_sub_images = 1 * 1 * 1 |
|
|
53 |
hr_samples = np.zeros(shape=(nb_sub_images, blockz, block_width, block_height), dtype=np.float) |
|
|
54 |
hr_mask_samples = np.zeros(shape=(nb_sub_images, blockz, block_width, block_height), dtype=np.float) |
|
|
55 |
rangz = lambda imagez, blockz: imagez if imagez < blockz else blockz |
|
|
56 |
rangwidth = lambda width, block_width: width if width < block_width else block_width |
|
|
57 |
rangheight = lambda height, block_height: height if width < block_height else block_height |
|
|
58 |
hr_samples[0, 0:blockz, 0:block_width, 0:block_height] = image[0:rangz, 0:rangwidth, 0:rangheight] |
|
|
59 |
hr_mask_samples[0, 0:blockz, 0:block_width, 0:block_height] = mask[0:rangz, 0:rangwidth, 0:rangheight] |
|
|
60 |
return hr_samples, hr_mask_samples |
|
|
61 |
|
|
|
62 |
|
|
|
63 |
def make_patch(image, mask, patch_block_size, numberxy, numberz): |
|
|
64 |
""" |
|
|
65 |
make number patch |
|
|
66 |
:param image:[depth,512,512] |
|
|
67 |
:param patch_block: such as[64,128,128] |
|
|
68 |
:return:[samples,64,128,128] |
|
|
69 |
expand the dimension z range the subimage:[startpostion-blockz//2:endpostion+blockz//2,:,:] |
|
|
70 |
""" |
|
|
71 |
image_subsample, mask_subsample = subimage_generator(image=image, mask=mask, patch_block_size=patch_block_size, |
|
|
72 |
numberxy=numberxy, numberz=numberz) |
|
|
73 |
return image_subsample, mask_subsample |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
def gen_image_mask(flairimg, t1img, t1ceimg, t2img, segimg, index, shape, numberxy, numberz, trainImage, trainMask, |
|
|
77 |
part): |
|
|
78 |
""" |
|
|
79 |
:param flairimg: |
|
|
80 |
:param t1img: |
|
|
81 |
:param t1ceimg: |
|
|
82 |
:param t2img: |
|
|
83 |
:param segimg: |
|
|
84 |
:param index: |
|
|
85 |
:param shape: |
|
|
86 |
:param numberxy: |
|
|
87 |
:param numberz: |
|
|
88 |
:param trainImage: |
|
|
89 |
:param trainMask: |
|
|
90 |
:param part: |
|
|
91 |
:return: |
|
|
92 |
""" |
|
|
93 |
# step 2 get subimages (numberxy*numberxy*numberz,64, 128, 128) |
|
|
94 |
sub_flairimages,sub_maskimages = make_patch(flairimg,segimg, patch_block_size=shape, numberxy=numberxy, numberz=numberz) |
|
|
95 |
sub_t1images,_ = make_patch(t1img,segimg, patch_block_size=shape, numberxy=numberxy, numberz=numberz) |
|
|
96 |
sub_t1ceimages,_ = make_patch(t1ceimg,segimg, patch_block_size=shape, numberxy=numberxy, numberz=numberz) |
|
|
97 |
sub_t2images,_ = make_patch(t2img,segimg, patch_block_size=shape, numberxy=numberxy, numberz=numberz) |
|
|
98 |
# step 3 only save subimages (numberxy*numberxy*numberz,64, 128, 128) |
|
|
99 |
samples, imagez, height, width = np.shape(sub_flairimages)[0], np.shape(sub_flairimages)[1], \ |
|
|
100 |
np.shape(sub_flairimages)[2], np.shape(sub_flairimages)[3] |
|
|
101 |
for j in range(samples): |
|
|
102 |
sub_masks = sub_maskimages.astype(np.float) |
|
|
103 |
sub_masks = np.clip(sub_masks, 0, 255).astype('uint8') |
|
|
104 |
if np.max(sub_masks[j, :, :, :]) != 0: |
|
|
105 |
""" |
|
|
106 |
merage 4 model image into 4 channel (imagez,width,height,channel) |
|
|
107 |
""" |
|
|
108 |
fourmodelimagearray = np.zeros((imagez, height, width, 4), np.float) |
|
|
109 |
filepath1 = trainImage + "\\" + str(part) + "_" + str(index) + "_" + str(j) + ".npy" |
|
|
110 |
filepath = trainMask + "\\" + str(part) + "_" + str(index) + "_" + str(j) + ".npy" |
|
|
111 |
flairimage = sub_flairimages[j, :, :, :] |
|
|
112 |
flairimage = flairimage.astype(np.float) |
|
|
113 |
fourmodelimagearray[:, :, :, 0] = flairimage |
|
|
114 |
t1image = sub_t1images[j, :, :, :] |
|
|
115 |
t1image = t1image.astype(np.float) |
|
|
116 |
fourmodelimagearray[:, :, :, 1] = t1image |
|
|
117 |
t1ceimage = sub_t1ceimages[j, :, :, :] |
|
|
118 |
t1ceimage = t1ceimage.astype(np.float) |
|
|
119 |
fourmodelimagearray[:, :, :, 2] = t1ceimage |
|
|
120 |
t2image = sub_t2images[j, :, :, :] |
|
|
121 |
t2image = t2image.astype(np.float) |
|
|
122 |
fourmodelimagearray[:, :, :, 3] = t2image |
|
|
123 |
np.save(filepath1, fourmodelimagearray) |
|
|
124 |
np.save(filepath, sub_masks[j, :, :, :]) |
|
|
125 |
|
|
|
126 |
|
|
|
127 |
def normalize(slice, bottom=99, down=1): |
|
|
128 |
""" |
|
|
129 |
normalize image with mean and std for regionnonzero,and clip the value into range |
|
|
130 |
:param slice: |
|
|
131 |
:param bottom: |
|
|
132 |
:param down: |
|
|
133 |
:return: |
|
|
134 |
""" |
|
|
135 |
b = np.percentile(slice, bottom) |
|
|
136 |
t = np.percentile(slice, down) |
|
|
137 |
slice = np.clip(slice, t, b) |
|
|
138 |
|
|
|
139 |
image_nonzero = slice[np.nonzero(slice)] |
|
|
140 |
if np.std(slice) == 0 or np.std(image_nonzero) == 0: |
|
|
141 |
return slice |
|
|
142 |
else: |
|
|
143 |
tmp = (slice - np.mean(image_nonzero)) / np.std(image_nonzero) |
|
|
144 |
# since the range of intensities is between 0 and 5000 , |
|
|
145 |
# the min in the normalized slice corresponds to 0 intensity in unnormalized slice |
|
|
146 |
# the min is replaced with -9 just to keep track of 0 intensities |
|
|
147 |
# so that we can discard those intensities afterwards when sampling random patches |
|
|
148 |
tmp[tmp == tmp.min()] = -9 |
|
|
149 |
return tmp |
|
|
150 |
|
|
|
151 |
|
|
|
152 |
def prepare3dtraindata(pathhgg_list, bratshgg_path, trainImage, trainMask, shape=(16, 256, 256), numberxy=3, |
|
|
153 |
numberz=20, part=1): |
|
|
154 |
""" |
|
|
155 |
:param pathhgg_list: |
|
|
156 |
:param bratshgg_path: |
|
|
157 |
:param trainImage: |
|
|
158 |
:param trainMask: |
|
|
159 |
:param shape: |
|
|
160 |
:param numberxy: |
|
|
161 |
:param numberz: |
|
|
162 |
:return: |
|
|
163 |
""" |
|
|
164 |
""" |
|
|
165 |
load flair_image,t1_image,t1ce_image,t2_image,mask_image |
|
|
166 |
""" |
|
|
167 |
for subsetindex in range(len(pathhgg_list)): |
|
|
168 |
brats_subset_path = bratshgg_path + "/" + str(pathhgg_list[subsetindex]) + "/" |
|
|
169 |
flair_image = brats_subset_path + str(pathhgg_list[subsetindex]) + flair_name |
|
|
170 |
t1_image = brats_subset_path + str(pathhgg_list[subsetindex]) + t1_name |
|
|
171 |
t1ce_image = brats_subset_path + str(pathhgg_list[subsetindex]) + t1ce_name |
|
|
172 |
t2_image = brats_subset_path + str(pathhgg_list[subsetindex]) + t2_name |
|
|
173 |
mask_image = brats_subset_path + str(pathhgg_list[subsetindex]) + mask_name |
|
|
174 |
flair_src = sitk.ReadImage(flair_image, sitk.sitkInt16) |
|
|
175 |
t1_src = sitk.ReadImage(t1_image, sitk.sitkInt16) |
|
|
176 |
t1ce_src = sitk.ReadImage(t1ce_image, sitk.sitkInt16) |
|
|
177 |
t2_src = sitk.ReadImage(t2_image, sitk.sitkInt16) |
|
|
178 |
mask = sitk.ReadImage(mask_image, sitk.sitkUInt8) |
|
|
179 |
flair_array = sitk.GetArrayFromImage(flair_src) |
|
|
180 |
t1_array = sitk.GetArrayFromImage(t1_src) |
|
|
181 |
t1ce_array = sitk.GetArrayFromImage(t1ce_src) |
|
|
182 |
t2_array = sitk.GetArrayFromImage(t2_src) |
|
|
183 |
mask_array = sitk.GetArrayFromImage(mask) |
|
|
184 |
""" |
|
|
185 |
normalize every model image,mask is not normalization |
|
|
186 |
""" |
|
|
187 |
flair_array = normalize(flair_array) |
|
|
188 |
t1_array = normalize(t1_array) |
|
|
189 |
t1ce_array = normalize(t1ce_array) |
|
|
190 |
t2_array = normalize(t2_array) |
|
|
191 |
|
|
|
192 |
gen_image_mask(flair_array, t1_array, t1ce_array, t2_array, mask_array, subsetindex, shape=shape, |
|
|
193 |
numberxy=numberxy, numberz=numberz, trainImage=trainImage, trainMask=trainMask, part=part) |
|
|
194 |
|
|
|
195 |
|
|
|
196 |
def preparetraindata(): |
|
|
197 |
""" |
|
|
198 |
:return: |
|
|
199 |
""" |
|
|
200 |
bratshgg_path = "D:\Data\\brats18\HGG" |
|
|
201 |
bratslgg_path = "D:\Data\\brats18\LGG" |
|
|
202 |
trainImage = "D:\Data\\brats18\\train\Image" |
|
|
203 |
trainMask = "D:\Data\\brats18\\train\Mask" |
|
|
204 |
pathhgg_list = file_name_path(bratshgg_path) |
|
|
205 |
pathlgg_list = file_name_path(bratslgg_path) |
|
|
206 |
|
|
|
207 |
prepare3dtraindata(pathhgg_list, bratshgg_path, trainImage, trainMask, (64, 128, 128), 3, 15, 1) |
|
|
208 |
prepare3dtraindata(pathlgg_list, bratslgg_path, trainImage, trainMask, (64, 128, 128), 3, 15, 2) |
|
|
209 |
|
|
|
210 |
|
|
|
211 |
#preparetraindata() |