# Segmentation getBounds

**URL:** <https://discourse.slicer.org/t/segmentation-getbounds/16452>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [March 9, 2021, 3:25pm UTC](https://discourse.slicer.org/t/segmentation-getbounds/16452 "2021-03-09T15:25:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ludovic\_Ferrer](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ludovic_ferrer/32/5018_2.png) [@Ludovic\_Ferrer](https://discourse.slicer.org/u/Ludovic_Ferrer)\
**Post date:** [March 9, 2021, 3:25pm UTC](https://discourse.slicer.org/t/segmentation-getbounds/16452/1 "2021-03-09T15:25:07Z")

</div>

Hi everyone,  
I am interested in cropping images around each segmentation I have at hand. Naively, I though that the method GetBounds would provide me a bounding box around each segmentation. Here is the snippet I used to get the information.

> def getbounds\_segmentations(node\_string=‘_RTSTRUCT_’):  
> segmentationNode = slicer.util.getNode(node\_string)  
> segmentation = segmentationNode.GetSegmentation()  
> dico\_seg = {}  
> for i in range(segmentation.GetNumberOfSegments()):  
> bounds = [0]\*6  
> seg\_name = segmentation.GetNthSegment(i).GetName()  
> current\_seg = segmentation.GetNthSegment(i)  
> current\_seg.GetBounds(bounds)  
> dico\_seg[seg\_name] = bounds  
> return dico\_seg

here is the output dictionnary:

> {‘FOIE (% Max=40) - R’: [-178.71093750000003, 88.8671875, 141.59375, 193.3515625, -677.5, -355.5], ‘FOIE (Adaptatif) - R’: [-178.71093750000003, 83.3479995727539, 142.82000732421875, 189.4453125, -675.5, -355.5], ‘FOIE (Black) - R’: [-178.71093750000003, 88.23500061035156, 142.5703125, 192.50799560546875, -675.5, -353.5], ‘FOIE (Fitting) - R’: [-178.71093750000003, 78.45999908447266, 146.4765625, 187.62100219726562, -673.5, -357.5], ‘FOIE (Nestle) - R’: [-178.71093750000003, 82.53299713134766, 145.4669952392578, 188.46875, -675.5, -355.5], ‘Structure 001-os (% Max=40) - R’: [-178.9429931640625, 88.8671875, 141.59375, 193.3515625, -677.5, -355.5], ‘Structure 001-os (Adaptatif) - R’: [-178.73899841308594, 82.03125, 143.546875, 189.4453125, -675.5, -355.5], ‘Structure 001-os (Black) - R’: [-178.9429931640625, 87.890625, 142.5703125, 192.375, -675.5, -353.5], ‘Structure 001-os (Fitting) - R’: [-178.73899841308594, 77.1484375, 146.4765625, 187.4921875, -673.5, -356.40350341796875], ‘Structure 001-os (Nestle) - R’: [-178.9429931640625, 82.03125, 145.5, 188.46875, -675.5, -355.5]}

As you can see, despite the fact that the structures are way apart, the bounding box values are quite the same. ![slicer1](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/1/71dd52c205bd6b84b56ec81e2a4ab4a21938410e.png)

What am I missing here ?  
Best regards  
Ludovic

---

<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:** [March 9, 2021, 6:04pm UTC](https://discourse.slicer.org/t/segmentation-getbounds/16452/2 "2021-03-09T18:04:22Z")

</div>

The feature that you are considering implementing is already available in “Split volume” effect (provided by SegmentEditorExtraEffects extension).

> [@Ludovic\_Ferrer](#):
>
> I though that the method GetBounds would provide me a bounding box around each segmentation.

GetBounds provides union of bounds of the all representation of the segment. This is guaranteed to contain the segment but not guaranteed to be minimum size. Computing the minimum size can be an expensive operation that you can perform whenever it is necessary. For example, for binary labelmap representation, you can use vtkSegmentationCore.vtkOrientedImageDataResample.CalculateEffectiveExtent as it is done in Crop volume effect (that Split volume effect uses internally).

---

<div class="post-metadata">

**Author:** ![Ludovic\_Ferrer](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ludovic_ferrer/32/5018_2.png) [@Ludovic\_Ferrer](https://discourse.slicer.org/u/Ludovic_Ferrer)\
**Post date:** [March 10, 2021, 10:03am UTC](https://discourse.slicer.org/t/segmentation-getbounds/16452/3 "2021-03-10T10:03:11Z")

</div>

Thank you Andras.  
This is a nice feature. Is it possible to use it by scripting ?  
One more thing, my main objective here would be to create a majority voting (or staple) labelmap for each segmentation (I have 5 segmentations for each localisation of interest, eg: 5 for liver, 5 for tumor1, etc …). One way of doing whould be to create one labelmap for each segmentation considering the whole volume (which is not really efficient). And, as the number of segmentation is really large (up to 200), I came to the idea of cropping the volume before the creation of the labelmap. Is it a correct way of handling this ? if yes, what would be the best approach ?  
Best regards  
Ludovic

---

<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:** [March 10, 2021, 2:25pm UTC](https://discourse.slicer.org/t/segmentation-getbounds/16452/4 "2021-03-10T14:25:08Z")

</div>

> [@Ludovic\_Ferrer](#):
>
> Is it possible to use it by scripting ?

All Slicer features are available using Python scripting. See examples in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script).

> [@Ludovic\_Ferrer](#):
>
> Is it a correct way of handling this ? if yes, what would be the best approach ?

I’m not sure I understand the question. Please use segment and segmentation terms as defined [here](https://slicer.readthedocs.io/en/latest/user_guide/image_segmentation.html#basic-concepts) to make things more clear. If possible, also add a few pictures to explain what you would like to do.
