# How to dilate a 3D label image while keeping its' original label values?

**URL:** <https://discourse.itk.org/t/how-to-dilate-a-3d-label-image-while-keeping-its-original-label-values/4550>\
**Category:** Algorithms\
**Tags:** simpleitk, python\
**Created:** [November 5, 2021, 12:20am UTC](https://discourse.itk.org/t/how-to-dilate-a-3d-label-image-while-keeping-its-original-label-values/4550 "2021-11-05T00:20:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andieku](https://discourse.itk.org/user_avatar/discourse.itk.org/andieku/32/2346_2.png) [@andieku](https://discourse.itk.org/u/andieku)\
**Post date:** [November 5, 2021, 12:20am UTC](https://discourse.itk.org/t/how-to-dilate-a-3d-label-image-while-keeping-its-original-label-values/4550/1 "2021-11-05T00:20:20Z")

</div>

Hi, I am trying to use SimpleITK in Python to dilate all non-zero regions in a 3D label image while keeping the image’s original label values.

I tried following this [Multi-label Morphology example](http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python_html/32_Watersheds_Segmentation.html#Multi-label-Morphology) but `sitk.MorphologicalWatershedFromMarkers()` function kept assigning new label values to each region even if I change all label values to fall into the range 0-255 (by using `sitk.ChangeLabel`) beforehand.

And this is what is written in [the function description](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1MorphologicalWatershedFromMarkersImageFilter.html):

> The labels of the output image are the label of the marker image.

So I am not too sure why the function’s assigning new label values to each region instead of keeping market image’s label values. Does anyone know how to fix this? And please correct me if my understanding of the function is wrong.

Also, is there a faster way to achieve my purpose (multi-label dilation that keeps original labels) without going through `BinaryDilate`, `MorphologicalWatershedFromMarkers`, etc?

Thank you for your help in advance!

---

<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:** [November 5, 2021, 1:33pm UTC](https://discourse.itk.org/t/how-to-dilate-a-3d-label-image-while-keeping-its-original-label-values/4550/2 "2021-11-05T13:33:58Z")

</div>

Hello @andieku,

Not sure why you are looking into the `MorphologicalWatershedFromMarkers` if what you want is dilation. For dilation the relevant filter is [BinaryDilateImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1BinaryDilateImageFilter.html). Please see code below:

```auto
import SimpleITK as sitk

# Did some sort of segmentation and obtained an image
# with two arbitrary labels
seg_image = sitk.Image([128]*3, sitk.sitkUInt8)
seg_image[20:30, 30:50, 40:70] = 2
seg_image[40:70, 30:50, 20:30] = 8

sitk.WriteImage(seg_image,'seg_image.nrrd')

# Get all label values, assume all non zero values are label
np_arr_view = sitk.GetArrayViewFromImage(seg_image)
unique_values = set(np_arr_view[np_arr_view!=0])

dilated_image = seg_image
dilate_filter = sitk.BinaryDilateImageFilter()
dilate_filter.SetKernelRadius(3)
dilate_filter.SetKernelType(sitk.sitkBall)
for label in unique_values:
    dilate_filter.SetForegroundValue(int(label))
    dilated_image = dilate_filter.Execute(dilated_image)

sitk.WriteImage(dilated_image,'dilated_seg_image.nrrd')

```

---

<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:** [November 5, 2021, 1:59pm UTC](https://discourse.itk.org/t/how-to-dilate-a-3d-label-image-while-keeping-its-original-label-values/4550/3 "2021-11-05T13:59:03Z")

</div>

Perhaps you are looking for the algorithms described  
in “[Parallel algorithms for erosion and dilation of label images](https://www.insight-journal.org/browse/publication/886).” [Beare R.](https://www.insight-journal.org/search/?search=authors:Beare), [Jackway P.](https://www.insight-journal.org/search/?search=authors:Jackway)

---

<div class="post-metadata">

**Author:** ![andieku](https://discourse.itk.org/user_avatar/discourse.itk.org/andieku/32/2346_2.png) [@andieku](https://discourse.itk.org/u/andieku)\
**Post date:** [November 5, 2021, 10:47pm UTC](https://discourse.itk.org/t/how-to-dilate-a-3d-label-image-while-keeping-its-original-label-values/4550/4 "2021-11-05T22:47:52Z")

</div>

@zivy thank you for the reply. I was looking into `MorphologicalWatershedFromMarkers` since I was hoping to do a simultaneous dilation of all labeled regions. I think I figured out how to keep label values from the market image. It wasn’t working before since I was passing a label map that had values outside of the range 0-255 to the `SimpleITK.ChangeLabels`.

---

<div class="post-metadata">

**Author:** ![andieku](https://discourse.itk.org/user_avatar/discourse.itk.org/andieku/32/2346_2.png) [@andieku](https://discourse.itk.org/u/andieku)\
**Post date:** [November 5, 2021, 10:51pm UTC](https://discourse.itk.org/t/how-to-dilate-a-3d-label-image-while-keeping-its-original-label-values/4550/5 "2021-11-05T22:51:30Z")

</div>

Thank you @blowekamp. I will also try using `LabelSetDilateImageFilter` and compare the runtime performance with the other approach (Multi-label Morphology).
