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

**URL:** <https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422>\
**Category:** Development\
**Created:** [September 10, 2020, 1:36pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422 "2020-09-10T13:36:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanakm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evanakm/32/6725_2.png) [@evanakm](https://discourse.slicer.org/u/evanakm)\
**Post date:** [September 10, 2020, 1:36pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/1 "2020-09-10T13:36:29Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/9/0944533c22c86c5def188721e61f28468a2a5b60.jpeg)

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?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 10, 2020, 1:41pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/2 "2020-09-10T13:41:04Z")

</div>

> [@evanakm](#):
>
> 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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Export_labelmap_node_from_segmentation_node) and [get voxels as numpy array](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_value_of_a_volume_at_specific_voxel_coordinates).

---

<div class="post-metadata">

**Author:** ![evanakm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evanakm/32/6725_2.png) [@evanakm](https://discourse.slicer.org/u/evanakm)\
**Post date:** [September 10, 2020, 1:50pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/3 "2020-09-10T13:50:32Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/f/ff272b80d8049c819ad8b6a6f7a9681d46bd164f.png)

(I’m using Slicer 4.10.2)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 10, 2020, 1:52pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/4 "2020-09-10T13:52:40Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![evanakm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evanakm/32/6725_2.png) [@evanakm](https://discourse.slicer.org/u/evanakm)\
**Post date:** [September 10, 2020, 2:24pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/5 "2020-09-10T14:24:52Z")

</div>

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)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/7/e756683c4a634aad9460192174f1f58c67567aa0.jpeg)

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 10, 2020, 2:54pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/6 "2020-09-10T14:54:09Z")

</div>

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](https://slicer.readthedocs.io/en/latest/user_guide/image_segmentation.html)

---

<div class="post-metadata">

**Author:** ![evanakm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evanakm/32/6725_2.png) [@evanakm](https://discourse.slicer.org/u/evanakm)\
**Post date:** [September 10, 2020, 5:59pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/7 "2020-09-10T17:59:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 10, 2020, 6:10pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/8 "2020-09-10T18:10:52Z")

</div>

> [@evanakm](#):
>
> Is there an easy way to extract the contour representation from Python?

Yes, see examples in the script repository: [Documentation/Nightly/ScriptRepository - Slicer Wiki](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Segmentations)

---

<div class="post-metadata">

**Author:** ![evanakm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evanakm/32/6725_2.png) [@evanakm](https://discourse.slicer.org/u/evanakm)\
**Post date:** [September 10, 2020, 6:27pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/9 "2020-09-10T18:27:54Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/a/eabc4e0dfc6d5e676795faba29acfd6b3d2f91ed.png)

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?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 10, 2020, 6:35pm UTC](https://discourse.slicer.org/t/getting-a-2d-binary-mask-from-a-3d-binary-volume/13422/10 "2020-09-10T18:35:04Z")

</div>

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](http://apidocs.slicer.org/master/classvtkSegment.html#a9a890bc95ebdd9b035d655b9075689cd).
