Manipulate a image of VTK in CUDA

Dear All,

I want to manipulate a VTK image in CUDA (PyCUDA):

        reader1 = vtk.vtkMetaImageReader()
        reader1.SetFileName('c:\\users\\luisgo//catia_pre//'+'binary2.mhd')
        reader1.Update()
        reader = reader1.GetOutput()
        dim3=reader.GetDimensions()
        reader=reader1
        image=reader.GetOutput().GetScalarPointer()
        dados1=np.asarray([np.int32(dim3[0]),np.int32(dim3[1]),np.int32(dim3[2])],dtype=np.int32)
        count3=dados1[0]*dados1[1]*dados1[2]
        rot4 = mod.get_function("delete_img")
        rot4(cuda.InOut(image), cuda.In(dados1),block=(32,1,1),grid=(int((np.uint32(count3) >> 
                                                                      5)+1),1,1))

Traceback (most recent call last):
File “C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pycuda\driver.py”, line 128, in get_device_alloc
self.dev_alloc = mem_alloc_like(self.array)
File “C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pycuda\driver.py”, line 720, in mem_alloc_like
return mem_alloc(ary.nbytes)
AttributeError: ‘str’ object has no attribute ‘nbytes’
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “D:\zipback\visual studio 2015\Projects\Registration\Registration\teste3.py”, line 252, in thread_function
rot4(cuda.InOut(image), cuda.In(dados1),block=(32,1,1),grid=(int((np.uint32(count3) >> 5)+1),1,1))
File “C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pycuda\driver.py”, line 437, in function_call
handlers, arg_buf = _build_arg_buf(args)
File “C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pycuda\driver.py”, line 196, in _build_arg_buf
arg_data.append(int(arg.get_device_alloc()))
File “C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pycuda\driver.py”, line 130, in get_device_alloc
raise TypeError(“could not determine array length of ‘%s’: unsupported array type or not an array” % type(self.array))
TypeError: could not determine array length of ‘<class ‘str’>’: unsupported array type or not an array


Seems that “image” is not in the right format. How to solve this? How to convert to the format PyCUDA understands?

Thanks,

Luís Gonçalves

I done:

        sc = reader.GetOutput().GetPointData().GetScalars()
        image = vtk_to_numpy(sc)

instead of:

        image=reader.GetOutput().GetScalarPointer()

and it works (CUDA Kernel).

But now I want to put “image” back to “reader”. Something like numpy_to_vtk.

How I can do that?

reader.GetOutput().GetPointData().SetScalars(numpy_support.numpy_to_vtk(image))

The numpy array is read/write. If you modify the numpy array then the image data is changed automatically. You just need to indicate to VTK that you have finished with updating the voxel data when you are done with all your changes by calling the necessary Modified() events:

Then:

reader.GetOutput().GetPointData().GetScalars().Modified()