SuperElastix / elastix

Official elastix repository
http://elastix.dev
Apache License 2.0
450 stars 111 forks source link

TransformixFilter does not process non-float pixel type images #322

Open thewtex opened 3 years ago

thewtex commented 3 years ago

When itk::TransformixFilter is templated with a TMovingImage that does not match the internal ElastixTemplate TMovingImage, this dynamic_cast will fail:

https://github.com/SuperElastix/elastix/blob/a187f9af5d11abff558faad1aaa4605e8c15bcce/Core/Kernel/elxElastixTemplate.hxx#L100-L101

and no moving image will be detected here:

https://github.com/SuperElastix/elastix/blob/a187f9af5d11abff558faad1aaa4605e8c15bcce/Core/Kernel/elxElastixTemplate.hxx#L387-L388

and no transformation is applied here:

https://github.com/SuperElastix/elastix/blob/a187f9af5d11abff558faad1aaa4605e8c15bcce/Core/Kernel/elxTransformixMain.cxx#L145

thewtex commented 3 years ago

A simple check for this is changing the PixelType from float to double in the test added in: https://github.com/SuperElastix/elastix/pull/323

thewtex commented 3 years ago

@N-Dekker @mstaring @stefanklein is it possible to take a look at this? I think a cast of the type is intended and required somewhere, which I do not know where that should be.

dzenanz commented 3 years ago

I was unaware of this as I was trying to use transformix to deform an atlas (uchar image).

N-Dekker commented 1 year ago

Current status:

  1. The pixel type of the input image should correspond with the template argument of TransformixFilter<TMovingImage>: https://github.com/SuperElastix/elastix/blob/d8e9e1b5bcd1184ca9d3b343e61ccc85857ce407/Core/Main/itkTransformixFilter.h#L58-L59 When using Python + ITKElastix, the image type may be specified in square brackets:
ImageType = itk.Image[itk.F, 2]
transformix_filter = itk.TransformixFilter[ImageType].New()
  1. The pixel type of the images may also need to be specified as parameter, to be included with the parameter map, to be added to the parameter object. In Python, something like the following (untested code):
parameter_map = 
{
  "FixedInternalImagePixelType": ("float"),
  "MovingInternalImagePixelType": ("float")
   ... [other parameters here] ...
}

parameter_object = itk.ParameterObject.New()
parameter_object.AddParameterMap(parameter_map)
transformix_filter.SetMovingImage(moving_image)
transformix_filter.SetTransformParameterObject(parameter_object)
  1. The pixel type must be included during CMake configuration of elastix (ELASTIX_IMAGE_2D_PIXEL_TYPES, ELASTIX_IMAGE_3D_PIXEL_TYPES, etc)

That's it for now! Even though it may be somewhat inconvenient, I think it works well.