# Checking simpleitk.Image Equality

**URL:** <https://discourse.itk.org/t/checking-simpleitk-image-equality/6323>\
**Category:** Beginner Questions\
**Tags:** nifti, simpleitk, python\
**Created:** [November 29, 2023, 9:42am UTC](https://discourse.itk.org/t/checking-simpleitk-image-equality/6323 "2023-11-29T09:42:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Franciska](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/b38774/32.png) [@Franciska](https://discourse.itk.org/u/Franciska)\
**Post date:** [November 29, 2023, 9:42am UTC](https://discourse.itk.org/t/checking-simpleitk-image-equality/6323/1 "2023-11-29T09:42:33Z")

</div>

Hi! I have two methods to the same task. I’d like to check, whether the result of the two methods gives equal outcomes. The files are nifti Images.

I can check if their numpy part is the same easily:

> output1=sitk.ReadImage(‘./output1.nii.gz’)  
> output1\_np=sitk.GetArrayFromImage(output1)
> 
> output2=sitk.ReadImage(‘./output2.nii.gz’)  
> output2\_np=sitk.GetArrayFromImage(output2)  
> print( np.array\_equal(output1\_np,output2\_np))

Is there any similar operation in simpleitk, for checking the whole simpleITK image? I found sitk.EqualImageFilter(), and tried

> areequal=equalfilter.Execute(output1,output2)

but unfortunately I can’t really interpret the output.

---

<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:** [November 29, 2023, 2:19pm UTC](https://discourse.itk.org/t/checking-simpleitk-image-equality/6323/2 "2023-11-29T14:19:08Z")

</div>

Hello @Franciska,

Generally speaking, the test you are asking for will **always return false**. Even when comparing two implementations of the same algorithm the results are more than likely to be different. Most of the time they will be very similar, but not exactly equal. This is a rule of thumb with respect to general computations, comparison is up to an epsilon.

In some cases it does make sense to compare for exact equality. For example, regression testing of a segmentation algorithm is expected to result in the same label image every time.

Now for some code:

```auto
# Exactly equal (both pixel values and the spatial location)
equal = sitk.Hash(image1) == sitk.Hash(image2)
try:
  image1[0:1,0:1,0:1] + image2[0:1,0:1,0:1]
except Exception:
  equal = False

# Approximately equal
max_diff_eps = 1e-6
_, max_diff = sitk.MinimumMaximum(sitk.Abs(image1 - image2))
equal = max_diff < max_diff_eps

# Approximately equal in region of interest (outside of the ROI the differences/errors don't matter)
binary_mask_roi = #mask with 1 denoting ROI 0 denoting regions we aren't interested in
max_diff_eps = 1e-6
_, max_diff = sitk.MinimumMaximum(binary_mask*sitk.Abs(image1 - image2))
equal = max_diff < max_diff_eps

```

Note that in SimpleITK we are comparing both the pixel values and the physical region occupied by the image (numpy comparison is not aware of the physical nature of an image it is treated as an array of values, not a physical object).

---

<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:** [November 29, 2023, 2:52pm UTC](https://discourse.itk.org/t/checking-simpleitk-image-equality/6323/3 "2023-11-29T14:52:43Z")

</div>

It seems that the EqualImageFilter returns an Image. What does that image mean?

---

<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:** [November 29, 2023, 3:03pm UTC](https://discourse.itk.org/t/checking-simpleitk-image-equality/6323/4 "2023-11-29T15:03:39Z")

</div>

Hello @dchen,

The filter works at the pixel level, returns an image with 1 for pixels that have equal values and 0 for those that don’t.

---

<div class="post-metadata">

**Author:** ![Franciska](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/b38774/32.png) [@Franciska](https://discourse.itk.org/u/Franciska)\
**Post date:** [November 29, 2023, 3:26pm UTC](https://discourse.itk.org/t/checking-simpleitk-image-equality/6323/5 "2023-11-29T15:26:48Z")

</div>

Yes, the physical region is the reason I want to compare them more precisely, than only comparing the numpy parts.

So does this theoretically mean that if I check if the numpyarray generated from the result of the  
`EqualImageFilter`, and its all ones, the simpleITK images are the same, with the same physical region as well?

---

<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:** [November 29, 2023, 3:37pm UTC](https://discourse.itk.org/t/checking-simpleitk-image-equality/6323/6 "2023-11-29T15:37:19Z")

</div>

Hello @Franciska,

Yes. If they don’t occupy the same physical region the `EqualImageFilter` will raise an exception. If that operation goes smoothly, no exception, and the resulting image is all 1’s they are exactly equal (ITK/SimpleITK semantics - equal in physical dimensions and intensity values).
