|
a |
|
b/dataloaders/la_process.py |
|
|
1 |
import os |
|
|
2 |
import numpy as np |
|
|
3 |
from tqdm import tqdm |
|
|
4 |
import h5py |
|
|
5 |
import nrrd |
|
|
6 |
|
|
|
7 |
|
|
|
8 |
output_size =[112, 112, 80] |
|
|
9 |
data_path = 'E:/data/LASet/origin' |
|
|
10 |
out_path = 'E:/data/LASet/data' |
|
|
11 |
def covert_h5(): |
|
|
12 |
listt = os.listdir(data_path) |
|
|
13 |
for case in tqdm(listt): |
|
|
14 |
image, img_header = nrrd.read(os.path.join(data_path,case,'lgemri.nrrd')) |
|
|
15 |
label, gt_header = nrrd.read(os.path.join(data_path,case, 'laendo.nrrd')) |
|
|
16 |
label = (label == 255).astype(np.uint8) |
|
|
17 |
w, h, d = label.shape |
|
|
18 |
# 返回label中所有非零区域(分割对象)的索引 |
|
|
19 |
tempL = np.nonzero(label) |
|
|
20 |
# 分别获取非零区域在x,y,z三轴的最小值和最大值,确保裁剪图像包含分割对象 |
|
|
21 |
minx, maxx = np.min(tempL[0]), np.max(tempL[0]) |
|
|
22 |
miny, maxy = np.min(tempL[1]), np.max(tempL[1]) |
|
|
23 |
minz, maxz = np.min(tempL[2]), np.max(tempL[2]) |
|
|
24 |
# 计算目标尺寸比分割对象多余的尺寸 |
|
|
25 |
px = max(output_size[0] - (maxx - minx), 0) // 2 |
|
|
26 |
py = max(output_size[1] - (maxy - miny), 0) // 2 |
|
|
27 |
pz = max(output_size[2] - (maxz - minz), 0) // 2 |
|
|
28 |
# 在三个方向上随机扩增 |
|
|
29 |
minx = max(minx - np.random.randint(10, 20) - px, 0) |
|
|
30 |
maxx = min(maxx + np.random.randint(10, 20) + px, w) |
|
|
31 |
miny = max(miny - np.random.randint(10, 20) - py, 0) |
|
|
32 |
maxy = min(maxy + np.random.randint(10, 20) + py, h) |
|
|
33 |
minz = max(minz - np.random.randint(5, 10) - pz, 0) |
|
|
34 |
maxz = min(maxz + np.random.randint(5, 10) + pz, d) |
|
|
35 |
# 图像归一化,转为32位浮点数(numpy默认是64位) |
|
|
36 |
image = (image - np.mean(image)) / np.std(image) |
|
|
37 |
image = image.astype(np.float32) |
|
|
38 |
# 裁剪 |
|
|
39 |
image = image[minx:maxx, miny:maxy, minz:maxz] |
|
|
40 |
label = label[minx:maxx, miny:maxy, minz:maxz] |
|
|
41 |
print(label.shape) |
|
|
42 |
|
|
|
43 |
case_dir = os.path.join(out_path,case) |
|
|
44 |
os.mkdir(case_dir) |
|
|
45 |
f = h5py.File(os.path.join(case_dir, 'mri_norm2.h5'), 'w') |
|
|
46 |
f.create_dataset('image', data=image, compression="gzip") |
|
|
47 |
f.create_dataset('label', data=label, compression="gzip") |
|
|
48 |
f.close() |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
if __name__ == '__main__': |
|
|
52 |
covert_h5() |