wasserth / TotalSegmentator

Tool for robust segmentation of >100 important anatomical structures in CT and MR images
Apache License 2.0
1.47k stars 244 forks source link

Merging of segmented output classes #235

Closed jmanden closed 2 months ago

jmanden commented 11 months ago

Hi Wasserth,

Thank you for this open access segmentation model. The model currently outputs one nii file for each segmented class. I was wondering if there is a shortcut to combine the segmentation masks of each class into one nii file containing all masks?

Kind regards

wasserth commented 11 months ago

To combine the segmentation masks of each class into one nii file containing all masks, TotalSegmentator provides a convenient --ml flag that can be used during the segmentation process. By adding this flag to your command, TotalSegmentator will output a single multilabel nifti file that contains all classes with different label indices.

Here's how you can modify your command to output a single multilabel nifti file:

TotalSegmentator -i <input_file> -o <output_multilabel_file> --ml [other options]

For example, if your original command is:

TotalSegmentator -i ct_scan.nii.gz -o segmentations

You can change it to:

TotalSegmentator -i ct_scan.nii.gz -o segmentations_multilabel.nii.gz --ml

Make sure to replace ct_scan.nii.gz with the path to your input CT Nifti file, and segmentations_multilabel.nii.gz with the desired output path for the multilabel segmentation file.

If you have already generated individual class segmentations and wish to combine them after the fact, you would need to use a separate script or software tool that is capable of merging multiple nii files into one multilabel nifti file. One option could be to use a Python script with the nibabel library to manually load each segmentation nii file, assign unique labels, and save them into a single nifti file. However, using the --ml flag during the segmentation process is the simplest approach provided by TotalSegmentator.

jmanden commented 10 months ago

Thank you Mr Wasserthal! It works perfectly!

4lparslan commented 9 months ago

When i use --ml flag it does not give me an output file and any error?

grying93 commented 7 months ago

hi, 4lparslan, u should enter the complete command line, especially the output file name after "-o" , such as "-o segmentations_multilabel.nii.gz --ml".