Reshaping segmentation to fit original volume

I have a CT volume with a corresponding segmentation. Both are NIFTI files, but of different sizes: the main image is 512 x 512 x 37 while the segmentation is 512 x 512 x 23. It seems like the tool used to draw the segmentation only saves the slices where something is drawn. Opening the images with for example MRIcroGL works fine, which must mean that the NIFTI header contains all information needed to overlay the images correctly.
How can I reshape the segmentation so that it becomes the same size as the original image? I have the same problem on many volumes, so a command line tool / python method would be ideal. Any pointers much appreciated!

If you have FreeSurfer, mri_vol2vol will be straightforward:

mri_vol2vol --mov $SEG \  # Your segmentation
    --targ $CT \          # The image you want the output to align with
    --o $NEWSEG \         # Name of output image
    --regheader \         # Images are already in alignment, use header
    --nearest             # Use nearest-neighbor interpolation

You can find equivalents in most other software suites. The important bits are figuring out how to use the header information and using nearest-neighbor interpolation (appropriate for integer labels).

MANAGED BY INCF