Stitching (TPTBox.stitching)¶
Merges multiple NIfTI images that are already aligned in global space into a single volume. Useful for whole-body or long-spine multi-station acquisitions. You can verify alignment by opening the images in ITKSnap with "open additional image."
API¶
| Function | Description |
|---|---|
stitching(nii_list, out, ...) |
Stitch a list of NII objects; returns (result_nii, ramp_nii) |
stitching_raw(paths, out, ...) |
Stitch from file paths directly |
GNC_stitch_T2w(nii_list, ...) |
GNC-based stitching optimised for T2w spine MRI |

Standalone¶
This script can be run directly from the console. Copy 'stitching.py' and install the necessary package.
stitching.py
[-h] print the help message
[-i IMAGES [IMAGES ...]] a list of input image paths
[-o OUTPUT] The output image path
[-v] verbose - if set, there will be more printouts.
[-min_value MIN_VALUE] New pixels not present will get this value. Recommended 0 for MRI and for CT -1024 or the known min-value.
[-seg] This flag is required if you merge segmentation Niftis.
Switches:
[-no_bias] If set: Do not use n4_bias_field_correction. It speeds up the process, but n4_bias_field_correction helps in roughly aligning the histogram.
[-bias_crop] crop empty spaces by the bias field mask.
[-crop] crop empty space away
[-sr] Store the ramp and stitching of the images in a 4d nii.gz
Optional:
[-hists] Use histogram matching to put the images in the roughly same histogram. The previous image is used when hist_n is not set.
[-hist_n HISTOGRAM_NAME] path to an image that should be used for histogram matching
[-ramp_e RAMP_EDGE_MIN_VALUE] The ramp is only considering values above this minimum value
[-ms MIN_SPACING] Set the minimum Spacing (in mm)
[-dtype DTYPE] Force a dtype
Example:
Given the image a.nii.gz,b.nii.gz,c.nii.gz and the segmentations a_msk.nii.gz,b_msk.nii.gz,c_msk.nii.gz. The images can be merged with:
stitching.py -i a.nii.gz b.nii.gz c.nii.gz -o out.nii.gz
stitching.py -i a_msk.nii.gz b_msk.nii.gz c_msk.nii.gz -o out_msk.nii.gz -seg
Install as a package¶
Install on Python 3.10 or higher
from TPTBox import NII
from TPTBox.stitching import stitching
out_nii,_ = stitching([NII.load("a.nii.gz",seg=False), NII.load("b.nii.gz",seg=False), NII.load("c.nii.gz",seg=False)], out="out.nii.gz")
or
from TPTBox.stitching import stitching_raw
stitching_raw(["a.nii.gz", "b.nii.gz", "c.nii.gz"], "out.nii.gz", is_segmentation=False)
Cite¶
Graf, R., Platzek, PS., Riedel, E.O. et al. Generating synthetic high-resolution spinal STIR and T1w images from T2w FSE and low-resolution axial Dixon. Eur Radiol (2024). https://doi.org/10.1007/s00330-024-11047-1
@article{graf2024generating,
title={Generating synthetic high-resolution spinal STIR and T1w images from T2w FSE and low-resolution axial Dixon},
author={Graf, Robert and Platzek, Paul-S{\"o}ren and Riedel, Evamaria Olga and Kim, Su Hwan and Lenhart, Nicolas and Ramsch{\"u}tz, Constanze and Paprottka, Karolin Johanna and Kertels, Olivia Ruriko and M{\"o}ller, Hendrik Kristian and Atad, Matan and others},
journal={European Radiology},
pages={1--11},
year={2024},
publisher={Springer}
}