# How to create a binary mask from coordinates (indexes or physical)

**URL:** <https://discourse.itk.org/t/how-to-create-a-binary-mask-from-coordinates-indexes-or-physical/4611>\
**Category:** Beginner Questions\
**Created:** [November 24, 2021, 9:26pm UTC](https://discourse.itk.org/t/how-to-create-a-binary-mask-from-coordinates-indexes-or-physical/4611 "2021-11-24T21:26:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ervis](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@Ervis](https://discourse.itk.org/u/Ervis)\
**Post date:** [November 24, 2021, 9:26pm UTC](https://discourse.itk.org/t/how-to-create-a-binary-mask-from-coordinates-indexes-or-physical/4611/1 "2021-11-24T21:26:26Z")

</div>

Hi  
I am new to ITK , I am using SimpleITK in C#.  
I have planar closed contour , a set of coordinates. What I need to do is create a binary mask with the same dimensions spacing and origin as the original ITK Image.  
I am not sure which filters would be the correct ones to use for this purpose.  
I have tried to perform this task using VTK with limited success

Any help and pointers would be greatly appreciated

Thank you

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [November 24, 2021, 9:45pm UTC](https://discourse.itk.org/t/how-to-create-a-binary-mask-from-coordinates-indexes-or-physical/4611/2 "2021-11-24T21:45:10Z")

</div>

Hello @Ervis,

ITK and SimpleITK do not support this type of operation (polygon rasterization).

You were looking in the right place, VTK. Have you tried asking on the [vtk discourse](https://discourse.vtk.org/)? I suspect there are other C# libraries that allow you to `drawPolygon`/`fillPolygon`.

Once you have the polygon in a buffer you can [copy it over to a SimpleITK image](https://simpleitk.org/doxygen/latest/html/CSharp_2ImageGetSetBuffer_8cs-example.html).

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 24, 2021, 9:48pm UTC](https://discourse.itk.org/t/how-to-create-a-binary-mask-from-coordinates-indexes-or-physical/4611/3 "2021-11-24T21:48:49Z")

</div>

What is wrong with [vtkPolyDataToImageStencil](https://kitware.github.io/vtk-examples/site/Cxx/PolyData/PolyDataToImageData/)? Or its cousin [vtkLassoStencilSource](https://github.com/Kitware/VTK/blob/master/Imaging/Core/Testing/Cxx/TestStencilWithLasso.cxx)? Possibly relevant [old discussion](https://itk.org/pipermail/insight-users/2006-February/016593.html).

---

<div class="post-metadata">

**Author:** ![Ervis](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@Ervis](https://discourse.itk.org/u/Ervis)\
**Post date:** [November 25, 2021, 1:49pm UTC](https://discourse.itk.org/t/how-to-create-a-binary-mask-from-coordinates-indexes-or-physical/4611/4 "2021-11-25T13:49:07Z")

</div>

The issue with vtkPolyDataToImageStencil is that it seems to create artifacts, even thought the contour region is 100% fully closed. What seems to me to be random is entire lines missing from the contours ,for example :

![Capture](https://discourse.itk.org/uploads/default/original/2X/7/7c6f445c81a01d151a0afb7e728668af2d4e0c66.jpeg)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 26, 2021, 4:10pm UTC](https://discourse.itk.org/t/how-to-create-a-binary-mask-from-coordinates-indexes-or-physical/4611/5 "2021-11-26T16:10:11Z")

</div>

You are probably better off trying to fix those bugs in VTK, than to write your own implementation.

---

<div class="post-metadata">

**Author:** ![jasq](https://discourse.itk.org/user_avatar/discourse.itk.org/jasq/32/3067_2.png) [@jasq](https://discourse.itk.org/u/jasq)\
**Post date:** [November 23, 2022, 4:22pm UTC](https://discourse.itk.org/t/how-to-create-a-binary-mask-from-coordinates-indexes-or-physical/4611/6 "2022-11-23T16:22:21Z")

</div>

You can try fredtools (www.fredtools.ifj.edu.pl) to map dicom structure to an image to create a mask.

disclosure: I am the author.
