我正在使用python 2.6和ITK包装器(来自PythonXY 2.6.6.2)。我正在尝试将numpy / scipy的3D图像发送到itk进行处理。
import itk
imageType = itk.Image.F3
buf = scipy.zeros( (100,100,100), dtype = float)
itkImage = itk.PyBuffer[imageType].GetImageFromArray(buf)
GetImageFromArray()失败,出现以下错误:
RuntimeError: Contiguous array couldn't be created from input python object
但是,如果我自己不创建缓冲区,但让ITK创建图像,GetImageFromArray()突然起作用:
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)
如何自己创建一个numpy数组,GetImageFromArray()会接受它?
答案 0 :(得分:6)
答案很简单:
为ndarray指定正确的类型使其工作:
import itk
imageType = itk.Image.F3
buf = scipy.zeros( (100,100,100), dtype = numpy.float32)
itkImage = itk.PyBuffer[imageType].GetImageFromArray(buf)