Segment surface calculation

I am using this script to determine the volume and surface area of my segments.

segmentationNode = getNode("Segmentation")

# Compute segment statistics
import SegmentStatistics
segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
segStatLogic.computeStatistics()
stats = segStatLogic.getStatistics()

# Display volume and surface area of each segment
for segmentId in stats["SegmentIDs"]:
  volume_mm3 = stats[segmentId,"LabelmapSegmentStatisticsPlugin.volume_mm3"]
  surface_mm2 = stats[segmentId, "ClosedSurfaceSegmentStatisticsPlugin.surface_mm2"]
  segmentName = segmentationNode.GetSegmentation().GetSegment(segmentId).GetName()
  print(f"{segmentName} volume = {volume_mm3} mm3")
  print(f"{segmentName} volume = {surface_mm2} mm2")

The script works fine when the segments are displayed in 3D, but it works only for the volume when the 3D view is off.
When I have too large or complex volume/segments, I cannot display them in 3D because I get this error:

“Slicer has caught an application error, please save your work and restart.\n\nThe application has run out of memory. Increasing virtual memory size in system settings or adding more RAM may fix this issue.\n\nIf you have a repeatable sequence of steps that causes this message, please report the issue following instructions available at https://slicer.org\n\n\nThe message detail is:\n\nException thrown in event: bad array new length”

I guess the files are too complex for my computer and there is not much I can do about it. Although it is strange because this computer has 192 GB of RAM.

Is there another way to determinate the surface of the segments avoiding 3D visualization?

What size is the input? Just curious why 192 GB is not enough.

I’m not sure if this helps, but there is a new conversion method to closed surface, which may (not sure!) use less memory. You can enable it like this:

image

The volume I’m currently working with is a 1.4 GB .vtk file.
The more simple segments are displayed correctly in 3D. But I have a large and perhaps more complex one that cannot be displayed (even if segmented alone).
It is also strange that only about 50GB of RAM are in use when I get the error.

I tried the method you suggested but it only allows a small part of the volume to be displayed and then stops, but no error is shown.

I assume that the segment in question is not a single object, but maybe a set of disconnected and complex features. In that case calculating the surface may not even make sense. If you don’t insist on calculating the surface area of that segment, I suggest making a clone of the segmentation, removing that segment, and run the calculations on all the other segments.