Adding new segments from multiple numpy arrays

Hello,

I am working on importing numpy arrays into a segmentation so that they are shown in the segment editor as new segments.

What I have now after seeing some examples is:

label_map_volume = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
slicer.util.updateVolumeFromArray(label_map_volume, segmentation_mask)
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(label_map_volume, segmentation_node)
slicer.util.updateSegmentBinaryLabelmapFromArray(
            segmentation_mask, 
            segmentation_node, 
            label_map_volume.GetName(), 
            self.volume_node)
slicer.mrmlScene.RemoveNode(label_map_volume)

Where segmentation_node is the numpy array with the new segment to be added to the segmentation and self.volume_node.

The problem is that every segment adds to memory about as much as the volume size, as if the whole array is saved and shown, so after ~20 to 30 segments Slicer slows down. I considered using a single array to store all the segmentations because overlapping is not a problem, but I do not know if Slicer works more efficiently than this under the hood and this approach is not optimal. I assume this is not a memory leak because the label map node is deleted once the segmentation has been updated. What is the best option from here?

Thank you for the help.

Slicer is in fact more efficient than storing the entire extent as many times as the number of segments. Originally, each segment only took as much space as its actual content. Later, a “layer” mechanism was introduced to make this even more efficient, however, I am not super familiar with the implementation of this part. I assume the numpy importer takes the simplest approach possible, and the effective extents and layers are not considered. I’d try to call segmentation_node.GetSegmentation().CollapseBinaryLabelmaps() and see if it solves the issue.

This seems to solve the issue. Now only a very small amount of memory is taken for each new segment. Thank you!

1 Like