Checking simpleitk.Image Equality

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.

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:

# 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).

1 Like

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

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.

1 Like

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?

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).

2 Likes