# MultiLabelSTAPLEImageFilter does not work

**URL:** <https://discourse.itk.org/t/multilabelstapleimagefilter-does-not-work/4003>\
**Category:** Beginner Questions\
**Created:** [April 2, 2021, 12:50pm UTC](https://discourse.itk.org/t/multilabelstapleimagefilter-does-not-work/4003 "2021-04-02T12:50:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fleur](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Fleur](https://discourse.itk.org/u/Fleur)\
**Post date:** [April 2, 2021, 12:50pm UTC](https://discourse.itk.org/t/multilabelstapleimagefilter-does-not-work/4003/1 "2021-04-02T12:50:11Z")

</div>

Dear all,  
I’m trying to use MultiLabelSTAPLEImageFilter to combine several segmentations.  
However, when I run the code, nothing happens. The code seems to be running for hours, and calling ctrl + C to stop it does not do anything. Is MultiLabelSTAPLE supposed to take that long? Are my files too big? (they are images of size 105 x 100 x 120)  
I’ve also tried LabelVoting, which works perfectly.

Here is my code:

> segmentations = [sitk.ReadImage(file\_name, sitk.sitkUInt8, imageIO=“NiftiImageIO”)   
> for file\_name in path\_to\_segmentations]  
> filter = sitk.MultiLabelSTAPLEImageFilter()  
> filter.SetLabelForUndecidedPixels(255)  
> reference\_segmentation\_STAPLE = filter.Execute(segmentations)  
> sitk.WriteImage(reference\_segmentation\_STAPLE)

Thanks 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:** [April 2, 2021, 6:50pm UTC](https://discourse.itk.org/t/multilabelstapleimagefilter-does-not-work/4003/2 "2021-04-02T18:50:48Z")

</div>

Hello @Fleur,

No, this shouldn’t go on for ever, please see the artificial example below. As you are using the default filter settings, it would make sense to try and set appropriate values for the `terminationUpdateThreshold` and `maximumNumberOfIterations`.

```auto
import SimpleITK as sitk

# Artificial two label segmentation
image_size = [105,100,120]
box_size = [20,30,40]
box_start_indexes = [[10,10,10], [60,60,60]]
original = sitk.Image(image_size, sitk.sitkUInt8)
for i, si, in enumerate(box_start_indexes,1):
    original[si[0]:si[0]+box_size[0],
             si[1]:si[1]+box_size[1],
             si[2]:si[2]+box_size[2]] = i

sitk.WriteImage(original, 'original.mha')

# Variations on the original using distance maps to do dilations
thresh = [4, 8, 16]
distance_maps = [sitk.SignedMaurerDistanceMap(original==i, squaredDistance=False, useImageSpacing=True) for i in range(1,len(box_start_indexes)+1)]

segmentations = []
for t in thresh:
    seg = sitk.Image(image_size, sitk.sitkUInt8)
    for i, dm in enumerate(distance_maps,1):
        seg += (dm<t)*i
    segmentations.append(seg)
    sitk.WriteImage(seg, str(t)+'.mha')

labelForUndecidedPixels = 255
reference_STAPLE = sitk.MultiLabelSTAPLE(segmentations,
                                         labelForUndecidedPixels)
sitk.WriteImage(reference_STAPLE,'reference_STAPLE.mha')

```

---

<div class="post-metadata">

**Author:** ![Fleur](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Fleur](https://discourse.itk.org/u/Fleur)\
**Post date:** [April 6, 2021, 5:03pm UTC](https://discourse.itk.org/t/multilabelstapleimagefilter-does-not-work/4003/3 "2021-04-06T17:03:56Z")

</div>

Thank you for your help !  
The example works perfectly, I will try setting the parameters you suggested.
