# Create a mask filling holes of the image

**URL:** <https://discourse.itk.org/t/create-a-mask-filling-holes-of-the-image/4116>\
**Category:** Algorithms\
**Created:** [May 11, 2021, 8:01am UTC](https://discourse.itk.org/t/create-a-mask-filling-holes-of-the-image/4116 "2021-05-11T08:01:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zandarina](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.itk.org/u/zandarina)\
**Post date:** [May 11, 2021, 8:01am UTC](https://discourse.itk.org/t/create-a-mask-filling-holes-of-the-image/4116/1 "2021-05-11T08:01:35Z")

</div>

Hello,

I have a question, I want to remove the table of dicom files. For that, I do an “open” to remove the table and an “close” to fill the image as a mask. The problem comes with “close” to fill in the image with a big kernel, the program becomes very slow and it sometimes crashes. But If I remove this step some structures are missing with the erosion of the “open”. So I need a way to make the mask filled.

Is there any filter that can help me with that?. I have tried to make subregions but as one side of the table is very close to the body some areas cannot be discarded if I do that. So I need to do the “open” to the whole image.

The image is a 3d dicom.

 ![add a mask](https://discourse.itk.org/uploads/default/original/2X/5/5b2d8fadc57928808294241b7a61557539261869.png)

![mask](https://discourse.itk.org/uploads/default/original/2X/5/573cecc4b6759f42861f1a24de8f58c39d116613.png)

What I would like to obtain is this mask of the whole image but it is not feasible to make morphology for that.

Thanks

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [May 11, 2021, 1:01pm UTC](https://discourse.itk.org/t/create-a-mask-filling-holes-of-the-image/4116/2 "2021-05-11T13:01:24Z")

</div>

Hello @zandarina,

You can mimic the opening and closing binary morphological operators using a signed distance map, `SignedMaurerDistanceMapImageFilter`. This is likely to be faster.

With t\>0:  
closing (dilation followed by erosion):  
`signed_distance_map(signed_distance_map(mask)<t)<-t `

opening (erosion followed by dilation):  
`signed_distance_map(signed_distance_map(mask)<-t)<t`

As you are interested in removing the bed from CT, not necessarily in binary morphology, consider using the Slicer module that does this, see [YouTube video tutorial](https://www.youtube.com/watch?v=hys1fmXmLVg).

---

<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:** [May 11, 2021, 1:34pm UTC](https://discourse.itk.org/t/create-a-mask-filling-holes-of-the-image/4116/3 "2021-05-11T13:34:58Z")

</div>

You could pick up a C++ implementation of Ziv’s idea from here:

> <https://github.com/KitwareMedical/HASI/blob/d7c0f09dcc8ea2d5b273f8b0d94ede91ee3dfc8c/examples/segmentBonesInMicroCT.cxx#L122-L157>

---

<div class="post-metadata">

**Author:** ![zandarina](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.itk.org/u/zandarina)\
**Post date:** [May 11, 2021, 2:22pm UTC](https://discourse.itk.org/t/create-a-mask-filling-holes-of-the-image/4116/4 "2021-05-11T14:22:03Z")

</div>

Thank you very much. I will replace the morphology operations with the distance map. Thanks !!!
