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

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

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

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.

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

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.

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.