-1

I'm new in programming with Python and I need to translate code from c++ to python using the itk library, but I'm encountering many difficulties. I show you an example, in the c++ code I have :

typedef itk::PasteImageFilter <Image3D, Image3D, Image3D> PasteImageFilterType;

with:

typedef itk::Image <unsigned char, 3> BinaryImageType3D;

I translate it in Python in this way:

PasteImageFilterType = itk.PasteImageFilter[itk.Image[itk.UC, 3], itk.Image[itk.UC, 3], itk.Image[itk.UC, 3]]

But I obtain the following error:

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.PasteImageFilter.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.PasteImageFilter[itk.Image[itk.SS,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`.

Can someone explain how I can deal with this problem? Consider that it's not only for this function that I have this problem, but for many others; I don't find any documentation of itk for python; and I haven't understood very well the solution proposed by python. In your opinion which is the best strategy to deal with this error.

Thank you very much for the help

Dženan
  • 3,329
  • 3
  • 31
  • 44
Charlie97
  • 11
  • 1
  • 1
    Trying to translate code directly from one language to another is usually a mistake. Better to learn both languages well, learn what the original program does, then write a *new* program doing the same in the second language, using its idioms and conventions. – Jesper Juhl Jul 28 '22 at 11:03

1 Answers1

1

tl;dr: PasteImageFilterType = itk.PasteImageFilter[itk.Image[itk.UC, 3]]

Full answer:

A more direct way to translate those two lines of C++ into Python are:

BinaryImageType3D= itk.Image[itk.UC, 3]
PasteImageFilterType = itk.PasteImageFilter[BinaryImageType3D, BinaryImageType3D, BinaryImageType3D]

But you probably already knew that.

The error message also yields a list:

Supported input types:

itk.Image[itk.SS,2]
itk.Image[itk.SS,3]
itk.Image[itk.UC,2]
itk.Image[itk.UC,3]
itk.Image[itk.US,2]
itk.Image[itk.US,3]
itk.Image[itk.F,2]
itk.Image[itk.F,3]

And itk.Image[itk.UC,3] is on the list, so that causes confusion, understandably. You probably also took a look at the documentation of PasteImageFilter and saw that it has 3 template parameters, so providing 3 template parameters in Python was a logical choice.

It so happens that we are trying to make Python version of ITK simpler than C++ when we can, and PasteImageFilter was one of those places. As it is usually invoked with all 3 template parameters being the same, class' Python wrapping asks for just one template parameter. This is enough, as C++ declaration specifies default values for second and third template parameter equal to the first one.

So a declaration which works is: PasteImageFilterType = itk.PasteImageFilter[BinaryImageType3D].

Finally, ITK forum has more ITK experts paying attention, and is a better place to ask an ITK-related question.

Dženan
  • 3,329
  • 3
  • 31
  • 44