Runtime Error trying to use the LabelMapToBinaryImageFilter in SimpleITK

Hello,

I am trying to combine multiple images (same spacing, dimensions, etc.) Each image contains a 1 for object and 0 for background and these are 3D volumes.

this is my code:

def labels_to_binary_image(image_list):
    """Creates a new image that contains all the labels"""
    arr_union = None
    for image in image_list:
        if arr_union is None:
            arr_union = sitk.GetArrayFromImage(image)
        else:
            arr_union += sitk.GetArrayFromImage(image)

    ima_union = sitk.GetImageFromArray(arr_union)
    _filter = sitk.LabelMapToBinaryImageFilter()
    return _filter.Execute(ima_union)

When I run it I obtain this error:

  File "/Users/odin/anaconda/lib/python2.7/site-packages/SimpleITK/SimpleITK.py", line 41239, in Execute
    return _SimpleITK.LabelMapToBinaryImageFilter_Execute(self, *args)
RuntimeError: Exception thrown in SimpleITK LabelMapToBinaryImageFilter_Execute: /scratch/dashboards/SimpleITK-OSX10.6-intel-pkg/SimpleITK/Code/Common/include/sitkMemberFunctionFactory.hxx:196:
sitk::ERROR: Pixel type: 8-bit unsigned integer is not supported in 3D byN3itk6simple27LabelMapToBinaryImageFilterE

What am I doing wrong?

Thanks,

Diego

This is what intended:

def labels_to_binary_image(image_list):
    """Creates a new image with the union"""
    arr_union = None
    for image in image_list:
        if arr_union is None:
            arr_union = sitk.GetArrayFromImage(image)
        else:
            arr_union += sitk.GetArrayFromImage(image)

    arr_union[list(np.where(arr_union > 0))] = 1 # just make it binary again

    #ima_union = sitk.GetImageFromArray(arr_union)
    #_filter = sitk.LabelMapToBinaryImageFilter()
    #return _filter.Execute(ima_union)

    ima_union = sitk.GetImageFromArray(arr_union)
    return ima_union # this works!

With your implementation you loose the image meta data! You should stay in SimpleITK and just use the overloaded operators. This one liner may work too:

‘’’

sitk.NaryAdd(image_list)!=0

‘’’

You probable could do ’sum(image_list)’ too

Hi Bradley, I think your post got cut off…

I removed the extra bit at the end. The proper SimpleITK solution really is that short.