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
| from glob import glob import os import SimpleITK as sitk from pathlib import Path import numpy as np import imageio path = glob(r"D:\compation\kaggle\3D_preprocess\a\*") save = Path(r"D:\compation\kaggle\3D_preprocess\a") for i in range(len(path)): file = path[i] file_name = file.split("\\")[-1].split("_seg")[0] case = file_name.split("_")[0] print("case:{}, file_name:{}".format(case, file_name)) seg = sitk.ReadImage(file) seg = sitk.GetArrayFromImage(seg) for j in range(seg.shape[0]): save_path = save / case / file_name /"scans" name = "slice_"+str(j)+str(seg.shape[1]) +"_"+str(seg.shape[2])+"_"+str(1.5)+"_"+str(1.5) output = seg[j, ...] Snapshot_img = np.zeros(shape=(seg.shape[1],seg.shape[2],3), dtype=np.uint8) Snapshot_img[:, :, 0][np.where(output == 1)] = 255 Snapshot_img[:, :, 1][np.where(output == 2)] = 255 Snapshot_img[:, :, 2][np.where(output == 3)] = 255
os.makedirs(save_path, exist_ok=True) imageio.imwrite(os.path.join(save_path, name + '.png'), Snapshot_img[:, :, :])
|