Getting a 2D binary mask from a 3D Binary Volume

My segmentation is a 3D binary volume (shown in green in the screenshot) imported from a RTSS file. I need to do two things:

  1. Extract the 2D mask in all three planar directions (the specific voxels that are shown in all three slices)
  2. Convert this 2D region to a contour. (There are algorithms for doing that, so I essentially just need to extract the mask from part 1)

I’ve figured out that slicer.util.getNode(‘1: RTSTRUCT: AutoSS’) will return a vthMRMLSegmentationNode, but I can’t figure out how to traverse it to get the segmentation. Is there an easy way to just export the 3d array of zeros and ones using python?

Of course! Probably the easiest way is to export to labelmap and get voxels as numpy array.

Thanks a lot. I just tried it and I’m getting an error when I try running ExportAllSegmentsToLabelmapNode…

(I’m using Slicer 4.10.2)

Examples in the “Nightly” script repository page work with the latest Slicer Preview Release (which I would highly recommend to use, as latest stable is very old now and we added tons of features and fixes since then).

Great. It seems to work in 4.11. Thank you. Just one question though. When I show the Labelmap as well as the segments, they don’t seem to match up completely. (the green segment in the Yellow and Green slices appear much more blocky)

Is this just a case of changing the resolution when I export to a Labelmap?

You see two different representations of the same segmentation (closed surface and binary labelmap). The higher the resolution is in the reference geometry (or higher the oversampling factor is) the labelmap, the closer is the match between the two representations. See more information about representations in segmentations here: https://slicer.readthedocs.io/en/latest/user_guide/image_segmentation.html

I believe I understand. Is there an easy way to extract the contour representation from Python? When it exports as a DICOM RT file, I believe this is what gets output as the contour data field (tag 3006,0050). I’m sure it’s another function under segmentations.logic(), but I can’t find anything obvious in the API docs.

I will be doing a transform on the data, and want to extract the contour along a specific plane, in the same format (i.e. a sequence of points) that would be exported to a Dicom RT file.

Yes, see examples in the script repository: https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Segmentations

On that page, I only see how to extract two representations, binary label map and closed surface.

I don’t see a way to extract the contours. Does this have to be done by exporting the closed surface and using a function in the vtk library?

These are convenience functions for the most useful representations. Most likely you actually want the closed surface representation and not the contours (that is the one you see on screen).

Parallel contours representation is really, really bad way to store a 3D shape, but if you are curious how you can access it: you can do it via segmentation node -> GetSegmentation -> GetSegment-> GetRepresentation.