How to use itk.cast_image_filter (in Python)?

When I try this:

# Here deformation_field is of type itk.Image[itk.Vector[itk.F, 2], 2].
deformation_field_d = itk.cast_image_filter(deformation_field, ttype=itk.Image[itk.Vector[itk.D, 2], 2])

I get this error:

File “...\Lib\site-packages\itk\itkCastImageFilterPython.py”, line 36927, in cast_image_filter
instance = itk.CastImageFilter.New(*args, **kwargs)
File “...\Lib\site-packages\itk\support\template_class.py”, line 703, in New
raise RuntimeError(
…<2 lines>…
)
RuntimeError: Expected 2 template parameters. 1 parameters given.

What is the correct way of using cast_image_filter in this case? How can I cast from itk.itkImagePython.itkImageVF22 to itk.itkImagePython.itkImageVD22 please?

Hi Constantine,

For ttype, the argument is a Python tuple of the template type parameters for the filter, the input image type and the output image type.

It is easier to use cast_image_filter via the astype method on Image, which just takes the output pixel type, i.e.

deformation_field_d = deformation_field.astype(itk.Vector\[itk.D, 2\])