1313img = nib .load (input_file )
1414data = img .get_fdata ().astype (np .int16 )
1515
16- # Define the ROI list (in the order used for segmentation)
17- roi_list = [
18- "spleen" , "kidney_right" , "kidney_left" , "gallbladder" , "liver" , "stomach" ,
19- "pancreas" , "esophagus" , "small_bowel" , "duodenum" , "urinary_bladder" , "prostate" ,
20- "spinal_cord"
21- ]
16+ # Full TotalSegmentator mapping
17+ total_mapping = {
18+ 1 : "spleen" ,
19+ 2 : "kidney_right" ,
20+ 3 : "kidney_left" ,
21+ 4 : "gallbladder" ,
22+ 5 : "liver" ,
23+ 6 : "stomach" ,
24+ 7 : "pancreas" ,
25+ 8 : "adrenal_gland_right" ,
26+ 9 : "adrenal_gland_left" ,
27+ 10 : "lung_upper_lobe_left" ,
28+ 11 : "lung_lower_lobe_left" ,
29+ 12 : "lung_upper_lobe_right" ,
30+ 13 : "lung_middle_lobe_right" ,
31+ 14 : "lung_lower_lobe_right" ,
32+ 15 : "esophagus" ,
33+ 16 : "trachea" ,
34+ 17 : "thyroid_gland" ,
35+ 18 : "small_bowel" ,
36+ 19 : "duodenum" ,
37+ 20 : "colon" ,
38+ 21 : "urinary_bladder" ,
39+ 22 : "prostate" ,
40+ 23 : "kidney_cyst_left" ,
41+ 24 : "kidney_cyst_right" ,
42+ 25 : "sacrum" ,
43+ 26 : "vertebrae_S1" ,
44+ 27 : "vertebrae_L5" ,
45+ 28 : "vertebrae_L4" ,
46+ 29 : "vertebrae_L3" ,
47+ 30 : "vertebrae_L2" ,
48+ 31 : "vertebrae_L1" ,
49+ 32 : "vertebrae_T12" ,
50+ 33 : "vertebrae_T11" ,
51+ 34 : "vertebrae_T10" ,
52+ 35 : "vertebrae_T9" ,
53+ 36 : "vertebrae_T8" ,
54+ 37 : "vertebrae_T7" ,
55+ 38 : "vertebrae_T6" ,
56+ 39 : "vertebrae_T5" ,
57+ 40 : "vertebrae_T4" ,
58+ 41 : "vertebrae_T3" ,
59+ 42 : "vertebrae_T2" ,
60+ 43 : "vertebrae_T1" ,
61+ 44 : "vertebrae_C7" ,
62+ 45 : "vertebrae_C6" ,
63+ 46 : "vertebrae_C5" ,
64+ 47 : "vertebrae_C4" ,
65+ 48 : "vertebrae_C3" ,
66+ 49 : "vertebrae_C2" ,
67+ 50 : "vertebrae_C1" ,
68+ 51 : "heart" ,
69+ 52 : "aorta" ,
70+ 53 : "pulmonary_vein" ,
71+ 54 : "brachiocephalic_trunk" ,
72+ 55 : "subclavian_artery_right" ,
73+ 56 : "subclavian_artery_left" ,
74+ 57 : "common_carotid_artery_right" ,
75+ 58 : "common_carotid_artery_left" ,
76+ 59 : "brachiocephalic_vein_left" ,
77+ 60 : "brachiocephalic_vein_right" ,
78+ 61 : "atrial_appendage_left" ,
79+ 62 : "superior_vena_cava" ,
80+ 63 : "inferior_vena_cava" ,
81+ 64 : "portal_vein_and_splenic_vein" ,
82+ 65 : "iliac_artery_left" ,
83+ 66 : "iliac_artery_right" ,
84+ 67 : "iliac_vena_left" ,
85+ 68 : "iliac_vena_right" ,
86+ 69 : "humerus_left" ,
87+ 70 : "humerus_right" ,
88+ 71 : "scapula_left" ,
89+ 72 : "scapula_right" ,
90+ 73 : "clavicula_left" ,
91+ 74 : "clavicula_right" ,
92+ 75 : "femur_left" ,
93+ 76 : "femur_right" ,
94+ 77 : "hip_left" ,
95+ 78 : "hip_right" ,
96+ 79 : "spinal_cord" ,
97+ 80 : "gluteus_maximus_left" ,
98+ 81 : "gluteus_maximus_right" ,
99+ 82 : "gluteus_medius_left" ,
100+ 83 : "gluteus_medius_right" ,
101+ 84 : "gluteus_minimus_left" ,
102+ 85 : "gluteus_minimus_right" ,
103+ 86 : "autochthon_left" ,
104+ 87 : "autochthon_right" ,
105+ 88 : "iliopsoas_left" ,
106+ 89 : "iliopsoas_right" ,
107+ 90 : "brain" ,
108+ 91 : "skull" ,
109+ 92 : "rib_left_1" ,
110+ 93 : "rib_left_2" ,
111+ 94 : "rib_left_3" ,
112+ 95 : "rib_left_4" ,
113+ 96 : "rib_left_5" ,
114+ 97 : "rib_left_6" ,
115+ 98 : "rib_left_7" ,
116+ 99 : "rib_left_8" ,
117+ 100 : "rib_left_9" ,
118+ 101 : "rib_left_10" ,
119+ 102 : "rib_left_11" ,
120+ 103 : "rib_left_12" ,
121+ 104 : "rib_right_1" ,
122+ 105 : "rib_right_2" ,
123+ 106 : "rib_right_3" ,
124+ 107 : "rib_right_4" ,
125+ 108 : "rib_right_5" ,
126+ 109 : "rib_right_6" ,
127+ 110 : "rib_right_7" ,
128+ 111 : "rib_right_8" ,
129+ 112 : "rib_right_9" ,
130+ 113 : "rib_right_10" ,
131+ 114 : "rib_right_11" ,
132+ 115 : "rib_right_12" ,
133+ 116 : "sternum" ,
134+ 117 : "costal_cartilages"
135+ }
22136
23- # Define desired mapping: keys are ROI names, values are target label integers
137+ # Define a desired remapping.
24138desired_mapping = {
25139 "spleen" : 1 ,
26140 "kidney_right" : 2 ,
30144 "stomach" : 6 ,
31145 "pancreas" : 7 ,
32146 "esophagus" : 8 ,
33- "small_bowel" : 9 , # corresponds to "Small-Intestine "
147+ "small_bowel" : 9 , # corresponds to "small_bowel "
34148 "duodenum" : 10 ,
35- "urinary_bladder" : 11 , # corresponds to "Bladder"
149+ "urinary_bladder" : 11 ,
36150 "prostate" : 12 ,
37- "spinal_cord" : 13 # corresponds to "Spinal-Canal"
151+ #"spinal_cord": 13
152+ # Add additional mappings if needed.
38153}
39154
40- # Get the unique labels (ignoring background 0)
41- unique_vals = np .unique (data )
42- unique_vals = unique_vals [unique_vals != 0 ]
43- unique_vals = np .sort (unique_vals )
44-
45- if len (unique_vals ) != len (roi_list ):
46- print ("Warning: Expected {} ROI labels, but found {} in the segmentation." .format (len (roi_list ), len (unique_vals )))
47-
48- # Build a remapping dictionary based on the assumed order.
49- remap_dict = {}
50- for i , orig_label in enumerate (unique_vals ):
51- if i < len (roi_list ):
52- roi = roi_list [i ]
53- remap_dict [orig_label ] = desired_mapping [roi ]
54- else :
55- remap_dict [orig_label ] = 0 # fallback to background if extra
155+ # Create a new data array for the remapped segmentation.
156+ new_data = np .copy (data )
56157
57- # Create a new data array with remapped labels
58- new_data = np .zeros_like (data , dtype = np .int16 )
59- for orig , new in remap_dict .items ():
60- new_data [data == orig ] = new
158+ # Iterate through each label in the TotalSegmentator mapping.
159+ for orig_label , structure in total_mapping .items ():
160+ # Determine the new label
161+ new_label = desired_mapping .get (structure , 0 )
162+ new_data [data == orig_label ] = new_label
61163
62- # Save the new segmentation with the same header and affine
164+ # Save the new segmentation with the same header and affine.
63165new_img = nib .Nifti1Image (new_data , img .affine , img .header )
64- nib .save (new_img , output_file )
166+ nib .save (new_img , output_file )
0 commit comments