# Volume Cropping from Scripted Module

**URL:** <https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169>\
**Category:** Development\
**Created:** [April 17, 2020, 11:21pm UTC](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169 "2020-04-17T23:21:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)\
**Post date:** [April 17, 2020, 11:21pm UTC](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169/1 "2020-04-17T23:21:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 17, 2020, 11:35pm UTC](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169/3 "2020-04-17T23:35:21Z")

</div>

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](https://github.com/SlicerIGT/SlicerIGT/blob/e7f07bb303e26fde0a010cc528bf36b4759b963c/PlusRemote/PlusRemote.py)

You can use crop volume module via [Crop Volume module logic](http://apidocs.slicer.org/master/classvtkSlicerCropVolumeLogic.html). You can access module logic by calling

```
cropVolumeLogic = slicer.modules.cropvolume.logic()
```

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [April 18, 2020, 1:58am UTC](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169/4 "2020-04-18T01:58:02Z")

</div>

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](https://github.com/QIICR/SlicerDevelopmentToolbox/blob/master/SlicerDevelopmentToolboxUtils/mixins.py#L614-L623).

---

<div class="post-metadata">

**Author:** ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)\
**Post date:** [April 20, 2020, 7:18pm UTC](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169/5 "2020-04-20T19:18:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)\
**Post date:** [April 21, 2020, 8:26pm UTC](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169/6 "2020-04-21T20:26:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [April 21, 2020, 8:42pm UTC](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169/7 "2020-04-21T20:42:48Z")

</div>

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

```python
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?

---

<div class="post-metadata">

**Author:** ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)\
**Post date:** [April 21, 2020, 10:20pm UTC](https://discourse.slicer.org/t/volume-cropping-from-scripted-module/11169/8 "2020-04-21T22:20:49Z")

</div>

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.
