[ITK-users] Python Wrapper Pybuffer

Marc-Michel Rohé marc-michel.rohe at inria.fr
Wed Nov 16 08:55:51 EST 2016


Hello,

is there a way to read an vector image 
(itk::Image<itk::Vector<float,3>,3>) with the python wrapper and to 
convert it easily to a numpy array ?

I tried using PyBuffer but it does not seem to work and I get the 
following error:

vectorType=itk.Vector[itk.F,3]
fieldType=itk.Image[vectorType,3]
readerType=itk.ImageFileReader[fieldType,3]
reader=readerType.New()
reader.SetFileName("file")
reader.Update()
image=reader.GetOutput()
buf = itk.PyBuffer[imageType].GetArrayFromImage(image)
/
//Traceback (most recent call last)://
//  File "<stdin>", line 1, in <module>//
//NameError: name 'imageType' is not defined//
//>>> buf = itk.PyBuffer[fieldType].GetArrayFromImage(image)//
//Traceback (most recent call last)://
//  File "<stdin>", line 1, in <module>//
//  File 
"/home/mrohe/Librairies/ITK-4.9.0_wrap/build/Wrapping/Generators/Python/itkTemplate.py", 
line 252, in __getitem__//
//    (str(parameters), self.__name__))//
//KeyError: "itkTemplate : No template [<class 
'itkImagePython.itkImageVF33'>] for the itk::PyBuffer class"/


What I do is to cast manually the image to the numpy array (see below) 
with for loops (since I did not find any iterator on the python 
wrapper). It works but is really slow and inefficient. There must be a 
better way no ?

     for sx in range(sizeX):
         for sy in range(sizeY):
             for sz in range(sizeZ):
                 vector=image.GetPixel([sx,sy,sz])
                 data[sx,sy,sz,0]=vector[0]
                 data[sx,sy,sz,1]=vector[1]
                 data[sx,sy,sz,2]=vector[2]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20161116/8fd67c47/attachment.html>


More information about the Insight-users mailing list