Import nifti segmentation files

I trained a CT segmentation model that saves its result (n x 512 x 512 binary label map) into a .nii.gz file. I tried to import the segmentation file into slicer, but the segmentation mask always appeared outside the CT image. I tried to change orientation, change dimension order and save the result in .nrrd format, none worked, mask always appeared outside CT image. ITK-snap however had no difficulty load the result and the mask was in correct position. What should i do to make its result accessible to slicer? Does it have anything to do with orientation? Thanks in advance.

You’ll find a lot of information about nifti orientation issues in the forum.

This thread might help:

Thank you for your reply.
In fact my problem is not with NIFTI. I just want Slicer to recognize a numpy binary labelmap. I read the thread you provided. I saved that numpy array in nrrd form using pynrrd, and tried to give it different headers. I almost tried every combination of orientation, direction and RAS spaces, none of them worked. Slicer can read the mask file, but the segmentation mask always appeared outside the CT image. In fact I had no problem loading segmentation generated by Slicer using SimpleITK, but Slicer just cannot correctly recognize any segmentation that is not generated by itself. I really don’t know what should I do to make it work…

If you are really stuck making a nifti file work, but you know the numpy data is in the same pixel space as the volume you can do something like this:

    volumeNode = slicer.util.loadVolume(volumePath, properties={"singleFile": True})
    prob = numpy.load(probPath)['probabilities'][1] * 1000
    ijkToRAS = vtk.vtkMatrix4x4()
    probNode = slicer.util.addVolumeFromArray(prob, name ="prob", ijkToRAS=ijkToRAS)