Convert binary label map into 2D contours (ideally splines) and back

From @dzenanz:

Background: we are writing custom application with web UI for segmentation of 2D+time data. The semi-automatic algorithm relying on grow from seeds works well, but not all the time. So manual corrections are needed. Ideally, convert the label map into a 2D spline for each time point, to allow easy interaction. The structure of interest is smooth in these images, so smoothing of splines is a welcome side-effect. Converting that into a fractional label map is preferred over binary label map.

Segmentation module’s documentation mentions these conversions. It was already answered that direct editing of contours is not possible with Slicer. Is this still true? How hard would that be with an API, if one exists? Reuse is preferred over reimplementation.

Is conversion from binary label map into planar contours best done by converting to 3D mesh first, then cutting the mesh? Is that already implemented in Slicer, and where?

Is there an easy or already implemented way of converting the resulting 2D polygon into a spline? Pointers into relevant code are appreciated, as vtkLinearSpline.cxx does not contain a lot of code.

The last question about this was two years ago. Did anything important change? Can someone point me to relevant pieces of code for ripping it out of Slicer and into a stand-alone executable?

From @Sam_Horvath:

I don’t think anything has changed since the previous postings regarding planar contours.

Conversions between different representations are managed by conversion rules, which would be the best place to look for code to reuse:

Slicer: https://github.com/Slicer/Slicer/tree/master/Libs/vtkSegmentationCore

SlicerRT: https://github.com/SlicerRt/SlicerRT/tree/master/DicomRtImportExport/ConversionRules

1 Like

@dzenanz Similarly to what we did for vtkAddons, we also talked about moving vtkSegmentationCore into its own repository.

See https://github.com/Slicer/Slicer/issues/4792

Let me know if expediting this would be helpful for your project.

Storing and managing segmentation data in a series of 2D contours is a legacy method, and so far we only needed it for DICOM-RT Structure Set, and did not want to encourage its use (especially that there is the DICOM Segmentation Object, which is much superior to RTSS). This is why we did not even implement a conversion rule going from labelmap to planar contour, but the code that does the conversion is in the exporter function itself:

If you want it could be outsourced though. Any thoughts @lassoan @Sunderlandkyl?

With the recent addition of the new Markup type of closed curve this would be much easier to do than before. The only thing we’d need is a converter from/to planar contour segmentations (and the abovementioned labelmap → contours conversion, since the other way is implemented already).

1 Like

I agree that current markups curves should be able to handle editing quite well.

We just need an algorithm for getting closed curves from segmentation/slice intersections. It would require two steps:

  • segmentation slicing (cut segmentation with slice): The implementation in the segmentation 2D displayable manager and in the exporter (that @cpinter linked above) are about the same and should be usable for simple shapes. For complex cases you would need to use vtkContourTriangulator class but we could not switch to it because it has a small but serious bug that may cause the application hang for invalid inputs (see details here: https://github.com/Slicer/SlicerGitSVNArchive/pull/1075).
  • spline fitting: we could create a spline by resampling the linearly interpolated contour at uniform distances and use these as control points

Thank you all for responses!