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
roiNode.SetName("ROI")
roiNode.SetXYZ(PP)
roiNode.SetRadiusXYZ(50, 50, 50)

我是这样定义ROI大小的…

2 Likes

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

Hi,

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: https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#fit-markups-roi-to-volume

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).
expl

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:

slicer.modules.cropvolume.logic().SnapROIToVoxelGrid(cropVolumeParameters)

It worked. Thank you so much for your help.

1 Like