-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_label_2_overlap_label.py
63 lines (50 loc) · 2.98 KB
/
convert_label_2_overlap_label.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
import os
import SimpleITK as sitk
import numpy as np
from tqdm import tqdm
from label_systems import FLARE_label, AMOS_label, WORD_label, TotalSeg_label, BTCV_label, overlap_label
def generate_label_map(label_dict, target_label_dict):
target_value = {v: k for k, v in target_label_dict.items()}
return {k: target_value[v] for k, v in label_dict.items() if v in target_value}
def convert_folder(original_seg_folder, prediction_folders_raw_path, prediction_folders_converted_path, label_map):
if not os.path.exists(os.path.join(prediction_folders_converted_path, original_seg_folder)):
os.makedirs(os.path.join(prediction_folders_converted_path, original_seg_folder))
for seg_file in tqdm(os.listdir(os.path.join(prediction_folders_raw_path, original_seg_folder))):
if '.nii.gz' not in seg_file:
continue
seg_sitk = sitk.ReadImage(os.path.join(prediction_folders_raw_path, original_seg_folder, seg_file))
seg = sitk.GetArrayFromImage(seg_sitk)
converted = np.zeros_like(seg, dtype=seg.dtype)
for label in label_map:
converted[seg == int(label)] = int(label_map[label])
converted_sitk = sitk.GetImageFromArray(converted.astype(np.uint8))
converted_sitk.SetSpacing(seg_sitk.GetSpacing())
converted_sitk.SetOrigin(seg_sitk.GetOrigin())
converted_sitk.SetDirection(seg_sitk.GetDirection())
sitk.WriteImage(converted_sitk, os.path.join(prediction_folders_converted_path, original_seg_folder, seg_file))
def main():
prediction_folders_raw_path = 'predict_results'
prediction_folders_converted_path = 'predict_folders_converted_to_overlap'
# prediction_folders_raw_path = 'gtsVal_raw'
# prediction_folders_converted_path = 'gtsVal_overlap_label'
print(f"Converting all segmentation results from subfolders in {prediction_folders_raw_path} to subfolders in {prediction_folders_converted_path}.")
label_maps = {
'FLARE': generate_label_map(FLARE_label, overlap_label),
'AMOS': generate_label_map(AMOS_label, overlap_label),
'WORD': generate_label_map(WORD_label, overlap_label),
'TotalSeg': generate_label_map(TotalSeg_label, overlap_label),
'BTCV': generate_label_map(BTCV_label, overlap_label)
}
prediction_folders = os.listdir(prediction_folders_raw_path)
for original_seg_folder in prediction_folders:
for prefix, label_map in label_maps.items():
if original_seg_folder.startswith(prefix):
print(f"Converting label system of {prefix} to overlap label system")
for k, v in label_map.items():
print(f"{FLARE_label[v]}: {k} -> {v}")
convert_folder(original_seg_folder, prediction_folders_raw_path, prediction_folders_converted_path, label_map)
break
else:
print(f"The label system of {original_seg_folder} is not known. Skipping conversion.")
if __name__ == '__main__':
main()