# Remove multiple labels in 1 bone and have only 1 label per bone

**URL:** https://discourse.itk.org/t/remove-multiple-labels-in-1-bone-and-have-only-1-label-per-bone/5186
**Category:** Uncategorized
**Tags:** python, mask, segmentation, simpleitk
**Created:** [July 15, 2022, 11:53am UTC](https://discourse.itk.org/t/remove-multiple-labels-in-1-bone-and-have-only-1-label-per-bone/5186 "2022-07-15T11:53:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Vilma\_Bozgo](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/c6cbf5/32.png) [@Vilma\_Bozgo](https://discourse.itk.org/u/Vilma_Bozgo)
#### Post date: [July 15, 2022, 11:53am UTC](https://discourse.itk.org/t/remove-multiple-labels-in-1-bone-and-have-only-1-label-per-bone/5186/1 "2022-07-15T11:53:33Z")

</div>

Hello! I have a multilabel skeleton mask for MRI images of the skeleton. But for the same bone I sometimes get different labels, like in the image attached where the first bone gets a red label and a green label. But in fact I need one label for each bone. Is there any way or sitk function to fix this?  
I am working with sitk and itk-snap. Thanks in advance!

 ![multi_L](https://discourse.itk.org/uploads/default/original/2X/5/5b054e972c90b5475de436085722051b29a55b06.png)

---

<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: [July 15, 2022, 12:11pm UTC](https://discourse.itk.org/t/remove-multiple-labels-in-1-bone-and-have-only-1-label-per-bone/5186/2 "2022-07-15T12:11:21Z")

</div>

Using [3D Slicer’s segment editor’s logical operators](https://slicer.readthedocs.io/en/latest/user_guide/modules/segmenteditor.html#logical-operators) it should be easy to merge green label into the red one.

Using ITK-SNAP there is a workaround: select red as the active label, for “paint over” choose the green label. Then paintbrush, enable 3D, set the radius to maximum, then paint over the green label.

---

<div class="post-metadata">

### Author: ![Vilma\_Bozgo](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/c6cbf5/32.png) [@Vilma\_Bozgo](https://discourse.itk.org/u/Vilma_Bozgo)
#### Post date: [July 15, 2022, 12:26pm UTC](https://discourse.itk.org/t/remove-multiple-labels-in-1-bone-and-have-only-1-label-per-bone/5186/3 "2022-07-15T12:26:15Z")

</div>

Thank you for your fast reply!  
Apart from this solution, is there any sitk function or solution I could use in python?

---

<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: [July 15, 2022, 12:27pm UTC](https://discourse.itk.org/t/remove-multiple-labels-in-1-bone-and-have-only-1-label-per-bone/5186/4 "2022-07-15T12:27:42Z")

</div>

Automating this probably isn’t trivial.

---

<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: [July 15, 2022, 1:19pm UTC](https://discourse.itk.org/t/remove-multiple-labels-in-1-bone-and-have-only-1-label-per-bone/5186/5 "2022-07-15T13:19:37Z")

</div>

Hello @Vilma_Bozgo,

Assuming the labels you want to combine form a single connected component, then the solution is trivial (`ConnectedComponentImageFilter`), see code below. Also, based on the image you provided you do not want to simply replace existing label values as there are “green” labeled pixels on both the upper and lower vertebra and those need to be mapped differently.

```auto
import SimpleITK as sitk

# Label image with three labels, mimicking the setup above 
label_image = sitk.Image([128,128], sitk.sitkUInt8)
label_image[40:50, 40:50] = 50
label_image[50:60, 40:50] = 100
label_image[40:50, 80:90] = 200
label_image[50:60, 80:90] = 100
sitk.Show(label_image, 'original labels')

#relabel using connected components
relabeled_image = sitk.ConnectedComponent(label_image)
sitk.Show(relabeled_image, 'new labels')

```

---

<div class="post-metadata">

### Author: ![Vilma\_Bozgo](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/c6cbf5/32.png) [@Vilma\_Bozgo](https://discourse.itk.org/u/Vilma_Bozgo)
#### Post date: [July 15, 2022, 2:14pm UTC](https://discourse.itk.org/t/remove-multiple-labels-in-1-bone-and-have-only-1-label-per-bone/5186/6 "2022-07-15T14:14:29Z")

</div>

Okey, thank you!
