Currently I am working on developing a custom extension.
One of the functionalities that I want to implement is a user can scroll in axial view and select the axial slice as the slice-of-interest. Then I want to use this slice a 2D nrrd/mha file in my algorithm.
I looked at the similar topics and implemented suggested ways. The problem is that my original volume has size (512, 512 ,225) but extracted slice has size (932, 548, 1). I want it to be (512, 512, 1). How can I extract with correct shape?
Below you may find the code I used and images [1].
Thank you!
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()
slicer.util.exportNode(volumeNode, <SAVE_PATH>)
Soler, L., A. Hostettler, V. Agnus, A. Charnoz, J. Fasquel, J. Moreau, A. Osswald, M. Bouhadjar, and J. Marescaux. “3D image reconstruction for comparison of algorithm database: A patient specific anatomical and medical image database.” IRCAD, Strasbourg, France, Tech. Rep (2010)
Assuming the data was axial (like this CT) you can just access the volume node as an array and get array[k] where k is the slice offset transformed to IJK space. I f you look in the script repository you can find all the pieces pretty easily.