# \`CreateClosedSurfaceRepresentation()\` does not work when closing a segmentation surfaces

**URL:** <https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [August 3, 2022, 1:43pm UTC](https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617 "2022-08-03T13:43:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [August 3, 2022, 1:43pm UTC](https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617/1 "2022-08-03T13:43:16Z")

</div>

I want to convert a model to a segmentation and close the surface, but using `CreateClosedSurfaceRepresentation()` doesn’t work, what’s wrong?

```python
seg = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
seg.SetReferenceImageGeometryParameterFromVolumeNode(Vol)
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(model, seg)
seg.CreateClosedSurfaceRepresentation()

```

([the MRBfile link](https://drive.google.com/file/d/1mAKBc3qNRiLrMZF_S4_T8gWI6hMlAbZS/view?usp=sharing)…)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/5/152dc10857efdeeec2b48d6a051bc28b42ce9e22.jpeg)

However, I can achieve the goal by pressing the 'make master` button in the module of segmentation…

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/4/c48d7a609ca0a176429cb2f3dfa4d1b7074da90e.jpeg)

---

<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:** [August 3, 2022, 3:04pm UTC](https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617/2 "2022-08-03T15:04:45Z")

</div>

If the original representation for the segments will be model nodes, then you can set the master representation to closed surface.

```auto
seg = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
seg.SetReferenceImageGeometryParameterFromVolumeNode(Vol)
seg.SetMasterRepresentationToClosedSurface()
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(model, seg)

```

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [August 3, 2022, 7:33pm UTC](https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617/3 "2022-08-03T19:33:04Z")

</div>

> [@Sunderlandkyl](#):
>
> `seg.SetMasterRepresentationToClosedSurface()`

Thank @Sunderlandkyl !  
I used `seg.SetMasterRepresentationToClosedSurface()` to get the following seg. Why is the dyeing area of green slices different from that of other slices?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/f/5f3a28381395036ae4198436082ead673bb7df1a.png)

---

<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:** [August 3, 2022, 7:38pm UTC](https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617/4 "2022-08-03T19:38:02Z")

</div>

Probably the slicing of the surface along the coronal plane isn’t able to correctly distinguish between the inside/outside of the model.

It looks like your surface mesh is not actually a closed surface. Displaying the segmentation in 2D and converting it to a labelmap representation is likely going to have unexpected results since the algorithms cannot determine what is “inside” vs what is “outside”,

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [August 3, 2022, 7:41pm UTC](https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617/5 "2022-08-03T19:41:19Z")

</div>

@Sunderlandkyl How to close the surface?Or tell the algorithms I want “inside” instead of “outside”???

---

<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:** [August 3, 2022, 7:44pm UTC](https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617/6 "2022-08-03T19:44:11Z")

</div>

I’m not sure if there is a easy solution to the problem of converting an open surface to a closed one.

@lassoan do you know of an approach that might work?

---

<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:** [August 4, 2022, 6:46am UTC](https://discourse.slicer.org/t/createclosedsurfacerepresentation-does-not-work-when-closing-a-segmentation-surfaces/24617/7 "2022-08-04T06:46:41Z")

</div>

> [@Sunderlandkyl](#):
>
> I’m not sure if there is a easy solution to the problem of converting an open surface to a closed one.

It is impossible to solve this problem automatically. It is better to generate a closed surface when the surface is created (e.g., enable capping when cutting the surface).
