# Simple ITK Crop Image

**URL:** <https://discourse.itk.org/t/simple-itk-crop-image/4246>\
**Category:** Algorithms\
**Tags:** simpleitk, python\
**Created:** [July 6, 2021, 3:07am UTC](https://discourse.itk.org/t/simple-itk-crop-image/4246 "2021-07-06T03:07:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [July 6, 2021, 3:07am UTC](https://discourse.itk.org/t/simple-itk-crop-image/4246/1 "2021-07-06T03:07:36Z")

</div>

Hey guys:

I have a 3D grey-level image (256, 256, 192);  
I have a 3D label-image binary (1 and 0) to define a segmentation (256, 256, 192);

I use LabelShapeStatisticsImageFilter to get a bounding box or region for my label image;  
The region/boundingbox output is something like this: [115, 58, 122,32, 18, 8]

I then use that region info to crop this bounding box region from grey-level image;  
The crop filter takes an image, lowerBoundaryCropSize and upperBoundaryCropSize.

Question: How to find out those boundary indexes from region info?

I tried many combinations, like three first numbers for lower boundary, and the rest for upper boundary. Also subtracting the last three numbers from initial ones and set is as either lower or upper boundary. No success.

Error:The input image’s size [256, 256, 192] is less than the total of the crop size!

thaks in advance;

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [July 6, 2021, 12:46pm UTC](https://discourse.itk.org/t/simple-itk-crop-image/4246/2 "2021-07-06T12:46:48Z")

</div>

> [@machadoL](#):
>
> The region/boundingbox output is something like this: [115, 58, 122,32, 18, 8]

The return follows the SimpleITK [common convention for converting a regio](https://simpleitk.readthedocs.io/en/master/conventions.html#image-regions-as-index-and-size)n into a array: [idx\_x, idx\_y, idx\_z, size\_x, size\_y, size\_z].

The [RegionOfInterest](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1RegionOfInterestImageFilter.html) filter has the index and size parameters which conveniently works with the output values produced.

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [July 6, 2021, 2:38pm UTC](https://discourse.itk.org/t/simple-itk-crop-image/4246/3 "2021-07-06T14:38:37Z")

</div>

Excellent. Used and Worked.  
A single observation; `RegionOfInterest` takes as input `(Image, Size, Index)` rather than `(Image, Region)` .

---

<div class="post-metadata">

**Author:** ![aliabidi](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/f17d59/32.png) [@aliabidi](https://discourse.itk.org/u/aliabidi)\
**Post date:** [April 28, 2022, 4:33pm UTC](https://discourse.itk.org/t/simple-itk-crop-image/4246/4 "2022-04-28T16:33:47Z")

</div>

> [@machadoL](#):
>
> Excellent. Used and Worked.  
> A single observation; `RegionOfInterest` takes as input `(Image, Size, Index)` rather than `(Image, Region)` .

Hi, how could I use the bounding box of one image to crop another? Say I have two images: `imgA` and `imgB`. I first get a bounding box from `imgA` via the following:

```auto
label_shape_filter = sitk.LabelShapeStatisticsImageFilter()
label_shape_filter.Execute(imgA)
bounding_box = label_shape_filter.GetBoundingBox(1) 

```

which returns a bounding box of `(154, 314, 210, 284, 8, 44)`. I then want to crop `imgB` using this bounding box. How could I achieve this using `RegionOfInterest`?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [April 28, 2022, 5:31pm UTC](https://discourse.itk.org/t/simple-itk-crop-image/4246/5 "2022-04-28T17:31:03Z")

</div>

Hello @machadoL,

Work with the images as physical objects (assuming the axes of both images are aligned):

```auto
p1 = imgA.TransformIndexToPhysicalPoint(bounding_box[0 : int(len(bounding_box) / 2)])
p2 = imgA.TransformIndexToPhysicalPoint([x+sz for x,sz in zip(bounding_box[0 : int(len(bounding_box) / 2)], bounding_box[int(len(bounding_box) / 2) :])])

# crop using the indexes computed for imgB
imgB_start_index = imgB.TransformPhysicalPointToIndex(p1)
imgB_end_index = imgB.TransformPhysicalPointToIndex(p2)

```
