# Binary Morphological Filter with Padding

**URL:** https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921
**Category:** Algorithms
**Created:** [April 19, 2022, 1:35am UTC](https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921 "2022-04-19T01:35:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)
#### Post date: [April 19, 2022, 1:35am UTC](https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921/1 "2022-04-19T01:35:35Z")

</div>

I am using binary morphological image filter with large structuring element radius to find the convex hull between the lungs.

 ![lung1](https://discourse.itk.org/uploads/default/original/2X/8/84873e3bc4d835e13fb875abe50e9595f54f3c2c.png)

However I haven’t been able to build a satsfatory convex hull yet as some holes have remained in the processed image. If I increased the structuring element radius beyond a limit I would get segmentation fault.

So I tryed to add some padding, saving the original image in a larger image (10 times larger) with blank space in the borders and tried again to apply the filter with a larger radius.

Yet, the padding does not seem to work as I am only able to apply morphological closing with the same radius as I was using before. I get the error “Failed to allocate memory to an image”.

Am I doing something wrong?

Thanks in advance

---

<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 19, 2022, 1:12pm UTC](https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921/2 "2022-04-19T13:12:49Z")

</div>

Morphological operations with large radii are very slow. But I would not expect them to allocate a lot of memory. You can try the [approximation to that by use of signed distance field](https://github.com/KitwareMedical/HASI/blob/c75d6c45e573c143abe71dd836a639922347d3fb/include/itkSegmentBonesInMicroCTFilter.hxx#L67-L111). You will still need to pad the image to avoid image boundary effects.

---

<div class="post-metadata">

### Author: ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)
#### Post date: [April 28, 2022, 2:37pm UTC](https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921/3 "2022-04-28T14:37:42Z")

</div>

How do I use a signed distance field as an approximation to the convex hull ? 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: [April 28, 2022, 3:05pm UTC](https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921/4 "2022-04-28T15:05:19Z")

</div>

Hello @Oddone-Scatena,

I believe what you are trying to do is a dilation with a large structuring element. That does not create a [convex hull](https://en.wikipedia.org/wiki/Convex_hull). The usage of a distance map to mimic dilation is straightforward, see SinpleITK code below (it’s in 2D but the usage of the distance map is exactly the same for 2D and 3D):

```auto
import SimpleITK as sitk

binary_label_image = sitk.Image([256,256], sitk.sitkUInt8)
binary_label_image[20:50, 25:125] = 1
binary_label_image[70:95, 40:140] = 1

distance_threshold = 20
dilated_binary_label_image = sitk.SignedMaurerDistanceMap(binary_label_image, insideIsPositive=False, squaredDistance=False, useImageSpacing=True)<distance_threshold

```

---

<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 28, 2022, 4:49pm UTC](https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921/5 "2022-04-28T16:49:57Z")

</div>

> [@zivy](#):
>
> `sitk.Image([256,256], sitk.sitkUInt8)`

I assume this zero-initializes the image?

---

<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: [April 28, 2022, 5:09pm UTC](https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921/6 "2022-04-28T17:09:12Z")

</div>

Yes.

---

<div class="post-metadata">

### Author: ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)
#### Post date: [May 2, 2022, 5:31pm UTC](https://discourse.itk.org/t/binary-morphological-filter-with-padding/4921/7 "2022-05-02T17:31:52Z")

</div>

The signed distance field when used 2 times works well producing the convex hull between the lungs and is much more faster than the binary morphological closing filter.

Red is the convex hull and yellow the original lungs.

 ![itk](https://discourse.itk.org/uploads/default/original/2X/3/37b26898008594e815e84e2a9ed9e0bf438c1ad9.png)
