# Quick Test for "are images in the same space" aka "overalpping" (SimpleITK)?

**URL:** https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249
**Category:** Algorithms
**Tags:** python, simpleitk
**Created:** [August 8, 2022, 6:56pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249 "2022-08-08T18:56:59Z")
**Posts on this page:** 8
**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 8, 2022, 6:56pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249/1 "2022-08-08T18:56:59Z")

</div>

If I have a collection of images and I’d like to quickly check that they’re “overlapping”, that is, have the same origin, pixel dimensions and direction cosines, is there a filter/test which does this other than comparing the GetSpacing(), GetOrigin() and GetDirection() vectors directly?

---

<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: [August 8, 2022, 7:21pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249/2 "2022-08-08T19:21:26Z")

</div>

Hello @gdevenyi,

Here’s what I do (it isn’t the nicest code in the world but maintains consistency with the internals of ITK/SimpleITK):

```auto
if not np.array_equal(np.array(image1.GetSize()), np.array(image2.GetSize())):
  raise ValueError("Sizes don't match")

image1_proxy = image1[0:1,0:1,0:1]
image2_proxy = image2[0:1,0:1,0:1]
try: # comparison of origin, spacing and direction using ITK/SimpleITK epsilon
  image1_proxy + image2_proxy
except Exception:
  raise ValueError("Origin, spacing or direction do not match.")

```

---

<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 8, 2022, 7:26pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249/3 "2022-08-08T19:26:19Z")

</div>

Ha! That’s a very clever hack relying on the exception checks inside SimpleITK to achieve it.

I don’t think this will work out for me, as I want to avoid loading the image data as there could be a large number of high resolution images, and I need to test each one before I go downstream with my processing choices. Thanks for the suggestion!

---

<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: [August 8, 2022, 7:38pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249/4 "2022-08-08T19:38:05Z")

</div>

Still doable if the file format supports streaming:

```auto
file_reader = sitk.ImageFileReader()
file_reader.SetFileName(file_name1)
file_reader.ReadImageInformation()
image1_size = np.array(file_reader.GetSize())

# read the first voxel to get the image's origin, spacing, direction cosine
file_reader.SetExtractIndex([0,0,0])
file_reader.SetExtractSize([1,1,1])
image1_proxy = file_reader.Execute()

```

If it doesn’t support streaming, then you’ll just have to read the image information and construct the “proxy” images from that information.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [August 8, 2022, 8:04pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249/5 "2022-08-08T20:04:44Z")

</div>

> [@zivy](#):
>
> read the image information and construct the “proxy” images

… and that might be more trouble than directly comparing size, spacing, origin and direction. Invoking code in [ImageToImageFilter::VerifyInputInformation()](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.3rc04/Modules/Core/Common/include/itkImageToImageFilter.hxx#L146-L223) is probably not feasible, but stealing code from it totally is 😃

---

<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 8, 2022, 8:25pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249/6 "2022-08-08T20:25:34Z")

</div>

Since I’m already using numpy, I went with this test in my reader loop

```python
            if not (np.allclose(img.GetSpacing(), reader.GetSpacing(), atol=1e-6) and 
                    np.allclose(img.GetOrigin(), reader.GetOrigin(), atol=1e-6) and
                    np.allclose(img.GetDirection(), reader.GetDirection())):

```

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [August 8, 2022, 8:53pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249/7 "2022-08-08T20:53:01Z")

</div>

There are good reasons why `ImageToImageFilter::VerifyInputInformation()` is so complex. The proposed simple check (based on a fixed absolute tolerance value) will cause trouble by either incorrectly rejecting some images that have actually matching geometries or not catching some actual mismatches.

The current `ImageToImageFilter::VerifyInputInformation()` implementation should be exposed in Python, otherwise it would be very hard to ensure consistent behavior.

---

<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 8, 2022, 9:10pm UTC](https://discourse.itk.org/t/quick-test-for-are-images-in-the-same-space-aka-overalpping-simpleitk/5249/8 "2022-08-08T21:10:49Z")

</div>

Issue opened, [Expose ImageToImageFilter::VerifyInputInformation() as a usable True/False function · Issue #3536 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/issues/3536)
