How to get segmentations as numpy arrays with same dimensions?

hello, lassoan. I encountered the same problem when save the segmentation result to file. I right clicked the segmentation in the “Subject hierarchy”, and then choose “export visible segments to binary labelmap”, and got the labelmap which name is “Segmentation-bone-label”. And then I got the segmentation mask array using python. But the size of each image in segmentation mask is only 224332, and the original image in volume is 512512. I got the segmentation mask which size is same as original image before. And I donot know why this time the size is only bounding box. Here is the python code I used:

bone=array(“Segmentation-bone-label”)
bone.shape
(114, 224, 322)

volume = array(“5 ARTERIAL PHASE”)
volume.shape
(114, 512, 512)

Looking forward to your reply, thank you so much.

The simplest is to use the latest Slicer version, which does not crop the segmentation to the minimum necessary size. If you already created your segmentation in Slicer-4.10 then you need to specify a Reference volume as described above.

The version is 4.11(2020-6-15). And where can i find the advanced export section in 4.11 to add reference volume. In the screenshot below, you can see the segmentation size is not 512*512, and the Slicer version is 4.11.

How did you create Segmentation_1-bone-label and Segmentation_2-bone-label?
If they were created from two different segmentations (of two different volumes), could you check the dimensions of these volumes (in Volumes module, Volume Information section)?

In addition to @lassoan replies, you can change the segmentation geometry to match volume geometry with SetReferenceImageGeometryParameterFromVolumeNode(). This allowed me in the past to batch process mismatched examples, e.g.

loadedVolumeNode = slicer.util.loadVolume(ct_path)
loadedSegmentationNode = slicer.util.loadSegmentation(seg_path)
loadedSegmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(loadedVolumeNode)
slicer.util.saveNode(loadedSegmentationNode, seg_path)
slicer.mrmlScene.RemoveNode(loadedVolumeNode)
slicer.mrmlScene.RemoveNode(loadedSegmentationNode)
3 Likes

yes, they are from differnent volumes.Here is how can I create the labelmap in the scrrenshot 1-2. and the third screenshot showes the volume dimension. And I tried to get the dimension of the labelmap, the result is shown below.

bone = array(“Segmentation_5-bone-label”)
bone.shape
(118, 276, 382)

volume = array(“4 Tx Abdomen 5mm”)
volume.shape
(118, 512, 512)

I found if I save the segmentation to nrrd file first, and then load it and volume again. and then the labelmap created from the segmentation is bounding box size. If I save the segmentation directly after segmentation, the labelmap size is same as the volume(512*512).

Your solution works greatly. Thanks a lot. :+1:
BTW, do you know how to get the node name (like loadSegmentationNode and loadedVolumeNode in your code) if I load the volume and segmentation from GUI. thanks again.