How to show the segmentation in segment editor which is loaded from a nii.gz file generated by other program?

hello all,
I generated a segmented mark file from other program, and saved it to a nii.gz file which can be viewed in ITK snap. And the value is only 0 and 1 in the nii.gz file. Currently I would like use slicer to edit the segmentation result again. So I load the DICOM volume data first, and load the nii.gz file in “Data” module, choosed the label map as shown in picture 1 below.


But there is not segmentation in the segment editor, shown in picture 2 below.

And then switch to the Segmentation module, choose the right input node in the “exprots/imports models and labelmaps” and click import(shown in pic3), and then there is the segmentation in the segment editor.

but only the original dicom is shown in the editor, there is not any mark , shown in pic 4 below.

So how to show the mark in the nii.gz file which load just now? thanks in advance.

1 Like

Does anyone can help? Thank you so much.

I would load the labelmap (nii.gz) file and DICOM individually into Slicer scene, and make sure that they indeed occupy the same space. It is possible that something went wrong during the export process (or it was lossy). If the labelmap shows up in correct place, then you can go back and load the labelmap, but instead of volume type, choose segmentation.

1 Like

Thank your so much for detailed answer. I choose segmentation when load the nii.gz file now. The screenshot is shown in the picture below.


but if only this data loaded in the Slicer, there is not show window in the Slicer.

When I loaded the DICOM, only the dicom image is shown.

and then I choose donot show DICOM image, still nothing in the shown window, even I adjusted the Slice viewer’s FOV.

There must be something wrong in my operation, but i cannot figure out. Any help is appreciated.

Did you try to load the labelmap by itself as is? Does it have anything in it?

I tried to load the labelmap(nii.gz) as volume and uncheck SingleFile after check “show options”, the mark can be shown.

There seems like a 90 degree rotation with respect to your dicom image. That’s probably why it is not lining up.

Thanks @muratmaga. The dicom image size and labelmap image size is both 512*512. So it should not effected by the labelmap is rotation or not. Anyway I will try to adjust the rotation and do it again. I will update later. Thanks again. :grinning:

I tried to load the labelmap which is rotated to the right oritation. But still face the same problem. Is there any documentation I can refer to or some video tutourial? thanks a lot.

hi muratmaga, I found the labelmap which loaded from niigz file can be shown as 3D volume. But seems cannot be overlaied on the dicom images. here is the screenshot.


Here is the loaded node list.

How to make them shown together?

I tried to save a segmentation as nii.gz file with slicer.util.saveNode(labelmapVolumeNode, nii_path). This nii.gz file can be shown normally with dicom images when it was loaded as segmentation. But the nii.gz file I saved with the code segment below can not work.

nii = nib.Nifti1Image(segmentation_array, np.eye(4))
nib.save(nii, path+'/t_'+batch_dir+'.nii.gz')

Anything wrong in my code? Any help would be much appreciated.
t_1_2 in the node list is generated with nib.save. it cannot be shown.

1 in the node list is generated with slicer.util.saveNode. it can be shown.

You pass an identity matrix to nibabel instead of the IJK to LPS matrix. You can get IJK to RAS matrix from the volume node, convert to IJK to LPS, and pass it to nibabel, but it is easier to just call slicer.util.saveNode() instead. If you want to preserve segment colors, names, etc. then call slicer.util.saveNode() on the segmentation node (not on the exported labelmap volume node).

Note that Nifti file format is for brain MRI images only. As a general-purpose 3D image or segmentation file format, I would recommend to use NRRD instead (it has human-readable header, can store custom metadata, and has many other advantages over Nifti).

Thanks a lot, lassoan. I am so happy to see your reply after my long leave(almost seven monthes). unfortunately I have only the nifti file generated by the other program currently. so it cannot be used to load node, right? I tried it just now, and failed with the error “failed to load node from file XXX.nii.gz”. maybe the first solution(get IJK to RAS matrix from the volume node) is the only solution. I can get the IJKtoRAS matrix with the Node.GetIJKToRASMatrix() function. Is there any slicer’s function I can use to convert it to IJKtoLPS matrix? Thanks again. Looking forward to your reply.

I confirm that loading of segmentation from .nii.gz file works well in recent Slicer versions (in Add data window, choose Description → Segmentation, to load directly as segmentation). If you have any problems with a specific file then the best would be if you could share it with us (upload it somewhere and post the link here).

My nifti file’s affine matrix is identity matrix, so it cannot be shown with the volume together. According to your reply in Aug, 20th 2020, I get the solution to get the IJKtoRAS matrix from the volume data, but how to convert it to IJKtoLPS matrix? Is there some existing function can do this? thank s lot.

Probably the simplest is to save both the volume and segmentation as .mhd file and then copy the space, space directions and space origin field values from the volume node to the segmentation node.

Actually, I am the roobie of Slicer and medical image format. According to your axplaination about the nifti and nrrd, nrrd maybe more suitable to my work, but I only have the binary segmentation result, no header info neither.
I found a solution that save the volume node with saveNode function to nifti image, so I can get the affine matrix from the volume nifti image. But the saveNode return value is ‘False’, where can I find the usage of the saveNode? Sometimes the return value is True, but the output file is not generated in my laptop.
And I want to check the node which I wanna save, but the return result is the memory address. How can I check the content in the memory? See the pic below. Thanks a lot. looking forward to your reply.
image

Finally, I found the simple solution donot need to save node. I compared the IJKtoRAS matrix from the volume and the affine matrix from the nifti file(generated by saveNode). They are totally same. That means IJKtoRAS matrix can be used directly to save nifti. And then import the nifti file again, it can be shown with volume together.
Thanks for all the help.

1 Like