# How can I convert vtk.vtkImageData() to an OpenCv Image?

**URL:** <https://discourse.slicer.org/t/how-can-i-convert-vtk-vtkimagedata-to-an-opencv-image/13120>\
**Category:** Support\
**Created:** [August 21, 2020, 5:52pm UTC](https://discourse.slicer.org/t/how-can-i-convert-vtk-vtkimagedata-to-an-opencv-image/13120 "2020-08-21T17:52:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![apparrilla](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/apparrilla/32/9230_2.png) [@apparrilla](https://discourse.slicer.org/u/apparrilla)\
**Post date:** [August 21, 2020, 5:52pm UTC](https://discourse.slicer.org/t/how-can-i-convert-vtk-vtkimagedata-to-an-opencv-image/13120/1 "2020-08-21T17:52:00Z")

</div>

I want to work arround with images from slices but I have problems converting them to a cv2 format.  
Thanks

---

<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:** [August 21, 2020, 6:00pm UTC](https://discourse.slicer.org/t/how-can-i-convert-vtk-vtkimagedata-to-an-opencv-image/13120/2 "2020-08-21T18:00:55Z")

</div>

Would you like to extract 2D slices from a volume?

---

<div class="post-metadata">

**Author:** ![apparrilla](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/apparrilla/32/9230_2.png) [@apparrilla](https://discourse.slicer.org/u/apparrilla)\
**Post date:** [August 21, 2020, 6:18pm UTC](https://discourse.slicer.org/t/how-can-i-convert-vtk-vtkimagedata-to-an-opencv-image/13120/3 "2020-08-21T18:18:08Z")

</div>

I have extracted 2D slice with a peace of code from script repository but I can’t convert them to a openCV Mat to play with it.

---

<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:** [August 21, 2020, 6:37pm UTC](https://discourse.slicer.org/t/how-can-i-convert-vtk-vtkimagedata-to-an-opencv-image/13120/4 "2020-08-21T18:37:18Z")

</div>

If you are working in Python then the easiest is to get the image as a numpy array and pass that array to OpenCV.

---

<div class="post-metadata">

**Author:** ![apparrilla](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/apparrilla/32/9230_2.png) [@apparrilla](https://discourse.slicer.org/u/apparrilla)\
**Post date:** [August 21, 2020, 10:16pm UTC](https://discourse.slicer.org/t/how-can-i-convert-vtk-vtkimagedata-to-an-opencv-image/13120/5 "2020-08-21T22:16:41Z")

</div>

I´ve converted 40 index Red slice to numpy array with:

```
sliceNodeID = 'vtkMRMLSliceNodeRed'
# Get image data from slice view
sliceNode = slicer.mrmlScene.GetNodeByID(sliceNodeID)
appLogic = slicer.app.applicationLogic()
sliceLogic = appLogic.GetSliceLogic(sliceNode)
sliceLayerLogic = sliceLogic.GetBackgroundLayer()
reslice = sliceLayerLogic.GetReslice()
reslicedImage = vtk.vtkImageData()
reslicedImage.DeepCopy(reslice.GetOutput())

# Create new volume node using resliced image
volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")
volumeNode.SetIJKToRASMatrix(sliceNode.GetXYToRAS())
volumeNode.SetAndObserveImageData(reslicedImage)
volumeNode.CreateDefaultDisplayNodes()
volumeNode.CreateDefaultStorageNode()

# Get voxels as a numpy array
voxels = slicer.util.arrayFromVolume(volumeNode)

sliceIndex = 40
slice = voxels[sliceIndex:,:]

```

but I don´t know how to convert slice in a openCv Mat.

---

<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:** [August 21, 2020, 10:44pm UTC](https://discourse.slicer.org/t/how-can-i-convert-vtk-vtkimagedata-to-an-opencv-image/13120/6 "2020-08-21T22:44:01Z")

</div>

I don’t use OpenCV in Python, but it should be a trivial operation to convert numpy array to OpenCV image. Do a web search for `numpy array to opencv image` - the solution should be among the first few hits.
