Calculate external contour for segment in segmentation

I’m trying to calculate external contour and then transform it into markups curve node.Here is a VTK example 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:

    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

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.

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:

Hope it helps

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.