# How to create a mask from some ordered points?

**URL:** https://discourse.vtk.org/t/how-to-create-a-mask-from-some-ordered-points/8292
**Category:** Support
**Created:** [April 13, 2022, 2:40pm UTC](https://discourse.vtk.org/t/how-to-create-a-mask-from-some-ordered-points/8292 "2022-04-13T14:40:46Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [April 13, 2022, 2:40pm UTC](https://discourse.vtk.org/t/how-to-create-a-mask-from-some-ordered-points/8292/1 "2022-04-13T14:40:46Z")

</div>

I want to create a binary mask from some ordered points.

For example, for the points:

```auto
ps = [
    [64, 64, 0],
    [128, 64, 0],
    [128, 128, 0],
    [64, 128, 0],
]

```

I hope it can create a binary mask which is agree with:

```auto
import numpy as np
img = np.zeros(shape=[256, 256])
img[64:128, 64:128] = 1

```

I find `vtkPolyDataToImageStencil`&`vtkImageStencilToImage` may be the solution. And my code is:

```auto
import vtkmodules.all as vtk
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk

ps = [
    [64, 64, 0],
    [128, 64, 0],
    [128, 128, 0],
    [64, 128, 0],
]

polydata = vtk.vtkPolyData()
points = vtk.vtkPoints()
polygon = vtk.vtkPolygon()
polygon.GetPointIds().SetNumberOfIds(len(ps))

for idx, p in enumerate(ps):
    points.InsertNextPoint(p[0], p[1], p[2])
    polygon.GetPointIds().SetId(idx, idx)

polygons = vtk.vtkCellArray()
polygons.InsertNextCell(polygon)
polydata.SetPoints(points)
polydata.SetPolys(polygons)

polyDataToImageStencil = vtk.vtkPolyDataToImageStencil()
polyDataToImageStencil.SetInputData(polydata)
polyDataToImageStencil.SetOutputOrigin(0, 0, 0)
polyDataToImageStencil.SetOutputSpacing([1, 1, 1])
polyDataToImageStencil.SetOutputWholeExtent([0, 255, 0, 255, 0, 0])
polyDataToImageStencil.Update()

imgStencilToImage = vtk.vtkImageStencilToImage()
imgStencilToImage.SetInputConnection(polyDataToImageStencil.GetOutputPort())
imgStencilToImage.SetInsideValue(1)
imgStencilToImage.SetOutsideValue(0)
imgStencilToImage.Update()

vtkMask = imgStencilToImage.GetOutput()
mask = vtk_to_numpy(vtkMask.GetPointData().GetScalars())
print(mask.sum())

```

However, the final mask is all 0.

What’s wrong with my code? Any suggestion is appreciated~~~

---

<div class="post-metadata">

### Author: ![isachpaz](https://discourse.vtk.org/user_avatar/discourse.vtk.org/isachpaz/32/9965_2.png) [@isachpaz](https://discourse.vtk.org/u/isachpaz)
#### Post date: [May 7, 2025, 1:31pm UTC](https://discourse.vtk.org/t/how-to-create-a-mask-from-some-ordered-points/8292/2 "2025-05-07T13:31:31Z")

</div>

Hi,  
Is it already solved? I have the same issue. Could someone provide any hint?

May thanks,  
Ilias
