# Direction of images not matching

**URL:** <https://discourse.itk.org/t/direction-of-images-not-matching/6232>\
**Category:** Uncategorized\
**Tags:** simpleitk, registration, python\
**Created:** [September 26, 2023, 2:24pm UTC](https://discourse.itk.org/t/direction-of-images-not-matching/6232 "2023-09-26T14:24:11Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [September 26, 2023, 5:36pm UTC](https://discourse.itk.org/t/direction-of-images-not-matching/6232/2 "2023-09-26T17:36:52Z")

</div>

Hello @sdesutter,

What file format are you using to the save the image? Formats like `mhd` write a text header file which includes the image meta-data. The precision of what is written may be different from what you have in memory. I don’t think this is what is happening here, but I may be wrong. An [old and resolved issue](https://discourse.itk.org/t/direction-orientation-matrix-dicom-vs-nifti/3289) which dealt with precision discrepancy problems may be relevant, depends on which ITK/SimpleITK version is being used.

More importantly, the issue here is the concept of `equality` between floating point numbers. The two direction cosine matrices are considered equivalent up to a given threshold, same way you would compare any two floating numbers for equality.

In SimpleITK the two matrices provided in the example are considered equivalent. Below is code which illustrates how to check if two images have the same metadata up to the threshold used by SimpleITK (origin, spacing, direction cosine matrix):

```auto
import SimpleITK as sitk

def equal_metadata(image1, image2):
    # Create smallest possible images with same metadata as originals and cast to
    # same pixel type so that the addition operation is always valid. Any exception
    # is a result of inconsistent metadata.
    image1_meta = sitk.Cast(sitk.Extract(image1, [1]*image1.GetDimension()), sitk.sitkUInt8)
    image2_meta = sitk.Cast(sitk.Extract(image2, [1]*image2.GetDimension()), sitk.sitkUInt8)
    try:
        image1_meta + image2_meta
        return True
    except Exception as e:
        # Uncomment next line to see what is the discrepancy:
        # print("not equal metadata: " + str(e))
        return False

image1 = sitk.Image([10, 20, 30], sitk.sitkFloat32)
image1.SetDirection(
    [
        0.99363196,
        -0.08103074,
        -0.07829134,
        -0.05990832,
        -0.96842784,
        0.24198869,
        0.09542805,
        0.23575737,
        0.96711527,
    ]
)

image2 = sitk.Image([10, 20, 30], sitk.sitkUInt8)
image2_dir = [
    0.99363197,
    -0.08103075,
    -0.07829135,
    -0.05990831,
    -0.96842784,
    0.24198867,
    0.09542804,
    0.23575738,
    0.96711527,
]
image2.SetDirection(image2_dir)

if equal_metadata(image1, image2):
    print("1: equal metadata (up to SimpleITK eps)")
else:
    print("1: not equal metadata (up to SimpleITK eps)")
    
image2_dir[0] = 0.99363
image2.SetDirection(image2_dir)
if equal_metadata(image1, image2):
    print("2: equal metadata (up to SimpleITK eps)")
else:
    print("2: not equal metadata (up to SimpleITK eps)")

```

---

_[View the full topic](https://discourse.itk.org/t/direction-of-images-not-matching/6232)._
