How to use NumericTraits in itk python binding


(yann) #1

I’m using itk.GetImageViewFromArray(image) to view an np image as itk image.
How to get the type of pixel, and get information about it using NumericTraits ?

In C++ I was using for example itk::NumericTraits<PixelType>::max(), but with python I don’t see how to do it…
Thanks!


(Matt McCormick) #2

Hi Yann,

For the itk.Image, the Python type built-in function can be used to inspect the type.

print(type(image))

For numpy.array, inspect the dtype attribute:

print(array.dtype)

To get the template parameters for an itk object, use, itk.template, e.g.:

>>> itk.template(image)
(<itkTemplate itk::Image>, (itkRGBPixelPython.itkRGBPixelUC, 2))

The pixel type can be re-used with the wrapped NumericTraits class:

print(itk.NumericTraits[itk.template(image)[1][0]].max())

HTH,
Matt


(yann) #3

Thanks Matt, I wouldn’t get that by myself !