Skip to content

Robust Segmentation and Labeling of Vertebrae, Intervertebral Discs, Spinal Cord, and Spinal Canal in MRI Images Using nnU-Net and Iterative Algorithm.

License

Notifications You must be signed in to change notification settings

neuropoly/totalspineseg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

totalsegmentator-mri

Code for the TotalSegmentator MRI project.

Steps to install

  1. Clone this repository

    git clone https://github.com/neuropoly/totalsegmentator-mri.git
    
  2. Clone SynthSeg repository

    git clone https://github.com/BBillot/SynthSeg.git
    
  3. Download this google folder (the TotalSegmentator example image was downloaded from here).

  4. Create Virtual Environment (Please make sure you're using python 3.8 !!!)

    python -m venv venv
    
  5. 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
    
  6. Activate Virtual Environment

    source venv/bin/activate
    
  7. Install requirements:

    pip install -r SynthSeg/requirements_python3.8.txt
    

To run scripsts

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).

Option 1 - Run script for all TotalSegmentator labels

  1. 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
    
  2. 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
    
  3. 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
    
  4. 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
    

Option 2 - Run script with TotalSegmentator labels reduced to 15 labels

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.

  1. 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
    
  2. 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
    
  3. 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
    
  4. 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
    

Data organization

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