AddSegmentFromClosedSurfaceRepresentation performance

Hello,

I am working on a custom module where I need to add many (360) objects to an existing segmentation node. Although the code seems very simple it takes hours to finish. I am not sure what causes the slowdown. Maybe there is a way to add all objects in a batch without calling AddSegmentFromClosedSurfaceRepresentation for each object. I will greatly appreciate any help.

My code:

segmentationNode = slicer.mrmlScene.GetFirstNodeByName("My segmentation")
for wedge in wedges:
      wedgeID = segmentationNode.AddSegmentFromClosedSurfaceRepresentation(wedge["wedgePD"], wedge["seg_name"], wedge["seg_colour"])

The time to execute AddSegmentFromClosedSurfaceRepresentation increases with the number of wedges added. Below is the time to execution time for first 30 steps
1st call to AddSegmentFromClosedSurfaceRepresentation finished in 0.009
2nd call to AddSegmentFromClosedSurfaceRepresentation finished in 0.188
3rd call to AddSegmentFromClosedSurfaceRepresentation finished in 0.248
…
10th call to AddSegmentFromClosedSurfaceRepresentation finished in 0.448
…
30th call to AddSegmentFromClosedSurfaceRepresentation finished in 1.101

You probably have multiple representations in your segmentation. Each time you add a new segment, all the representations might need to be recomputed. You can make adding a segment take 0 seconds by setting the source (master) representation to “closed surface” and remove the “binary labelmap” representation.

1 Like

Setting the master representation as closed surface helped, but it didn’t make it instant. Right now it takes around 0.5-0.8 sec. to add one surface. I have a follow up question. How do I check how many representations I have? When I open the Segmentation module and select the active segmentation I only see one segmentation.

May be able to speed up the import if you do enclose all the import steps between a StartModify and an EndModify call.

If you only have one representation listed then you have only that representation. You can get the list of representation by calling methods od the vtkSegmentation object that is stored in the segmentation node.