By Yaqian Chen, Hanxue Gu, Yuwen Chen, Jicheng Yang, Haoyu Dong, Joseph Y. Cao, Adrian Camarena, Christopher Mantyh, Roy Colglazier, and Maciej Mazurowski
Download the required pre-trained model weights from this Google Drive folder. Make sure to select the appropriate files for your configuration.
This is the official code for our paper:
Automated Muscle and Fat Segmentation in Computed Tomography for Comprehensive Body Composition Analysis

where we developed an open-source method for segmenting skeletal muscle, subcutaneous adipose tissue (SAT), and visceral adipose tissue (VAT) across the chest, abdomen, and pelvis in axial CT images.This method provides various body composition metrics, including muscle density, visceral-to-subcutaneous fat (VAT/SAT) ratio, muscle area/volume, and skeletal muscle index (SMI). It supports both 2D assessments at the L3 level and 3D assessments spanning from T12 to L4.
Below is the tutorial that explains how to use our automated muscle and fat segmentation tool.
- First, create a new conda environment:
conda create -n muscle_seg python=3.12
conda activate muscle_se
- Install required packages:
pip install -r requirements.txt
The script (predict_muscle_fat.py
) performs three main tasks:
- Muscle and fat segmentation using nnUNet
- Vertebrae detection using TotalSegmentator
- Body composition analysis (2D at L3 level and/or 3D between T12-L4)
python predict_muscle_fat.py --input <input_path> --output <output_path>
Argument | Description | Default |
---|---|---|
--input |
Path to input NIfTI file or folder | "demo" |
--output |
Path for saving segmentation results | "results" |
--save_probabilities |
Save probability maps | False |
--overwrite |
Overwrite existing predictions | False |
--checkpoint_type |
Use 'best' or 'final' checkpoint | "final" |
--body_composition_type |
Choose '2D', '3D', 'both', or 'None' | "both" |
--output_body_composition |
Path for body composition metrics | None |
- Process a single file:
python predict_muscle_fat.py --input patient001.nii.gz --output results/
- Process a directory of images:
python predict_muscle_fat.py --input dataset/images/ --output results/ --body_composition_type both
- Use best checkpoint and save probabilities:
python predict_muscle_fat.py --input dataset/images/ --output results/ --checkpoint_type best --save_probabilities True
output_path/
├── input_name_segmentation.nii.gz # Muscle/fat segmentation
└── input_name_vertebrae_segmentation/ # Vertebrae segmentation
├── vertebrae_T12.nii.gz
├── vertebrae_L3.nii.gz
└── vertebrae_L4.nii.gz
output_path/
├── body_composition_2d.csv # L3-level metrics
└── body_composition_3d.csv # T12-L4 metrics
muscle_area_mm2
: Muscle area at L3 levelmuscle_density_hu
: Average muscle density in Hounsfield unitssfat_area_mm2
: Subcutaneous fat areavfat_area_mm2
: Visceral fat areamfat_area_mm2
: Muscle fat areatotal_fat_area_mm2
: Total fat areabody_area_mm2
: Total body area
muscle_volume_mm3
: Muscle volumemuscle_density_hu
: Average muscle densitysfat_volume_mm3
: Subcutaneous fat volumevfat_volume_mm3
: Visceral fat volumemfat_volume_mm3
: Muscle fat volumetotal_fat_volume_mm3
: Total fat volumebody_volume_mm3
: Total body volumebody_height_mm
: Height between T12 and L4
- Use NIfTI format (.nii.gz)
- Ensure proper orientation (typically axial)
- Check image quality and contrast
- Check image direction (align with TotalSegmentator requirement)
Issue | Solution |
---|---|
Missing vertebrae | Check image coverage |
Vertebrae segmentation errors | Verify image orientation |
This tool combines state-of-the-art deep learning models for accurate muscle and fat analysis in medical images. For more information, please refer to the documentation of nnUNet and TotalSegmentator.
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.