How to get a ROI to fit volume in the Volume Rendering module with python?

@jamesobutler @jcfr @lassoan @pieper
How to get a ROI to fit volume in the Volume Rendering module with python?

Or how to get the center of a volume with python?

I have recently used the crop volume module in python in order to fit an ROI to a volume:

# Get the first volume in the scene
volumeNode = slicer.util.getNode("vtkMRMLScalarVolumeNode1")
# Create a new ROI
roiNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLAnnotationROINode")
# Make new parameter node in order to use the crop volume module programmatically
crop_module = slicer.vtkMRMLCropVolumeParametersNode()
# Add parameter node to the scene
slicer.mrmlScene.AddNode(crop_module)
# Set the volume as the input volume in the crop volume module
crop_module.SetInputVolumeNodeID(volumeNode.GetID())
# Set output volume as the same volume to overwrite original volume (only needed if you actually want to crop the volume)
crop_module.SetOutputVolumeNodeID(volumeNode.GetID())
# Set the input ROI
crop_module.SetROINodeID(roiNode.GetID())
# Use the Fit ROI to Volume function of the crop volume module
slicer.modules.cropvolume.logic().FitROIToInputVolume(crop_module)

3 Likes

Great! Thank you very much!

@Juicy I too did the same, however, how did you set the dimensions for the ROI in this code?

Hi Panda,

The dimensions of the ROI do not need to be entered. The code uses the functionality of the crop volume module to fit an ROI to the same dimensions as the volume. The dimensions depend on which input volume you choose. Do this with the first line of the code. You can type the name of the volume instead of ‘vtkMRMLScalarVolumeNode1’

1 Like