# Best way to get a silhouette of a mesh?

**URL:** <https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897>\
**Category:** Development\
**Tags:** python, code\
**Created:** [May 16, 2024, 7:26pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897 "2024-05-16T19:26:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dzenanz/32/212_2.png) [@dzenanz](https://discourse.vtk.org/u/dzenanz)\
**Post date:** [May 16, 2024, 7:26pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/1 "2024-05-16T19:26:07Z")

</div>

![Screenshot 2024-05-16 13.18.02](https://discourse.vtk.org/uploads/default/original/2X/e/e5618697b09913b31188342e5346197100717c1e.jpeg)  
Given a mesh in the top left, I would like a silhouette on the top right. I know I can use [vtkCutter](https://kitware.github.io/vtk-js/examples/Cutter.html) to get an outline created by intersection with a plane (bottom right). I tried [vtkPolyDataSilhouette](https://examples.vtk.org/site/Cxx/PolyData/Silhouette/) (bottom left), but I find it unsatisfactory - there are too many extraneous edges. Is there a way to improve it?

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [May 17, 2024, 2:07am UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/2 "2024-05-17T02:07:38Z")

</div>

If this is for highlighting an actor then you can also use the outline glow rendering pass.

[https://examples.vtk.org/site/Cxx/Rendering/OutlineGlowPass/](https://examples.vtk.org/site/Cxx/Rendering/OutlineGlowPass/)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dzenanz/32/212_2.png) [@dzenanz](https://discourse.vtk.org/u/dzenanz)\
**Post date:** [May 17, 2024, 12:41pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/3 "2024-05-17T12:41:17Z")

</div>

It is not to highlight an actor. It is to draw the outline on a 2D image (e.g. 2D slice or an X-ray). I assume it is possible to read the frame buffer, and set up object, outline, and background colors to make it possible to extract the outline. However this seems like so much effort that cutter approach sounds very appealing.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [May 17, 2024, 12:56pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/4 "2024-05-17T12:56:14Z")

</div>

You can also project the model to the slice plane (simple projection matrix). This will give you a filled model. If you need the outline of that then you can use some 2D image filtering.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/3/3d6e2e92ba2b42492b24371b18707c520f04baf4.jpeg)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dzenanz/32/212_2.png) [@dzenanz](https://discourse.vtk.org/u/dzenanz)\
**Post date:** [May 17, 2024, 1:23pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/5 "2024-05-17T13:23:47Z")

</div>

I would like to have this in a stand-alone library, written in Python. Could you point me to the implementation of this feature in the code? I looked at [vtkSlicerModelsLogic](https://github.com/Slicer/Slicer/blob/v5.6.2/Modules/Loadable/Models/Logic/vtkSlicerModelsLogic.cxx), [vtkMRMLModelDisplayNode](https://github.com/Slicer/Slicer/blob/v5.6.2/Libs/MRML/Core/vtkMRMLModelDisplayNode.cxx#L486-L489), and [qMRMLModelDisplayNodeWidget](https://github.com/Slicer/Slicer/blob/v5.6.2/Modules/Loadable/Models/Widgets/qMRMLModelDisplayNodeWidget.cxx#L687-L706), but I could not figure out where is the code to transform a mesh into a 2D image.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [May 17, 2024, 1:28pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/6 "2024-05-17T13:28:49Z")

</div>

This is a display option, so it is implemented in the displayable manager of models. See the source code [here](https://github.com/Slicer/Slicer/blob/332732c558c041622b33129a3a402cabcadda73f/Libs/MRML/DisplayableManager/vtkMRMLModelSliceDisplayableManager.cxx#L463-L470) for turning the transformation matrix that you would use for getting slice intersection to a matrix that provides slice projection.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dzenanz/32/212_2.png) [@dzenanz](https://discourse.vtk.org/u/dzenanz)\
**Post date:** [May 17, 2024, 1:42pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/7 "2024-05-17T13:42:48Z")

</div>

Thank you. This sounds reasonable. Make a projection matrix that way, then use [PIL’s ImageDraw.polygon()](https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.polygon) to draw all the mesh’s triangles onto a 2D image, then binary erosion (from ITK, SciPy, or OpenCV) and subtraction to get the outline.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [May 17, 2024, 3:02pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/8 "2024-05-17T15:02:41Z")

</div>

Yes, this should work. Probably rasterizing the polygons using VTK would be much faster than PIL. You can implement the image filtering using VTK, too, if you don’t want to use additional (and potentially slower) libraries.

---

<div class="post-metadata">

**Author:** ![omdaniel](https://discourse.vtk.org/user_avatar/discourse.vtk.org/omdaniel/32/8538_2.png) [@omdaniel](https://discourse.vtk.org/u/omdaniel)\
**Post date:** [May 17, 2024, 3:17pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/9 "2024-05-17T15:17:20Z")

</div>

If you have the polygon already you can just create on outline by drawing the boundary edge directly without having to do a raster image erosion and subtraction to get the outline

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [May 18, 2024, 11:31am UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/10 "2024-05-18T11:31:40Z")

</div>

There is not just one polygon but many of overlapping polygons, potentially millions.

The GPU can rasterize them in near-zero time even if there are millions, so it is a safe bet.

But it is true that you could also get the silhouette by computing the union of all the polygons. I don’t think there is a VTK filter for it, but you can use shapely or others. It would be interesting to see how much time it takes.

---

<div class="post-metadata">

**Author:** ![omdaniel](https://discourse.vtk.org/user_avatar/discourse.vtk.org/omdaniel/32/8538_2.png) [@omdaniel](https://discourse.vtk.org/u/omdaniel)\
**Post date:** [May 18, 2024, 3:43pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/11 "2024-05-18T15:43:54Z")

</div>

That makes sense and I see why you recommend doing the heavy liftering in vtk as opposed to rasterizing possibly millions of polygons in Python with PIL, I was recently doing some compositing work with PIL and found it rather slow.

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)\
**Post date:** [May 18, 2024, 5:10pm UTC](https://discourse.vtk.org/t/best-way-to-get-a-silhouette-of-a-mesh/13897/12 "2024-05-18T17:10:23Z")

</div>

Maybe using the featureEdges filter of VTK with the boundaryEdges option on the planar projection of the mesh could help 🙂

Hope it helps
