# Fill small holes in the segmentation label image

**URL:** <https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575>\
**Category:** Beginner Questions\
**Tags:** simpleitk\
**Created:** [April 15, 2024, 3:55pm UTC](https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575 "2024-04-15T15:55:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![G\_Y](https://discourse.itk.org/user_avatar/discourse.itk.org/g_y/32/3996_2.png) [@G\_Y](https://discourse.itk.org/u/G_Y)\
**Post date:** [April 15, 2024, 3:55pm UTC](https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575/1 "2024-04-15T15:55:32Z")

</div>

Hi all, I have done a `Sitk.MorphologicalWatershed` to my image and got a label image, I find there are some small segments of one or two pixels, or like this little “cross”:  
 ![image](https://discourse.itk.org/uploads/default/original/2X/c/c45feed16689ff2514f91eea7fce5d3cbf8320a0.png)

What would be the best filter/way to fill these small holes, I tried `Sitk.BinaryFillhole` but it doesn’t do much, any other API shall I try? Thanks!

---

<div class="post-metadata">

**Author:** ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)\
**Post date:** [April 15, 2024, 4:36pm UTC](https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575/2 "2024-04-15T16:36:32Z")

</div>

That fill hole filter ought to work. Perhaps try SimpleITK’s MedianImageFilter?

[https://simpleitk.org/doxygen/latest/html/classitk\_1\_1simple\_1\_1MedianImageFilter.html](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1MedianImageFilter.html)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [April 15, 2024, 4:45pm UTC](https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575/3 "2024-04-15T16:45:34Z")

</div>

Frequently people look at 2D slices of 3D images, and “identify” holes. The little cross is a morphological hole if the image is 2D, but might be beginning (first slice) of a some 3D pit/cavity.

Take a look at this [page](https://www.quantamagazine.org/topology-101-how-mathematicians-study-holes-20210126/). A slice through the middle of the torus would show a big “hole”, which in 3D is just the edge of the torus, and not a hole at all.

---

<div class="post-metadata">

**Author:** ![G\_Y](https://discourse.itk.org/user_avatar/discourse.itk.org/g_y/32/3996_2.png) [@G\_Y](https://discourse.itk.org/u/G_Y)\
**Post date:** [April 15, 2024, 4:56pm UTC](https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575/4 "2024-04-15T16:56:47Z")

</div>

Thanks @dchen ! `MedianImageFilter` seems working for me. Do all the `Binary*` API assume the input image has one background value and one foreground value (hence binary), so they won’t work well with label image which usually has more values?

---

<div class="post-metadata">

**Author:** ![G\_Y](https://discourse.itk.org/user_avatar/discourse.itk.org/g_y/32/3996_2.png) [@G\_Y](https://discourse.itk.org/u/G_Y)\
**Post date:** [April 15, 2024, 4:58pm UTC](https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575/5 "2024-04-15T16:58:11Z")

</div>

Thanks @dzenanz , currently I am working only with single 2D slices, but you have a good point and I will remember it when I start working with 3D images!

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [April 15, 2024, 5:07pm UTC](https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575/6 "2024-04-15T17:07:12Z")

</div>

> [@G\_Y](#):
>
> Do all the `Binary*` API assume the input image has one background value and one foreground value (hence binary), so they won’t work well with label image which usually has more values?

I am not sure what segmentation artifact you are trying to address. You may want to look into [LabelVoting](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1LabelVotingImageFilter.html), or “LabelSet” morphology ( which is not enabled by default in SimpleITK ), or perhaps grayscale morphology operations such as closing or opening would work.

---

<div class="post-metadata">

**Author:** ![cookpa](https://discourse.itk.org/user_avatar/discourse.itk.org/cookpa/32/255_2.png) [@cookpa](https://discourse.itk.org/u/cookpa)\
**Post date:** [April 16, 2024, 2:16pm UTC](https://discourse.itk.org/t/fill-small-holes-in-the-segmentation-label-image/6575/7 "2024-04-16T14:16:01Z")

</div>

With multi-label images it gets more complicated to fill in holes with just a binary operator. You can set the foreground value, if you’re sure that the holes can never overlap.

If your labels can be nested, then a hole in label A might also be a hole in label B, in which case I would use something like the signed distance transform ([SimpleITK: itk::simple::SignedMaurerDistanceMapImageFilter Class Reference](https://simpleitk.org/doxygen/v2_2/html/classitk_1_1simple_1_1SignedMaurerDistanceMapImageFilter.html)) to decide which label should fill a particular hole.
