# BinaryErodeImageFilter erodes the entire foreground

**URL:** https://discourse.itk.org/t/binaryerodeimagefilter-erodes-the-entire-foreground/4518
**Category:** Algorithms
**Tags:** filtering, simpleitk
**Created:** [October 21, 2021, 4:38pm UTC](https://discourse.itk.org/t/binaryerodeimagefilter-erodes-the-entire-foreground/4518 "2021-10-21T16:38:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kliron](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/c6cbf5/32.png) [@kliron](https://discourse.itk.org/u/kliron)
#### Post date: [October 21, 2021, 4:38pm UTC](https://discourse.itk.org/t/binaryerodeimagefilter-erodes-the-entire-foreground/4518/1 "2021-10-21T16:38:16Z")

</div>

I have a 3D labelmap image with the values 1 for foreground and 0 for background. I want to erode 1-2 voxels from the edge of the foreground. This is my code:

```auto
f = sitk.BinaryErodeImageFilter()
f.SetKernelType(sitk.sitkBall)
f.SetKernelRadius(1)
f.SetBackgroundValue(0)
f.SetForegroundValue(1)
new_img = f.Execute(img)

```

`new_img` is all zeros (while `img` has 496 voxels labeled as 1). What am I doing wrong? I have tried with kernels other than `sitkBall` with similar results.

---

<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: [October 21, 2021, 6:09pm UTC](https://discourse.itk.org/t/binaryerodeimagefilter-erodes-the-entire-foreground/4518/2 "2021-10-21T18:09:03Z")

</div>

Hello @kliron,

Code looks good. If you can provided a Minimal Working Example (MWE) illustrating the issue that would help.

Below is a MWE, but it works as expected:

```auto
import SimpleITK as sitk

img = sitk.Image([64]*3, sitk.sitkUInt8)
img[10:20,10:20,10:20] = 1

f = sitk.BinaryErodeImageFilter()
f.SetKernelType(sitk.sitkBall)
f.SetKernelRadius(1)
f.SetBackgroundValue(0)
f.SetForegroundValue(1)
new_img = f.Execute(img)

#multiply images by 255 so that the foreground is visible
#in Fiji without modifying window/level
sitk.Show(img*255, 'orig')
sitk.Show(new_img*255,'erroded')

```

---

<div class="post-metadata">

### Author: ![kliron](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/c6cbf5/32.png) [@kliron](https://discourse.itk.org/u/kliron)
#### Post date: [October 21, 2021, 6:46pm UTC](https://discourse.itk.org/t/binaryerodeimagefilter-erodes-the-entire-foreground/4518/3 "2021-10-21T18:46:33Z")

</div>

Hi @zivy  
Here is an example [example.nrrd](https://discourse.itk.org/uploads/short-url/i14No4QrJmzhEXbzpHxUUWAUMTt.nrrd) (1.3 MB)  
label map image.

The image has 496 voxels with the value 1.

If I run the erosion:

```auto
f = sitk.BinaryErodeImageFilter()
f.SetKernelType(sitk.sitkBall)
f.SetKernelRadius(1)
f.SetBackgroundValue(0)
f.SetForegroundValue(1)
eroded_img = f.Execute(img)

```

Then all voxels in eroded\_img have the value 0.

---

<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: [October 21, 2021, 8:09pm UTC](https://discourse.itk.org/t/binaryerodeimagefilter-erodes-the-entire-foreground/4518/4 "2021-10-21T20:09:01Z")

</div>

Hello @kliron,

Thanks for sharing the data.

It looks like it is an “issue/feature” with the segmentation, it is just 2 voxels wide in all locations and the erosion gets rid of everything. Possibly try thinning, [BinaryThinningImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1BinaryThinningImageFilter.html), the result is a skeleton (1 voxel wide representation).

---

<div class="post-metadata">

### Author: ![kliron](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/c6cbf5/32.png) [@kliron](https://discourse.itk.org/u/kliron)
#### Post date: [October 22, 2021, 4:15am UTC](https://discourse.itk.org/t/binaryerodeimagefilter-erodes-the-entire-foreground/4518/5 "2021-10-22T04:15:26Z")

</div>

Thank you @zivy  
I suspected it should be an issue with the images themselves as I got normal results with other images and the same code. I think I will have to use higher resolution images to segment the structure of interest and then resample on the lower resolution ones or figure a way to avoid the erosion altogether. Or maybe go with BinaryThinningImageFilter as you suggest. Thanks again!
