Generating Closed Contours with `vtkImageMarchingSquares`

Hi everyone,

I’m currently working with the vtkImageMarchingSquares filter to generate contours from an image, similar to what the find_contours function does in Scikit Image. However, I’m facing an issue where the resulting contour is not closed. I have an image, and after running the marching squares algorithm, I obtain line segments, but I expected a closed contour. The input image looks like this:


And the output from vtkImageMarchingSquares looks like this:


My question is, how can I ensure that I obtain a closed contour from the vtkImageMarchingSquares filter? What should be the correct way to order the points so that I get a closed surface where values above the iso-level are inside the contour?

Any help or guidance on this would be greatly appreciated.

Thanks in advance!

This is partly an issue with how things are named in VTK.

In VTK, a closed contour just means that there are no holes in the contour. As far as most VTK filters are concerned, the order in which the line segments are listed is unimportant. And the fact that it is unimportant is actually quite useful, because it often makes it easier to write efficient multithreaded algorithms that operate on the data.

For situations where the ordering is important, VTK usually calls it a “loop” or a “ContourLoop”. There are two filters in VTK C++ that are commonly used to sort a “contour” to make a “loop”. These filters are vtkContourLoopExtraction and vtkStripper. But I don’t think either of these filters is available in vtk.js.

Thanks a lot for the reply David! I ended up using vtkClosedPolyLineToSurfaceFilter afterwards which seems to filter the non-closed contours.