Why doesn't Python ITK PyBuffer accept my numpy array?

I am using python 2.6 with ITK shells (from PythonXY 2.6.6.2). I am trying to send a 3D image from numpy / scipy to itk for processing.

import itk imageType = itk.Image.F3 buf = scipy.zeros( (100,100,100), dtype = float) itkImage = itk.PyBuffer[imageType].GetImageFromArray(buf) 

GetImageFromArray () error with the following error:

 RuntimeError: Contiguous array couldn't be created from input python object 

However, if I myself do not create the buffer, but let ITK create the image, GetImageFromArray () will suddenly start working:

 import itk imageType = itk.Image.F3 itkImage1 = imageType.New(Regions=[256, 256, 256]) buf = itk.PyBuffer[imageType].GetArrayFromImage(itkImage1) itkImage2 = itk.PyBuffer[imageType].GetImageFromArray(buf) 

How do I create a numpy array that GetImageFromArray () will accept?

+4
source share
1 answer

The answer is simple:

  • In python, a "float" can be 64-bit (double in c).
  • There is a 32-bit float in itk F3.

Specifying the correct type for ndarray makes it work:

 import itk imageType = itk.Image.F3 buf = scipy.zeros( (100,100,100), dtype = numpy.float32) itkImage = itk.PyBuffer[imageType].GetImageFromArray(buf) 
+6
source

Source: https://habr.com/ru/post/1379654/


All Articles