Continuing the discussion from Creating a displacement field image from a numpy array:
Hello!
I am trying to warp an image. The image is built from an array, and I would like to build the displacement field from another array as well.
(This post is very similar. This and this may be related). The difference is that I am trying to do this (1) without reading files and (2) setting each voxel (pixel) value with for
loops (something like ImageIterator
), but using pure arrays. My hair is getting loose .
import numpy as np
import itk
img_shape = (5, 101, 101)
fixed_image = itk.image_from_array(
np.zeros(img_shape).astype(np.float32))
array_from_fixed_image = itk.array_from_image(fixed_image)
VoxelType = itk.F
ImageDimension = len(img_shape)
ImageType = itk.Image[VoxelType, ImageDimension]
VectorVoxelType = itk.F
VectorDimension = 3
VectorTypeDim = itk.Vector[VectorVoxelType, VectorDimension]
VectorImageType = itk.Image[VectorTypeDim, ImageDimension]
DisplacementField = itk.image_from_array(
np.ones((3, *array_from_fixed_image.shape),
dtype=np.float32),
is_vector=True)
interpolator = itk.LinearInterpolateImageFunction[
ImageType, itk.D].New()
# This statement failed
warpFilter = itk.WarpImageFilter[
ImageType, ImageType, itk.Image[itk.F,4]].New(
Interpolator=interpolator,
OutputSpacing=fixed_image.GetSpacing(),
OutputOrigin=fixed_image.GetOrigin(),
DisplacementField=DisplacementField,
Input=fixed_image)
moving_image = warpFilter.GetOutput()
The error that I get is:
>>>
Traceback (most recent call last):
File "/home/edgar/.local/lib/python3.10/site-packages/itk/support/template_class.py", line 525, in __getitem__
this_item = self.__template__[key]
KeyError: (<class 'itk.itkImagePython.itkImageF3'>, <class 'itk.itkImagePython.itkImageF3'>, <class 'itk.itkImagePython.itkImageF4'>)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 17, in __PYTHON_EL_eval
File "<string>", line 26, in <module>
File "/home/edgar/.local/lib/python3.10/site-packages/itk/support/template_class.py", line 529, in __getitem__
raise itk.TemplateTypeError(self, key)
itk.support.extras.TemplateTypeError: itk.WarpImageFilter is not wrapped for input type `itk.Image[itk.F,3], itk.Image[itk.F,3], itk.Image[itk.F,4]`.
To limit the size of the package, only a limited number of
types are available in ITK Python. To print the supported
types, run the following command in your python environment:
itk.WarpImageFilter.GetTypes()
Possible solutions:
* If you are an application user:
** Convert your input image into a supported format (see below).
** Contact developer to report the issue.
* If you are an application developer, force input images to be
loaded in a supported pixel type.
e.g.: instance = itk.WarpImageFilter[itk.Image[itk.SS,2], itk.Image[itk.SS,2], itk.Image[itk.Vector[itk.F,2],2]].New(my_input)
* (Advanced) If you are an application developer, build ITK Python yourself and
turned to `ON` the corresponding CMake option to wrap the pixel type or image
dimension you need. When configuring ITK with CMake, you can set
`ITK_WRAP_${type}` (replace ${type} with appropriate pixel type such as
`double`). If you need to support images with 4 or 5 dimensions, you can add
these dimensions to the list of dimensions in the CMake variable
`ITK_WRAP_IMAGE_DIMS`.
Supported input types:
itk.Image[itk.SS,2]
itk.Image[itk.UC,2]
itk.Image[itk.US,2]
itk.Image[itk.F,2]
itk.Image[itk.D,2]
itk.Image[itk.SS,3]
itk.Image[itk.UC,3]
itk.Image[itk.US,3]
itk.Image[itk.F,3]
itk.Image[itk.D,3]
itk.Image[itk.SS,4]
itk.Image[itk.UC,4]
itk.Image[itk.US,4]
itk.Image[itk.F,4]
itk.Image[itk.D,4]