Code for the TotalSegmentator MRI project.
-
Clone this repository
git clone https://github.com/neuropoly/totalsegmentator-mri.git
-
Clone SynthSeg repository
git clone https://github.com/BBillot/SynthSeg.git
-
Download this google folder (the TotalSegmentator example image was downloaded from here).
-
Create Virtual Environment (Please make sure you're using python 3.8 !!!)
python -m venv venv
-
Add SynthSeg to Virtual Environment (If not using bash change '$(pwd)' to the working directory):
echo "$(pwd)/SynthSeg" > venv/lib/python3.8/site-packages/SynthSeg.pth
-
Activate Virtual Environment
source venv/bin/activate
-
Install requirements:
pip install -r SynthSeg/requirements_python3.8.txt
resources/labels.json
- Contain mapping of each mask to unique number.
resources/classes.json
- Contain mapping of each mask to class of masks with similar statistics (total 15 classes).
-
Combine all MPRAGE 'blob' masks for each subject into a single segmentation file:
python totalsegmentator-mri/scripts/combine_masks.py -d TotalSegmentatorMRI_SynthSeg/data/derivatives/manual_masks -o output/ALL_LAB/MP-RAGE_Masks_Combined -m totalsegmentator-mri/resources/labels.json
-
Calculate signal statistics (mean + std) for each masks (group masks into classes of similar statistics):
python totalsegmentator-mri/scripts/build_intensity_stats.py -d TotalSegmentatorMRI_SynthSeg/data -s output/ALL_LAB/MP-RAGE_Masks_Combined -o output/ALL_LAB/MP-RAGE_priors -m totalsegmentator-mri/resources/labels.json -c totalsegmentator-mri/resources/classes.json
-
Combine all TotalSegmentator masks for each subject into a single segmentation file:
python totalsegmentator-mri/scripts/combine_masks.py -d TotalSegmentatorMRI_SynthSeg/Totalsegmentator_dataset -o output/ALL_LAB/TotalSegmentator_Masks_Combined -m totalsegmentator-mri/resources/labels.json --subject-prefix s --subject-subdir segmentations --seg-suffix _ct_seg --output-bids 0
-
Create a synthetic image using TotalSegmentator segmentation and the calculated MPRAGE signal statistics:
python totalsegmentator-mri/scripts/generate_image.py -s output/ALL_LAB/TotalSegmentator_Masks_Combined -p output/ALL_LAB/MP-RAGE_priors -o output/ALL_LAB/MP-RAGE_Synthetic/test1 -n 2
To reduce number of labels and group all vertebrae, we use resources/classes.json
as the main masks mapping when combining masks with combine_masks. This way all masks of the same classes will be mapped to the same label.
-
Combine all MPRAGE 'blob' masks for each subject into a single segmentation file:
python totalsegmentator-mri/scripts/combine_masks.py -d TotalSegmentatorMRI_SynthSeg/data/derivatives/manual_masks -o output/15_LAB/MP-RAGE_Masks_Combined -m totalsegmentator-mri/resources/classes.json
-
Calculate signal statistics (mean + std) for each masks:
python totalsegmentator-mri/scripts/build_intensity_stats.py -d TotalSegmentatorMRI_SynthSeg/data -s output/15_LAB/MP-RAGE_Masks_Combined -o output/15_LAB/MP-RAGE_priors -m totalsegmentator-mri/resources/classes.json
-
Combine all TotalSegmentator masks for each subject into a single segmentation file:
python totalsegmentator-mri/scripts/combine_masks.py -d TotalSegmentatorMRI_SynthSeg/Totalsegmentator_dataset -o output/15_LAB/TotalSegmentator_Masks_Combined -m totalsegmentator-mri/resources/classes.json --subject-prefix s --subject-subdir segmentations --seg-suffix _ct_seg --output-bids 0
-
Create a synthetic image using TotalSegmentator segmentation and the calculated MPRAGE signal statistics:
python totalsegmentator-mri/scripts/generate_image.py -s output/15_LAB/TotalSegmentator_Masks_Combined -p output/15_LAB/MP-RAGE_priors -o output/15_LAB/MP-RAGE_Synthetic/test1 -n 2
As a starting point, a few MPRAGE data are under our private google folder.
We will follow the BIDS structure:
├── derivatives
│ └── manual_masks
│ └── sub-errsm37
│ └── anat
└── sub-errsm37
└── anat
├── sub-errsm37_T1w.json
└── sub-errsm37_T1w.nii.gz