1+ #! /bin/bash
2+ # segment_folder.sh
3+ #
4+ # This script loops over all nii.gz files in an input folder,
5+ # runs TotalSegmentator with a restricted ROI subset, and then
6+ # remaps the resulting multi-label segmentation to a custom label mapping.
7+ #
8+ # The desired final mapping is:
9+ # background: 0
10+ # Spleen: 1
11+ # Kidney-Right: 2
12+ # Kidney-Left: 3
13+ # Gall-Bladder: 4
14+ # Liver: 5
15+ # Stomach: 6
16+ # Pancreas: 7
17+ # Esophagus: 8
18+ # Small-Intestine: 9
19+ # Duodenum: 10
20+ # Bladder: 11
21+ # Prostate: 12
22+ # Spinal-Canal: 13
23+ #
24+ # Note:
25+ # The names used with --roi_subset must match the TotalSegmentator classes.
26+ # Here we use:
27+ # spleen kidney_right kidney_left gallbladder liver stomach pancreas esophagus small_bowel duodenum urinary_bladder prostate spinal_cord
28+ # which correspond to the desired labels (with small naming differences).
29+ #
30+ # Usage:
31+ # ./segment_folder.sh /path/to/input_folder /path/to/output_folder
32+
33+ # Check for two arguments
34+ if [ " $# " -ne 2 ]; then
35+ echo " Usage: $0 <input_folder> <output_folder>"
36+ exit 1
37+ fi
38+
39+ INPUT_FOLDER=" $1 "
40+ OUTPUT_FOLDER=" $2 "
41+
42+ # Create output folder if it doesn't exist
43+ mkdir -p " $OUTPUT_FOLDER "
44+
45+ # Create a temporary directory for TotalSegmentator outputs
46+ TMP_DIR=$( mktemp -d)
47+
48+ # Function to process a single file
49+ process_file () {
50+ local file=" $1 "
51+ local filename=$( basename " $file " )
52+ local base=" ${filename% .nii.gz} "
53+ echo " Processing $filename ..."
54+
55+ # Create a temporary folder for this file's segmentation
56+ local FILE_TMP_DIR=" $TMP_DIR /$base "
57+ mkdir -p " $FILE_TMP_DIR "
58+
59+ # Run TotalSegmentator using ROI subset and multi-label output
60+ if ! TotalSegmentator -i " $file " -o " $FILE_TMP_DIR " \
61+ --roi_subset spleen kidney_right kidney_left gallbladder liver stomach pancreas esophagus small_bowel duodenum urinary_bladder prostate spinal_cord \
62+ --ml; then
63+ echo " TotalSegmentator failed for $file ; skipping."
64+ return
65+ fi
66+
67+ # Log the contents of the temporary directory
68+ echo " Contents of $FILE_TMP_DIR :"
69+ ls -l " $FILE_TMP_DIR "
70+
71+ # Find the segmentation file produced by TotalSegmentator.
72+ local SEG_FILE=$( find " $TMP_DIR " -maxdepth 1 -name " $base .nii" | head -n 1)
73+ if [ -z " $SEG_FILE " ]; then
74+ echo " No segmentation file found for $file ; skipping."
75+ return
76+ fi
77+
78+ # Run the Python script to remap labels
79+ if ! python3 scripts/remap_labels.py " $SEG_FILE " " $OUTPUT_FOLDER /${base} .nii.gz" ; then
80+ echo " Label remapping failed for $file ; skipping."
81+ return
82+ fi
83+
84+ echo " Saved remapped segmentation as ${base} .nii.gz in $OUTPUT_FOLDER "
85+ }
86+
87+ export -f process_file
88+ export TMP_DIR
89+ export OUTPUT_FOLDER
90+
91+ # Loop over all nii.gz files in the input folder and process them in parallel
92+ find " $INPUT_FOLDER " -name " *.nii.gz" -print0 | xargs -0 -n 1 -P 4 bash -c ' process_file "$@"' _
93+
94+ # Clean up temporary files
95+ rm -rf " $TMP_DIR "
96+ echo " Processing complete."
0 commit comments