Pixel type error when using MergeLabelMapFilter

Hi,

I am running into an error when trying to merge several label maps/masks into one label map. The basics of my code are:

import SimpleITK as sitk

map_files = ['label_map1.nrrd', 'label_map2.nrrd']
map_imgs = [sitk.ReadImage(map) for map in map_files]

combiner = sitk.MergeLabelMapFilter()
combiner.SetMethod(2)

combiner.Execute(*map_imgs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/hunglab/mwarkent/.virtualenvs/torch/lib64/python3.6/site-packages/SimpleITK/SimpleITK.py", line 49075, in Execute
    return _SimpleITK.MergeLabelMapFilter_Execute(self, *args)
RuntimeError: Exception thrown in SimpleITK MergeLabelMapFilter_Execute: /tmp/SimpleITK/Code/Common/include/sitkMemberFunctionFactory.hxx:196:
sitk::ERROR: Pixel type: 16-bit unsigned integer is not supported in 3D byN3itk6simple19MergeLabelMapFilterE

I have tried forcing different pixel types when reading in the image with sitk.ReadImage() but the exception is thrown every time. Any idea what the issue is?

So it seems like I needed to convert the segmentation masks into label map objects via sitk.BinaryImageToLabelMap() and then it worked. Maybe I’m using the terms segmentation mask and label map too interchangeably. Could someone clarify the salient differences?

Following from the above code, this fixes it:

label_maps = [sitk.BinaryImageToLabelMap(img) for img in map_imgs]
combined = combiner.Execute(*lmaps)
combined_img = sitk.LabelMapToLabel(combined)

LabelMap is a special data structure, whereas mask image is an image (with binary or enumerated label values).

1 Like

There is also ā€œLabelImageToLabelMapFilterā€ which will preserve the label values. Also you can use sitk.Cast(img, sitk.sitkLabelUInt16) to run this filter too.

1 Like

There is also ā€œLabelImageToLabelMapFilterā€ which will preserve the label values

@blowekamp Do you mean in place of BinaryImageToLabelMap? If, for example, each image had a sequentially numbered label instead of binary label?

Yes.

BinaryImageToLabelMap splits the label into distinctly labeled connected components or objects. This does a one to many relabeling.

LabelImageToLabelMap preserved the values/labels of the input image.

1 Like