# Fill / Color area of closed polyline

**URL:** https://discourse.vtk.org/t/fill-color-area-of-closed-polyline/10497
**Category:** Support
**Created:** [January 19, 2023, 11:23am UTC](https://discourse.vtk.org/t/fill-color-area-of-closed-polyline/10497 "2023-01-19T11:23:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![SebastianWilke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastianwilke/32/6441_2.png) [@SebastianWilke](https://discourse.vtk.org/u/SebastianWilke)
#### Post date: [January 19, 2023, 11:23am UTC](https://discourse.vtk.org/t/fill-color-area-of-closed-polyline/10497/1 "2023-01-19T11:23:56Z")

</div>

Hello!  
I am rendering DICOM contours (Yes, against advice to use existing implementations). At the moment, I am creating `vtkPolyData` using `vtkPoints` and `vtkCellArray` from the 3D-point data extracted from DICOM. Then, I put this data through `vtkLinearExtrusionFilter`, `vtkPolyDataToImageStencil` and `vtkImageStencil` to retrieve `vtkImageData`, which I render using a `vtkImageViewer2`. This works, but now I want to fill / color the area of this closed polyline.

I might be able to do so using an approach similar to `vtkPolygon::PointInPolygon()`, potentially using `vtkContourLoopExtraction`. Yet, I don’t have `vtkPolygon`-data but it might be extractable somewhere along the pipeline?

My question is different from [How to color or fill the selected contour?](https://discourse.vtk.org/t/how-to-color-or-fill-the-selected-contour/3887), as I don’t use `vtkContourWidget`.

My question(s):

- Where in my pipeline would I include the filling/coloring of the area enclosed by the contour?
- How would I do that?

Any help is appreciated. Cheers!  
Sebastian

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 20, 2023, 2:22pm UTC](https://discourse.vtk.org/t/fill-color-area-of-closed-polyline/10497/2 "2023-01-20T14:22:17Z")

</div>

I think that what you want is vtkContourTriangulator, which will fill the contour with triangles so that its interior can be rendered (rather than just the edges). Or do you want to color the vtkImageData voxels that lie within the contour?

---

<div class="post-metadata">

### Author: ![SebastianWilke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastianwilke/32/6441_2.png) [@SebastianWilke](https://discourse.vtk.org/u/SebastianWilke)
#### Post date: [January 20, 2023, 4:14pm UTC](https://discourse.vtk.org/t/fill-color-area-of-closed-polyline/10497/3 "2023-01-20T16:14:22Z")

</div>

Thank you for the suggestion. I indeed want to do the latter.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 20, 2023, 4:28pm UTC](https://discourse.vtk.org/t/fill-color-area-of-closed-polyline/10497/4 "2023-01-20T16:28:56Z")

</div>

If you want to fill that part of the image, and leave the rest of the image alone, you can do that by reversing `vtkImageStencil`. For example,

```python
fill_value = 0
imageStencil.ReverseStencil();
imageStencil.SetBackgroundValue(fill_value);

```

If you pass the image through `vtkImageMapToColors` first (so that it is RGB), then you can do this:

```python
red = (1.0, 0.0, 0.0)
imageStencil.ReverseStencil();
imageStencil.SetBackgroundColor(red);

```

Or you can create a binary image with `vtkImageStencilToImage`

---

<div class="post-metadata">

### Author: ![SebastianWilke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastianwilke/32/6441_2.png) [@SebastianWilke](https://discourse.vtk.org/u/SebastianWilke)
#### Post date: [January 22, 2023, 6:47pm UTC](https://discourse.vtk.org/t/fill-color-area-of-closed-polyline/10497/5 "2023-01-22T18:47:43Z")

</div>

Thank you very much - this is exactly what I need!
