# Finding the largest physical region (bounding box) across an arbitrary number of input files (SimpleITK)

**URL:** https://discourse.itk.org/t/finding-the-largest-physical-region-bounding-box-across-an-arbitrary-number-of-input-files-simpleitk/5234
**Category:** Algorithms
**Created:** [August 1, 2022, 5:59pm UTC](https://discourse.itk.org/t/finding-the-largest-physical-region-bounding-box-across-an-arbitrary-number-of-input-files-simpleitk/5234 "2022-08-01T17:59:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![gdevenyi](https://discourse.itk.org/user_avatar/discourse.itk.org/gdevenyi/32/355_2.png) [@gdevenyi](https://discourse.itk.org/u/gdevenyi)
#### Post date: [August 1, 2022, 5:59pm UTC](https://discourse.itk.org/t/finding-the-largest-physical-region-bounding-box-across-an-arbitrary-number-of-input-files-simpleitk/5234/1 "2022-08-01T17:59:51Z")

</div>

I’m working on an arbitrary image averaging script, where I’d like to assume input images are not guaranteed to overlap (completely, or even partially). As such, I need to construct a “common” space which contains all the input images. Finding the voxel dimensions is easy per-image, but before I go about writing something to locate all the “corners” of the data in space manually, is there a more clever solution in ITK I might be able to use?

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [August 1, 2022, 6:18pm UTC](https://discourse.itk.org/t/finding-the-largest-physical-region-bounding-box-across-an-arbitrary-number-of-input-files-simpleitk/5234/2 "2022-08-01T18:18:10Z")

</div>

I don’t know of anything clever, but I did write the code for computing a world space bounding box of a volume.

Here’s my code to ‘regularize’ a volume. It makes a volume with cubic voxels and an orientation of the identity matrix.

> <https://github.com/dave3d/dicom2stl/blob/main/utils/regularize.py>

---

<div class="post-metadata">

### Author: ![gdevenyi](https://discourse.itk.org/user_avatar/discourse.itk.org/gdevenyi/32/355_2.png) [@gdevenyi](https://discourse.itk.org/u/gdevenyi)
#### Post date: [August 1, 2022, 6:31pm UTC](https://discourse.itk.org/t/finding-the-largest-physical-region-bounding-box-across-an-arbitrary-number-of-input-files-simpleitk/5234/3 "2022-08-01T18:31:28Z")

</div>

Thanks for the code, I can definitely steal some of that.

Since you’re isotropizing images, you might find this useful, [Resampling to Isotropic: Signal Processing Theory](https://discourse.itk.org/t/resampling-to-isotropic-signal-processing-theory/1403)

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [August 1, 2022, 7:38pm UTC](https://discourse.itk.org/t/finding-the-largest-physical-region-bounding-box-across-an-arbitrary-number-of-input-files-simpleitk/5234/4 "2022-08-01T19:38:46Z")

</div>

Interesting thread. Yeah, I don’t do any sort of filtering, so it can result it artifacts.

---

<div class="post-metadata">

### Author: ![gdevenyi](https://discourse.itk.org/user_avatar/discourse.itk.org/gdevenyi/32/355_2.png) [@gdevenyi](https://discourse.itk.org/u/gdevenyi)
#### Post date: [August 2, 2022, 5:49pm UTC](https://discourse.itk.org/t/finding-the-largest-physical-region-bounding-box-across-an-arbitrary-number-of-input-files-simpleitk/5234/5 "2022-08-02T17:49:56Z")

</div>

Thanks very much again @dchen, I stole your code mostly wholesale to sort out the bounding box, now I can combine arbitrary-space images into a common “mega” image covering the full bounding box, as I wanted.

> <https://github.com/CoBrALab/optimized_antsMultivariateTemplateConstruction/pull/49/files>
>
> Alternative script to average input files, but requires SimpleITK and python.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [August 2, 2022, 10:52pm UTC](https://discourse.itk.org/t/finding-the-largest-physical-region-bounding-box-across-an-arbitrary-number-of-input-files-simpleitk/5234/6 "2022-08-02T22:52:16Z")

</div>

You’re welcome. I’m glad you found my code useful.
