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

**URL:** <https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793>\
**Category:** Support\
**Tags:** python\
**Created:** [July 19, 2021, 12:06am UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793 "2021-07-19T00:06:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 19, 2021, 12:06am UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/1 "2021-07-19T00:06:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 19, 2021, 12:08am UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/2 "2021-07-19T00:08:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Juicy](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Juicy](https://discourse.slicer.org/u/Juicy)\
**Post date:** [July 19, 2021, 12:50am UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/3 "2021-07-19T00:50:55Z")

</div>

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

```auto
# 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)

```

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 19, 2021, 12:55am UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/4 "2021-07-19T00:55:36Z")

</div>

> [@Juicy](#):
>
> ```auto
> # 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)
> 
> ```

Great! Thank you very much!

---

<div class="post-metadata">

**Author:** ![Panda](https://avatars.discourse-cdn.com/v4/letter/p/71c47a/32.png) [@Panda](https://discourse.slicer.org/u/Panda)\
**Post date:** [July 20, 2021, 6:24am UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/5 "2021-07-20T06:24:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Juicy](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Juicy](https://discourse.slicer.org/u/Juicy)\
**Post date:** [July 20, 2021, 6:31am UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/6 "2021-07-20T06:31:57Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 25, 2021, 12:14pm UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/7 "2021-07-25T12:14:28Z")

</div>

> [@jumbojing](#):
>
> > [@Juicy](#):
> >
> > ````auto
> > > # 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)
> > > ```
> > 
> > ````

```auto
roiNode.SetName("ROI")
roiNode.SetXYZ(PP)
roiNode.SetRadiusXYZ(50, 50, 50)

```

我是这样定义ROI大小的…

---

<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:** [February 24, 2022, 8:21pm UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/8 "2022-02-24T20:21:25Z")

</div>

2 posts were split to a new topic: [How to fit ROI to volume in Pythond](https://discourse.slicer.org/t/how-to-fit-roi-to-volume-in-pythond/22168)

---

<div class="post-metadata">

**Author:** ![sabasa](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@sabasa](https://discourse.slicer.org/u/sabasa)\
**Post date:** [February 24, 2022, 6:28pm UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/10 "2022-02-24T18:28:03Z")

</div>

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)](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/4/e4a8341146271f60cc0fa6c32aef4aaaad120cff.png)

---

<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:** [February 24, 2022, 11:36pm UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/11 "2022-02-24T23:36:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sabasa](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@sabasa](https://discourse.slicer.org/u/sabasa)\
**Post date:** [February 25, 2022, 5:01pm UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/12 "2022-02-25T17:01:32Z")

</div>

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

 ![expl2](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/8/f8c1300ab51043f608f4efcd87c1a30901074a42.jpeg)

But when I run the code, I got not-oriented volume for cropping(second attached image).  
 ![expl](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/f/ff3104dc78548de91a9997db8a9eec3d0d3927b5.png)

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?

---

<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:** [February 25, 2022, 5:39pm UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/13 "2022-02-25T17:39:48Z")

</div>

If you want to rotate the ROI to match the volume axis directions then you can add this line to the [code snippet](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#fit-markups-roi-to-volume):

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

```

---

<div class="post-metadata">

**Author:** ![sabasa](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@sabasa](https://discourse.slicer.org/u/sabasa)\
**Post date:** [February 25, 2022, 6:05pm UTC](https://discourse.slicer.org/t/how-to-get-a-roi-to-fit-volume-in-the-volume-rendering-module-with-python/18793/14 "2022-02-25T18:05:02Z")

</div>

It worked. Thank you so much for your help.
