Sorry to bother you with my question. I’m new to Slicer and I’m trying to write a scripted python module. I’m trying to recognize MRI images by a deep-learning model. I have already loaded my model and I’m now trying to load my image to my module. My image is a single image in jpg format. I tried this:
x = cv2.resize((img), (224, 224), interpolation=cv2.INTER_CUBIC) / 255.0
it didn’t work, I don’t think it read the image.What is the correct way of implementing this?(by the way, I have already installed cv2)
You’ve got a couple options.
Read the file directly from disk (not recommended), using conventional
If you’ve loaded the image into Slicer, then you can do
from vtk.util import numpy_support
imageNode = slicer.util.getNode('<nameOfJpgImageWithoutExtension>')
vtk_image = imageNode.GetImageData()
np_image = numpy_support.vtk_to_numpy(vtk_image.GetPointData().GetScalars())
dims = vtk_image.GetDimensions()
np_image.reshape(dims, dims, dims)
See this link https://stackoverflow.com/questions/12754971/how-to-convert-a-3d-vtkdataset-into-a-numpy-array for more information.
cv2 python bindings use numpy arrays as their images.
You can also load an image directly to a volume node:
volumeNode = slicer.util.loadVolume('path/to/myimage.jpg')