# Creating mask from rtstruct

**URL:** https://discourse.itk.org/t/creating-mask-from-rtstruct/3826
**Category:** Uncategorized
**Tags:** python, dicom, simpleitk
**Created:** [February 2, 2021, 4:09pm UTC](https://discourse.itk.org/t/creating-mask-from-rtstruct/3826 "2021-02-02T16:09:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![roirodriguez](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/ea5d25/32.png) [@roirodriguez](https://discourse.itk.org/u/roirodriguez)
#### Post date: [February 2, 2021, 4:09pm UTC](https://discourse.itk.org/t/creating-mask-from-rtstruct/3826/1 "2021-02-02T16:09:06Z")

</div>

Hi,

I’m just beginning with SimpleITK and all other medical image related software. I’m actually loading some dicom series and their corresponding RTSTRUCT contours in python, manually with numpy, because as far as i know there’s no support for loading RTSTRUCT into SimpleITK. Then i convert those contours to a mask, then load both image serie and mask into SimpleITK and from that into Pyradiomics.

My problem is with my contour to mask conversion, i’ve written a first approach simple algo, but it does not work well at all. I’m wondering if there’s some filter or other feature in SimpleITK helping with that task. I guess that would imply creating kind of a mesh from the 3d contour, maybe softening it, and pass and algorithm to determine interior voxels to it. Is there such a feature in SimpleITK? Or any other approach anybody can recommend?

Thank you in advance.  
Roi

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [February 2, 2021, 4:38pm UTC](https://discourse.itk.org/t/creating-mask-from-rtstruct/3826/2 "2021-02-02T16:38:04Z")

</div>

Yeah, scan converting complex polygons into pixels is a tricky, non-trivial process. I wrote some python code do to it that you can see here:

> <https://github.com/dave3d/dicomrt_tools/blob/main/drt2volume.py>

It uses VTK’s vtkPolyDataToImageStencil filter to convert polyline to image stencils.

---

<div class="post-metadata">

### Author: ![roirodriguez](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/ea5d25/32.png) [@roirodriguez](https://discourse.itk.org/u/roirodriguez)
#### Post date: [February 2, 2021, 4:44pm UTC](https://discourse.itk.org/t/creating-mask-from-rtstruct/3826/3 "2021-02-02T16:44:07Z")

</div>

Thanks a lot! i’ll try and read your github repo.  
EDIT: Still fighting with this, i seem to get unconnected slices, have to take a look.

Anyone knows any simpleitk approach?

---

<div class="post-metadata">

### Author: ![asim-shrestha](https://discourse.itk.org/user_avatar/discourse.itk.org/asim-shrestha/32/1912_2.png) [@asim-shrestha](https://discourse.itk.org/u/asim-shrestha)
#### Post date: [February 10, 2021, 3:34am UTC](https://discourse.itk.org/t/creating-mask-from-rtstruct/3826/4 "2021-02-10T03:34:49Z")

</div>

Hey my team and I actually wrote a small Python package called RT-Utils that I think can help you out! The package will allow you to load a 3D numpy binary mask for any contour within an RTStruct and deal with things such as holes and disconnected components in contours automatically. You can find the package here [https://github.com/qurit/RT-Utils](https://github.com/qurit/RT-Utils) and install via pip. Your use case would fall under the _Loading an existing RT Struct contour as a mask_ section and example code would be as simple as:

```
mask_3d = rtstruct.get_roi_mask_by_name("ROI NAME")
```

---

<div class="post-metadata">

### Author: ![BrianMAnderson](https://discourse.itk.org/user_avatar/discourse.itk.org/brianmanderson/32/3234_2.png) [@BrianMAnderson](https://discourse.itk.org/u/BrianMAnderson)
#### Post date: [February 16, 2023, 5:45pm UTC](https://discourse.itk.org/t/creating-mask-from-rtstruct/3826/5 "2023-02-16T17:45:57Z")

</div>

The code shown by Asim is great and very useful! If you would like something else to parse through folders of DICOM, or are working with non-axially acquired MR images, please also check out our code here: [GitHub - brianmanderson/Dicom\_RT\_and\_Images\_to\_Mask: Various utilities created to help with the interpretation of dicom images/RT Structures, specifically into NumPy masks and Nifti images. Some necessary steps for deep learning](https://github.com/brianmanderson/Dicom_RT_and_Images_to_Mask)  
The technical note for the program is here: [Simple Python Module for Conversions Between DICOM Images and Radiation Therapy Structures, Masks, and Prediction Arrays - ScienceDirect](https://www.sciencedirect.com/science/article/abs/pii/S1879850021000485)
