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
# Set the volume as the input volume in the crop volume module
# Set output volume as the same volume to overwrite original volume (only needed if you actually want to crop the volume)
# Set the input ROI
# Use the Fit ROI to Volume function of the crop volume module


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
roiNode.SetRadiusXYZ(50, 50, 50)



2 posts were split to a new topic: How to fit ROI to volume in Pythond


Operating system: Windows 10
Slicer version: 4.11

Actually I want to Fit the ROI to the volume in the “Crop Volume” module. My Dicom file is oriented and I need to have an oriented volume for cropping. I can do it directly in 3D Slicer but I want to automate this process and I want to do it by Jupyter.

The code didn’t work for me. I need to pass my volume as an input of slicer.modules.crop volume.logic().FitROIToInputVolume(crop_module) function but it just accepts cropped volume as an input. Is there any solution to this problem?

I need to have the attached image by Jupyter.
Screenshot (10)

The example above works well, but I’ve added an updated code snippet to the script repository that uses a markup ROI instead of the old annotation ROI:

1 Like

Yes the previous code worked but not in the way I expected.

When I press the “Fit to Volume” in 3D slicer (first attached image), the image I attached in previous post, was shown

But when I run the code, I got not-oriented volume for cropping(second attached image).

My question is about having oriented volume in the same orientation of the Dicom(image attached to the previous post).

Is there any other function to use?

If you want to rotate the ROI to match the volume axis directions then you can add this line to the code snippet:


It worked. Thank you so much for your help.

1 Like