# Manipulate a image of VTK in CUDA

**URL:** https://discourse.vtk.org/t/manipulate-a-image-of-vtk-in-cuda/5339
**Category:** Support
**Created:** [March 7, 2021, 7:40pm UTC](https://discourse.vtk.org/t/manipulate-a-image-of-vtk-in-cuda/5339 "2021-03-07T19:40:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [March 7, 2021, 7:40pm UTC](https://discourse.vtk.org/t/manipulate-a-image-of-vtk-in-cuda/5339/1 "2021-03-07T19:40:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [March 7, 2021, 11:23pm UTC](https://discourse.vtk.org/t/manipulate-a-image-of-vtk-in-cuda/5339/2 "2021-03-07T23:23:46Z")

</div>

> <https://stackoverflow.com/questions/25230541/how-to-convert-a-vtkimage-into-a-numpy-array>

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?

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [March 8, 2021, 12:13am UTC](https://discourse.vtk.org/t/manipulate-a-image-of-vtk-in-cuda/5339/3 "2021-03-08T00:13:15Z")

</div>

reader.GetOutput().GetPointData().SetScalars(numpy\_support.numpy\_to\_vtk(image))

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 8, 2021, 3:04pm UTC](https://discourse.vtk.org/t/manipulate-a-image-of-vtk-in-cuda/5339/4 "2021-03-08T15:04:09Z")

</div>

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:

> <https://github.com/Slicer/Slicer/blob/121ffbd8525ba37d4a653043aadaa622704e5da6/Base/Python/slicer/util.py#L1458-L1463>

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [March 8, 2021, 9:05pm UTC](https://discourse.vtk.org/t/manipulate-a-image-of-vtk-in-cuda/5339/5 "2021-03-08T21:05:38Z")

</div>

Then:

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