1
votes

I'm trying to read a nrrd file by itk and show it by vtk. But I have some trouble on convert itk to vtk.

import itk


file_name = '/home/yao/workspace/test/1.nrrd'
image_type = itk.Image[itk.UC, 2]
reader = itk.ImageFileReader[image_type].New()
reader.SetFileName( file_name )
reader.Update()

itk_vtk_converter = itk.ImageToVTKImageFilter[image_type].New()
itk_vtk_converter.SetInput(reader.GetOutput())
itk_vtk_converter.Update()

and I got the message

Traceback (most recent call last):   File "ex1.py", line 11, in <module>
    itk_vtk_converter = itk.ImageToVTKImageFilter[image_type].New()   File "/usr/lib/InsightToolkit/WrapITK/Python/itkLazy.py", line 14, in
__getattribute__
    value = types.ModuleType.__getattribute__(self, attr) AttributeError: 'LazyITKModule' object has no attribute 'ImageToVTKImageFilter'

I'm using itk3.20, python2.7. How can I fix it?

Regards.

Yao

4

4 Answers

1
votes

C++ filter itk::ImageToVTKImageFilter is not wrapped but you can use itk.VTKImageImport instead.You can see at http://www.vtk.org/Wiki/VTK/Examples/Python/vtkWithNumpy VTKImageImport in action. You can use itkPyBuffer to transform your itkImage in a numpy array

0
votes

Your code is correct. I think you may not correctly build the itk in Python. In my opinion, the easiest way to use ITK/VTK in Python is to use the Pythonxy. You can try it.

0
votes

Use the latest version of ITK 4, which has wrappers for these classes (you must enable the ITKVtkGlue Module in your CMake configuration).

0
votes

I don't know why it isn't working as I haven't tried directly from ITK to VTk but you can try using this code.

def itkImageToVtk(image):
    arrayData = sitk.GetArrayFromImage(image)
    print arrayData.flags
    arrayDataC = np.array(arrayData[:,:,1], order='C')
    print arrayDataC.flags
    ArrayDicom = numpy_support.numpy_to_vtk(arrayDataC)

It converts the 2D ITK image to 2D VTK