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)
There is also āLabelImageToLabelMapFilterā which will preserve the label values. Also you can use sitk.Cast(img, sitk.sitkLabelUInt16) to run this filter too.