# Calculate external contour for segment in segmentation

**URL:** <https://discourse.slicer.org/t/calculate-external-contour-for-segment-in-segmentation/24653>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [August 5, 2022, 2:30pm UTC](https://discourse.slicer.org/t/calculate-external-contour-for-segment-in-segmentation/24653 "2022-08-05T14:30:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [August 5, 2022, 2:30pm UTC](https://discourse.slicer.org/t/calculate-external-contour-for-segment-in-segmentation/24653/1 "2022-08-05T14:30:12Z")

</div>

I’m trying to calculate external contour and then transform it into markups curve node.Here is a [VTK example](https://kitware.github.io/vtk-examples/site/Cxx/PolyData/ExternalContour/) with such case. Position and orientation of camera are known. Segment polydata is white, background is black.

Can the same VTK filter be used in 3D Slicer, or the program has builtin methods for that?

Code below gives an empty polydata:

```auto
    view = slicer.app.layoutManager().threeDWidget(0).threeDView()
    renderWindow = view.renderWindow()
    renderers = renderWindow.GetRenderers()
    renderer = renderers.GetItemAsObject(0)

    winToImageFilter = vtk.vtkWindowToImageFilter()
    winToImageFilter.SetInput(renderWindow)
    winToImageFilter.SetScale(2) #image quality
    winToImageFilter.Update()

    contFilter = vtk.vtkContourFilter()
    contFilter.SetInputConnection(winToImageFilter.GetOutputPort())
    contFilter.SetValue(0, 255)
    contFilter.Update()

    # Make the contour coincide with the data.
    contour = contFilter.GetOutput()

```

Data example

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/4/a42196f3a3a77da36a0a987958292a07a42a593e.png)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [August 8, 2022, 8:38am UTC](https://discourse.slicer.org/t/calculate-external-contour-for-segment-in-segmentation/24653/2 "2022-08-08T08:38:34Z")

</div>

I’m not aware of any function ready-made in Slicer for this. I think your approach is reasonable. On thought is that maybe you want to switch to orthogonal projection instead of the default perspective for this. Or maybe not, if you want the contour that is visible from a point in space.

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [August 8, 2022, 9:02am UTC](https://discourse.slicer.org/t/calculate-external-contour-for-segment-in-segmentation/24653/3 "2022-08-08T09:02:50Z")

</div>

I think you could apply a projection transform to your model using your camera. And you may need to use vtkFeatureEdges after that in some combination of options

This may be related:

> **[Bounding volume of cylinder path](https://discourse.vtk.org/t/bounding-volume-of-cylinder-path/8498/11)**
>
> Thanks Will. I see the impact of Flying edges. But VoxelModeller is the time killer, especially when I bump the dimensions.

Hope it helps

---

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [August 8, 2022, 9:34am UTC](https://discourse.slicer.org/t/calculate-external-contour-for-segment-in-segmentation/24653/4 "2022-08-08T09:34:37Z")

</div>

Thank both of you!  
I will try several approaches and let you know. I’ve found some mistakes in my script and now trying to fix them.

The VTK example works but orientation and scale of the contour model must be modified.
