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