# Planar contour to binary labelmap or model

**URL:** <https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828>\
**Category:** Support\
**Created:** [December 1, 2020, 3:54pm UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828 "2020-12-01T15:54:56Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 1, 2020, 3:54pm UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/1 "2020-12-01T15:54:56Z")

</div>

Hi,

I have planar contours that turn into this when I select “Export visible segments to models”:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/3/93f5ace8e6a85e4d3c8e2b55560c66dbfc9ad886.png)  
i.e. the contours are not connected.  
I think this is because the distance between planes is too large as a result of having transformed all contour points with an affine transformation earlier in the process.  
Is there any way to enforce the contours to be connected to get a single closed surface?

Many thanks!

---

<div class="post-metadata">

**Author:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [December 1, 2020, 8:50pm UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/2 "2020-12-01T20:50:26Z")

</div>

What version of Slicer is this?  
Can you send me a message with the saved scene?

---

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 2, 2020, 11:23am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/3 "2020-12-02T11:23:17Z")

</div>

Hi, thank you for looking at this.

The slicer version is: 4.11.20200930 r29402 / 002be18

Here a link to the [scene](https://drive.google.com/file/d/1G122xf0k4r9ZEMvcTDVlXP9jTrQkgYCt/view?usp=sharing).

The scene contains the unregistered planar contour (SegmentationFromContourPoints\_refgeomT1) and the model created from it, which works fine.  
It also contains the planar contour from the registered contour points (SegmentationFromContourPoints\_refgeomT2) and the model created from it, which leads to the separated planes model shown in the first post.

The registration had almost no visual effect on the planar contours, i.e. the registered planes are barely distinguishable from the unregistered ones by looking at the 3D view. However when creating the models, it works in the unregistered case, but not in the registered one.

Thanks!

---

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 3, 2020, 10:45am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/4 "2020-12-03T10:45:56Z")

</div>

Hi,

I could narrow down the issue a little bit by modifying the contour point coordinates. Scaling all contour point coordinates (x,y,z) with any number between 0.1 and 10 did not lead to a closed surface. However, scaling only the z-coordinate of all contour points by 0.9 resulted in a closed surface. So the problem doesn’t seem to be related only to the absolute distance between the contour planes.

Another modification of contour points that led to a closed surface was to set all z-values of a plane to a constant value (I chose the z-value of the first contour point of each plane). Is it a requirement for all z-values of a contour line to be the same for the conversion to a model to work?

I also noticed that when the conversion to a model results in several planes (as shown in the first post) the conversion to a binary labelmap does not work either, resulting in a map without any foreground pixels.

---

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 8, 2020, 10:51am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/5 "2020-12-08T10:51:50Z")

</div>

Sorry, I am still struggling to understand why the conversion to a model doesn’t work in the second case. If anyone could explain to me what I’m doing wrong, that would be great.

---

<div class="post-metadata">

**Author:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [December 8, 2020, 9:34pm UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/6 "2020-12-08T21:34:53Z")

</div>

I don’t think you’re doing anything wrong, there is an issue with the conversion algorithm. I will take a look tomorrow and see if I can fix the problem.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 9, 2020, 3:59am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/7 "2020-12-09T03:59:53Z")

</div>

> [@Aaron](#):
>
> Is it a requirement for all z-values of a contour line to be the same for the conversion to a model to work?

Planar contour representation (as the name suggests) assumes that each contour are exactly planar. If this assumption is violated then anything can happen (I don’t remember if we check or enforce this and what problems can it cause exactly).

If you want to apply transform to a segmentation then change its master representation to closed surface or binary labelmap.

---

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 9, 2020, 10:44am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/8 "2020-12-09T10:44:33Z")

</div>

Thank you, that clarifies things for me.

I am wondering, though, why this is a requirement? The transformed contours are still planar in the sense that all contour points of a contour line are exactly in one plane and all planes are parallel to each other. This is, because the transformation is affine. However, the contour planes are not parallel to the planes of the reference geometry.

Regarding the checks and enforcement, I don’t think there are any, because I used the same pipeline (transform + conversion to binary label map) on more than 200 cases and it seemed to have worked great in all but 2.

> [@lassoan](#):
>
> If you want to apply transform to a segmentation then change its master representation to closed surface or binary labelmap.

Is there anything I can do if my starting point are the registered contours? For some cases I don’t have access to the not transformed contours?

---

<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:** [December 9, 2020, 11:12am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/9 "2020-12-09T11:12:19Z")

</div>

> [@lassoan](#):
>
> I don’t remember if we check or enforce this and what problems can it cause exactly

I looked at the code and it seems that only consistent spacing is checked, [but not enforced](https://github.com/SlicerRt/SlicerRT/blob/master/DicomRtImportExport/ConversionRules/vtkPlanarContourToClosedSurfaceConversionRule.cxx#L1244). I did not find any trace for requiring all planes to be parallel in the closed surface conversion, [only in the ribbon one](https://github.com/SlicerRt/SlicerRT/blob/master/DicomRtImportExport/ConversionRules/vtkPlanarContourToRibbonModelConversionRule.cxx#L351).

---

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 9, 2020, 11:49am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/10 "2020-12-09T11:49:19Z")

</div>

Thanks for checking the code.

After the affine transform the spacing should still be consistent and all planes should still be parallel to each other. So these checks/warnings shouldn’t be triggered in this case (and I didn’t notice any warnings during conversion). Did I understand correctly that for this case a check for parallelism between the contour line planes and the reference geometry z-planes would be required?

---

<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:** [December 9, 2020, 11:52am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/11 "2020-12-09T11:52:58Z")

</div>

The reference volume does not take part in the planar contours to closed surface conversion, so there are no such requirements there.

---

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 9, 2020, 11:55am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/12 "2020-12-09T11:55:29Z")

</div>

In this case, why does applying an affine transform to all contour points cause a problem?

---

<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:** [December 9, 2020, 1:30pm UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/13 "2020-12-09T13:30:40Z")

</div>

I’m not sure, but I think if you attach a full log from a session when the conversion fails, then @Sunderlandkyl who is most familiar with this particular converter could hopefully figure out the root cause of the problem.

---

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 9, 2020, 1:52pm UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/14 "2020-12-09T13:52:17Z")

</div>

Ok, thank you very much.

@Sunderlandkyl: Thanks for looking at this. The conversion failure can be reproduced from the [scene](https://drive.google.com/file/d/1G122xf0k4r9ZEMvcTDVlXP9jTrQkgYCt/view?usp=sharing) I linked. I right-click on the segmentation of the SegmentationFromContourPoints\_refgeomT2 node and select “Export visible segments to models”. This leads to the failed model from the first post. Is this sufficient? If not, I’ll send the session log.

---

<div class="post-metadata">

**Author:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [December 9, 2020, 9:33pm UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/15 "2020-12-09T21:33:06Z")

</div>

Contour planes are transformed so that their normals align with the z-axis before triangulation, but there was a problem with the normal calculation.

I’ve pushed a fix: [https://github.com/SlicerRt/SlicerRT/commit/4abe727357899cc8f854a524307b9238e939ff9e](https://github.com/SlicerRt/SlicerRT/commit/4abe727357899cc8f854a524307b9238e939ff9e)

It should be included in the build tomorrow.

---

<div class="post-metadata">

**Author:** ![Aaron](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron](https://discourse.slicer.org/u/Aaron)\
**Post date:** [December 10, 2020, 9:06am UTC](https://discourse.slicer.org/t/planar-contour-to-binary-labelmap-or-model/14828/16 "2020-12-10T09:06:56Z")

</div>

Thanks a lot for fixing this!
