# Fill “holes” in an image (T1-weighted)

**URL:** <https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612>\
**Category:** Support\
**Created:** [April 17, 2018, 5:10pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612 "2018-04-17T17:10:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [April 17, 2018, 5:10pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/1 "2018-04-17T17:10:10Z")

</div>

Hi all!  
I would like to fill holes of a binary input 3D image (cf. image and the code on it).  
It’s possible with Scipy ([link](https://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.ndimage.morphology.binary_fill_holes.html)), but Scipy is not installed in slicer.

How could I do that?

Thanks in advance.!  
[Slicer\_holes|690x310](https://discourse.slicer.org/uploads/short-url/sxD0d6EDuni3kFocpJZFIZ2V8to.png)

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [April 17, 2018, 6:46pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/2 "2018-04-17T18:46:31Z")

</div>

Dd you try the SimpleFilters module? There are a lot of hole filling options.

[https://itk.org/SimpleITKDoxygen/html/classitk\_1\_1simple\_1\_1BinaryFillholeImageFilter.html](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1BinaryFillholeImageFilter.html)

[https://itk.org/SimpleITKDoxygen/html/classitk\_1\_1simple\_1\_1VotingBinaryIterativeHoleFillingImageFilter.html](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1VotingBinaryIterativeHoleFillingImageFilter.html)

[https://itk.org/SimpleITKDoxygen/html/classitk\_1\_1simple\_1\_1GrayscaleFillholeImageFilter.html](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1GrayscaleFillholeImageFilter.html)

---

<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:** [April 17, 2018, 9:28pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/3 "2018-04-17T21:28:17Z")

</div>

Hole filling using morphological operator is also available directly in Segment editor (in Smoothing effect).

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [April 18, 2018, 5:31pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/4 "2018-04-18T17:31:25Z")

</div>

Perfect. Thanks @pieper and @lassoan  
For the next, the script is:

> ```
> segmentEditorWidget.setActiveEffectByName("Smoothing")
> effect = segmentEditorWidget.activeEffect()
> effect.setParameter("SmoothingMethod", "MORPHOLOGICAL_CLOSING")
> effect.setParameter("KernelSizeMm", 20)
> 
> ```

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [May 16, 2018, 4:15pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/5 "2018-05-16T16:15:12Z")

</div>

I allow myself to come back to your reply @lassoan @pieper .  
This answer works but take a long time, is there a quicker way to fill the holes of a 3d model (or remove “the back-facing element”)?  
Thanks in advance.

---

<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:** [May 16, 2018, 5:35pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/6 "2018-05-16T17:35:53Z")

</div>

Kernel size of 20mm is extremely large! It is normal that it takes very long time to compute.

To remove internal holes from a segment, you may apply smoothing, then invert the segment, keep largest island, invert it back to get rid of most internal structures. If there are structures that are connected to the outside air (e.g., airways) then you can manually seal them off, to make them internal (disconnected from outside).

To extract skin, you should be able to use Flood fill effect (in SegmentEditorExtraEffects extension). Click in the air to create a segment (you can adjust neighborhood size to prevent leaking inside the body through small holes, like airways) and then invert the segment to get the skin surface.

I’ve created a repository where I’ll upload segmentation recipes and added detailed description of skin segmentation: [https://lassoan.github.io/SlicerSegmentationRecipes/SkinSurface/](https://lassoan.github.io/SlicerSegmentationRecipes/SkinSurface/)

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [May 17, 2018, 8:08am UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/7 "2018-05-17T08:08:29Z")

</div>

Nice process and example Andras, thanks.  
However, I use your [previous script](https://gist.github.com/lassoan/1673b25d8e7913cbc245b4f09ed853f9) routinely and I do not want to make it manually (scissors, …).  
Best.

---

<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:** [May 17, 2018, 5:54pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/8 "2018-05-17T17:54:00Z")

</div>

If you find that Flood fill works better than thresholding, then you can use that from script, too. Scissors, smoothing, etc. are all optional post-processing that may improve quality of thresholding or flood-filling results.

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [May 18, 2018, 1:05pm UTC](https://discourse.slicer.org/t/fill-holes-in-an-image-t1-weighted/2612/9 "2018-05-18T13:05:58Z")

</div>

Ok, I will try the two options.  
thanks Andras.
