# Getting ROI bounds from vtkMRMLMarkupsROINode

**URL:** <https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [September 25, 2021, 3:47pm UTC](https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852 "2021-09-25T15:47:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![masadcv](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/masadcv/32/10632_2.png) [@masadcv](https://discourse.slicer.org/u/masadcv)\
**Post date:** [September 25, 2021, 3:47pm UTC](https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852/1 "2021-09-25T15:47:52Z")

</div>

I am looking into switching from `vtkMRMLAnnotationROINode` to `vtkMRMLMarkupsROINode` node for selecting ROI bounding box within a volume.

Previously I have been using `GetBounds()` function to quickly get bounding box of the ROI from `vtkMRMLAnnotationROINode`. The code looked like as follows:

```auto
roiNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLAnnotationROINode")
roi_points_ras = [0.0] * 6
roiNode.GetBounds(roi_points_ras)
selected_roi = convertRasToIJK(roi_points_ras)

```

Now with the `vtkMRMLMarkupsROINode`, I am trying to get the same functionality. But it seems like it does not have a `GetBounds()` function with similar output. How can I achieve the same with this node?

Here is my code until now:

```auto
roiNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLMarkupsROINode")
center = [0] * 3
roiNode.GetCenter(center)
roi_points_ras = [(x-s/2, x+s/2) for x, s in zip(center, roiNode.GetSize())]
roi_points_ras=[item for sublist in roi_points_ras for item in sublist]
selected_roi = convertRasToIJK(roi_points_ras)

```

Maybe I am missing something here and perhaps there is a starightforward way of getting roi bounding box in IJK coordinate space with `vtkMRMLMarkupsROINode`. If this is the case, kindly can someone guide me how to achieve the above.

Many thanks!  
Muhammad

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [September 25, 2021, 6:37pm UTC](https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852/2 "2021-09-25T18:37:13Z")

</div>

> [@masadcv](#):
>
> Now with the `vtkMRMLMarkupsROINode` , I am trying to get the same functionality. But it seems like it does not have a `GetBounds()` function with similar output.

I believe you must be mistaken as the new ROI node type does have a `GetBounds()` method with results just like the previous.

```python
old_roi = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLAnnotationROINode")
old_roi.SetRadiusXYZ(5,10,15)
old_roi_bounds = [0]*6
old_roi.GetBounds(old_roi_bounds)
print(old_roi_bounds)
# [-5.0, 5.0, -10.0, 10.0, -15.0, 15.0]

new_roi = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsROINode")
new_roi.SetRadiusXYZ(5,10,15)
new_roi_bounds = [0]*6
new_roi.GetBounds(new_roi_bounds)
print(new_roi_bounds)
# [-5.0, 5.0, -10.0, 10.0, -15.0, 15.0]

```

---

<div class="post-metadata">

**Author:** ![masadcv](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/masadcv/32/10632_2.png) [@masadcv](https://discourse.slicer.org/u/masadcv)\
**Post date:** [September 27, 2021, 3:55pm UTC](https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852/3 "2021-09-27T15:55:26Z")

</div>

> [@jamesobutler](#):
>
> ```auto
> new_roi = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsROINode")
> new_roi.SetRadiusXYZ(5,10,15)
> new_roi_bounds = [0]*6
> new_roi.GetBounds(new_roi_bounds)
> print(new_roi_bounds)
> # [-5.0, 5.0, -10.0, 10.0, -15.0, 15.0]
> 
> ```

This works only in latest preview build (4.13). In stable release (4.11) it gives the following output:

```auto
new_roi = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsROINode")
new_roi.SetRadiusXYZ(5,10,15)
new_roi_bounds = [0]*6
new_roi.GetBounds(new_roi_bounds)
print(new_roi_bounds)
# [0, 0, 0, 0, 0, 0]

```

Any ideas how I can support both versions?

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [September 27, 2021, 4:22pm UTC](https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852/4 "2021-09-27T16:22:56Z")

</div>

An option when there aren’t many differences is to do something like the following:

```python
if slicer.app.majorVersion >= 5 or (slicer.app.majorVersion == 4 and slicer.app.minorVersion >= 13):
  # do code for Slicer 4.13 preview and later
  new_roi.GetBounds(new_roi_bounds) # or new_roi.GetBoundsROI(new_roi_bounds)
  print(new_roi_bounds)
  # [-5.0, 5.0, -10.0, 10.0, -15.0, 15.0]
else:
  # do code for Slicer 4.11.20210226 and older
  new_roi.GetBoundsROI(new_roi_bounds)
  print(new_roi_bounds)
  # [-5.0, 5.0, -10.0, 10.0, -15.0, 15.0]

```

@Sunderlandkyl May be able to tell what is the preferred method for getting bounds for this object. Either `GetBounds` or `GetBoundsROI`.

Another option is this could be accomplished by setting up different branches with support for each version as indicated below.

[https://slicer.readthedocs.io/en/latest/developer\_guide/extensions.html#extensions-index](https://slicer.readthedocs.io/en/latest/developer_guide/extensions.html#extensions-index)

> Extension developers have to make sure that the extension description in each branch of the Extensions index is compatible with the corresponding Slicer version. Extension developers often create the same branches ( `master` , `4.11` , `4.13` , …) in their repository and they specify this branch name in the extensions descriptor file.

---

<div class="post-metadata">

**Author:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [September 27, 2021, 5:51pm UTC](https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852/5 "2021-09-27T17:51:20Z")

</div>

Because the new Markups ROI can be rotated freely, using a normal bounds with 6 values doesn’t always work. GetBounds()/GetRASBounds() returns the axis-aligned bounds, so it will be correct if the ROI is axis-aligned. In the image below, the magenta cube will be the bounds that are returned by GetBounds() if the ROI is rotated.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/a/1a9d16bcc446b40ee95cc05f7005e4c05d98266c.png)

The way to get the OBB in any orientation would be to use the GetPlanes()/GetPlanesWorld() functions, which return the planes for the 6 faces of the ROI.

If you are checking whether or not a point is in the ROI, also consider the IsPointInROI()/IsPointInROIWorld() functions.

GetBoundsROI() returns the bounds in the axis-aligned ROI coordinate system, with the center of the ROI at (0,0,0). It is mostly used internally, and should probably be made into a protected function.

---

<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:** [September 27, 2021, 7:52pm UTC](https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852/6 "2021-09-27T19:52:52Z")

</div>

> [@Sunderlandkyl](#):
>
> GetBoundsROI() returns the bounds in the axis-aligned ROI coordinate system, with the center of the ROI at (0,0,0). It is mostly used internally, and should probably be made into a protected function.

Yes, @Sunderlandkyl please make it protected to make the API simpler (we can later re-add, if we find that it is needed). Also add your explanation to the ROI node API documentation (developers may not think about the impact of that the ROI can be rotated).

---

<div class="post-metadata">

**Author:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [September 27, 2021, 9:20pm UTC](https://discourse.slicer.org/t/getting-roi-bounds-from-vtkmrmlmarkupsroinode/19852/7 "2021-09-27T21:20:55Z")

</div>

I’ve made a PR to remove the function here: [ENH: Remove vtkMRMLMarkupsROINode::GetBoundsROI() and add documentation by Sunderlandkyl · Pull Request #5905 · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/pull/5905).
