# Converting a 2D numpy array to a VTK image

**URL:** <https://discourse.slicer.org/t/converting-a-2d-numpy-array-to-a-vtk-image/34825>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [March 11, 2024, 4:57pm UTC](https://discourse.slicer.org/t/converting-a-2d-numpy-array-to-a-vtk-image/34825 "2024-03-11T16:57:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaraabdelazim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yaraabdelazim/32/69460_2.png) [@yaraabdelazim](https://discourse.slicer.org/u/yaraabdelazim)\
**Post date:** [March 11, 2024, 4:57pm UTC](https://discourse.slicer.org/t/converting-a-2d-numpy-array-to-a-vtk-image/34825/1 "2024-03-11T16:57:03Z")

</div>

Hello,

I am trying to detect edges in a volume and therefore, I use OpenCV. After converting my slice to a numpy array and detecting the edges, I am able to display it in my Jupyter notebook. Here is the result I get:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/8/385e869fd7e1bfea315d95e3c745c136a3e8140c.png)

However, when I try to display this in 3D slicer, the image gets displayed on 2 different slices and the orientation isn’t right. Here’s what I mean:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/f/6f27c52df5afdb33ff3faa032c01632c69c0d589.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/8/483efa6bba1caee322975120db4acf74aa672efa.png)  
I’m not really sure where I went wrong as I have tried to change the dimensions in multiple different ways but nothing seems to be working. Is there something that I missed to be able to fix this issue?

Here is my code:

```python
data_type = vtk.VTK_UNSIGNED_CHAR
imageSpacing = [0.1, 0.1, 0.1]
imageDirections = [[1,0,0], [0,1,0], [0,0,1]]
shape = edges.shape
print("tableau contours:", shape)
dims = volume.GetImageData().GetDimensions()
print("tableau dimensions volume:", dims)

### edges = edges[:,np.newaxis,:]
flat_data_array = edges.flatten()
vtk_data = numpy_support.numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=data_type)

plt.figure()
plt.title("Résultat détection de contours")
plt.imshow(edges, cmap='gray')
plt.show()

result = vtk.vtkImageData()
result.GetPointData().SetScalars(vtk_data)
result.SetDimensions(60, 1, 512)

result.Modified()
result_volume = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLScalarVolumeNode')
result_volume.SetSpacing(imageSpacing)
result_volume.SetIJKToRASDirections(imageDirections)
result_volume.SetAndObserveImageData(result)
result_volume.CreateDefaultDisplayNodes()

slicer.util.updateVolumeFromArray(result_volume, edges)

### result_volume.SetIJKToRASDirections(imageDirections)

slicer.util.updateVolumeFromArray(result_volume, edges)

```

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 12, 2024, 4:27am UTC](https://discourse.slicer.org/t/converting-a-2d-numpy-array-to-a-vtk-image/34825/2 "2024-03-12T04:27:39Z")

</div>

OpenCV is for computer vision. It is very good for image processing for computer vision but not well suited for medical image processing. For example, most OpenCV functions operate in pixel space (instead of physical space), cannot deal with arbitrarily oriented images (ignores IJKtoRAS), many functions don’t work on 3D images, and in general its developers did not think about medical image processing needs when they implemented the library.

Most likely you will have much simpler life and get better results if you use ITK instead. You can either use SimpleITK or ITK-Python. There is a simple example of using SimpleITK in Slicer [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#running-an-itk-filter-in-python-using-simpleitk).

---

<div class="post-metadata">

**Author:** ![yaraabdelazim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yaraabdelazim/32/69460_2.png) [@yaraabdelazim](https://discourse.slicer.org/u/yaraabdelazim)\
**Post date:** [March 12, 2024, 9:03am UTC](https://discourse.slicer.org/t/converting-a-2d-numpy-array-to-a-vtk-image/34825/3 "2024-03-12T09:03:38Z")

</div>

My issue with SimpleITK is tha whenever I try to execute the CannyEdgeDetectionImageFilter(), I get an error that I don’t quite understand and I don’t know where it’s coming from. Here is my error:

RuntimeError: Exception thrown in SimpleITK CannyEdgeDetectionImageFilter\_Execute: sitk::ERROR: Pixel type: 16-bit unsigned integer is not supported in 3D by class slicer\_itk::simple::CannyEdgeDetectionImageFilter.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [March 12, 2024, 11:02am UTC](https://discourse.slicer.org/t/converting-a-2d-numpy-array-to-a-vtk-image/34825/4 "2024-03-12T11:02:49Z")

</div>

It seems that the scalar type of your image is not supported byu the Canny filter. You can cast it to 8 bits if it does not mean too much data loss for you.

The other option is to make sure the geometry of the image is preserved after the OpenCV processing. Here is some background about this  
[https://slicer.readthedocs.io/en/latest/user\_guide/coordinate\_systems.html](https://slicer.readthedocs.io/en/latest/user_guide/coordinate_systems.html)  
But basically what you’ll need is to set the numpy output to a volume that has the same geometry as the input. What I’d do is I’d clone the input volume using subject hierarchy and then use that cloned node with the `updateVolumeFromArray` method.  
[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#clone-a-node](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#clone-a-node)

---

<div class="post-metadata">

**Author:** ![yaraabdelazim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yaraabdelazim/32/69460_2.png) [@yaraabdelazim](https://discourse.slicer.org/u/yaraabdelazim)\
**Post date:** [March 12, 2024, 1:51pm UTC](https://discourse.slicer.org/t/converting-a-2d-numpy-array-to-a-vtk-image/34825/5 "2024-03-12T13:51:52Z")

</div>

Thank you very much, that was very helpful !
