Volume Cropping from Scripted Module

Hello,

I am using Slicer 4.11.0 on a Windows 10 computer and developing a Python scripted module.

I want to call a function from my scripted module that will allow the user to select a 3D region of interest, then once the user selects this region, I want to return to the processing steps in my scripted module and use the voxel coordinates of the user selected region.

Unfortunately, I don’t know the syntax for calling Slicer’s CropVolume module from my scripted module, and I don’t know any other methods in Python for creating a GUI that looks similar to the CropVolume module.

Any help would be greatly appreciated. If I can create a GUI that looks similar to the CropVolume module but takes a 3D numpy array as an input, that would be the preferred method. Otherwise, using CropVolume module would be fine.

Thanks,
Rohan

You can find examples of creating and manipulating Annotation ROI node from Python in this old module: https://github.com/SlicerIGT/SlicerIGT/blob/e7f07bb303e26fde0a010cc528bf36b4759b963c/PlusRemote/PlusRemote.py

You can use crop volume module via Crop Volume module logic. You can access module logic by calling

cropVolumeLogic = slicer.modules.cropvolume.logic()

A more recent example is SliceTracker, which is using this function from SlicerDevelopmentToolbox: https://github.com/QIICR/SlicerDevelopmentToolbox/blob/master/SlicerDevelopmentToolboxUtils/mixins.py#L614-L623.

1 Like

Thank you both for the help! I’ll look through this code and reply here if I have follow-up questions.

Hello,
After looking through these links, I’m aware that you input roi into CropVolume module like this

cropVolumeParameterNode.SetROINodeID(roi.GetID())

But unfortunately I’m still not sure how to allow the user to interactively define roi through a GUI that displays the input volume. Are there any simple examples that go through this?
Or maybe you could tell me which lines of code in the links you already shared do this part?

Thanks again,
Rohan Nadkarni

Rohan, if I were you, I would simply create an ROI widget automatically:

r=slicer.vtkMRMLAnnotationROINode()
slicer.mrmlScene.AddNode(r)

you can initialize its position based on the geometry of the volume you need to crop. Then once the user is done positioning and adjusting the ROI, they could click a button, and you would call CropVolume logic passing the ROI and input volume. I don’t think you need any additional GUI to do that - ROI visualization and resizing is handled by Slicer, it’s a core widget.

Does this make sense?

2 Likes

Thanks Andrey! I’ll probably be able to figure out the button part on my own.
Other than that, I now know how to implement everything I need for ROI selection from scripted module.

1 Like