I created a new ITK external module to benefit from the nice packaging and pypi distribution.
My filter is templated over the input image types (a scalar image and a mask image).
The wrapping instructions looks like:
itk_wrap_class("itk::DissolveMaskImageFilter" POINTER)
itk_wrap_image_filter_combinations("${WRAP_ITK_SCALAR}" "${WRAP_ITK_INT}" 2)
itk_wrap_image_filter_combinations("${WRAP_ITK_SCALAR}" "${WRAP_ITK_INT}" 3)
itk_end_wrap_class()
I am able to run it using any input image type using the object oriented api, e.g. (SCALAR_TYPE
could be itk.F
, itk.US
, etc.)
import itk
image = itk.imread('path/to/labels.mha').astype(SCALAR_TYPE)
mask = itk.imread('path/to/mask.mha').astype(itk.UC)
ImageType = type(image)
MaskType = type(mask)
dissolve = itk.DissolveMaskImageFilter[ImageType, MaskType].New()
dissolve.SetInput(image)
dissolve.SetMaskImage(mask)
dissolve.Update()
modified_image = dissolve.GetOutput()
itk.imwrite(modified_image, 'modified_image.mha')
For the pythonic api it seems ImageType
has to match MaskType
, e.g.:
import itk
labels = itk.imread('path/to/labels.mha').astype(itk.US)
mask = itk.imread('path/to/mask.mha').astype(itk.US)
modified_labels = itk.dissolve_mask_image_filter(labels, mask_image=mask)
itk.imwrite(modified_labels, 'path/to/modified_labels.mha')
If I use a different MaskType
, e.g. itk.Image[itk.UC,3]
I get following error:
Traceback (most recent call last):
File “/Users/lloyd/Code/ITK_Module/ITKDissolve/…/example_script.py”, line 11, in
modified_labels = itk.dissolve_mask_image_filter(labels, mask_image=mask)
File “/Users/lloyd/Code/ITK_Module/.venv/lib/python3.9/site-packages/itk/support/helpers.py”, line 173, in image_filter_wrapper
return image_filter(*args, **kwargs)
File “/Users/lloyd/Code/ITK_Module/.venv/lib/python3.9/site-packages/itk/itkDissolveMaskImageFilterPython.py”, line 2070, in dissolve_mask_image_filter
instance = itk.DissolveMaskImageFilter.New(*args, **kwargs)
File “/Users/lloyd/Code/ITK_Module/.venv/lib/python3.9/site-packages/itk/support/template_class.py”, line 734, in New
return self[list(keys)[0]].New(*args, **kwargs)
File “/Users/lloyd/Code/ITK_Module/.venv/lib/python3.9/site-packages/itk/itkDissolveMaskImageFilterPython.py”, line 2046, in New
template_class.New(obj, *args, **kargs)
File “/Users/lloyd/Code/ITK_Module/.venv/lib/python3.9/site-packages/itk/support/template_class.py”, line 800, in New
itk.set_inputs(self, args, kargs)
File “/Users/lloyd/Code/ITK_Module/.venv/lib/python3.9/site-packages/itk/support/extras.py”, line 1252, in set_inputs
attrib(itk.output(value))
TypeError: Expecting argument of type itkImageUS3 or itkImageSourceIUS3
Any ideas what I am doing wrong?