Hi,
The flat array “direction” property of sitk images are specified as a row-order concatenation of the 3x3 direction matrix.
For Elastix/SimpleElastix, the Direction property specified in Transformparamterer maps is a column order flattening.
This is a little impractical and leaves room for error. I guess it is really a clash of conventions between elastix and ITK.
I just wanted to bring this up - not sure which table (elastix or ITK) it belongs on!
Self contained example below.
Soren
import SimpleITK as sitk
import numpy as np
test_image = sitk.GetImageFromArray(np.zeros((10,10,10),dtype=np.float32))
dir1 = [0,0,-1] #voxel X maps to neg Z world
dir2 = [0,1,0] #voxel Y maps to pos Y world
dir3 = [1,0,0] #voxel Z maps to pos X world
directions_serial = dir1+dir2+dir3
test_image.SetDirection(directions_serial)
sitk.WriteImage(test_image,"test.nii")
test_image_read = sitk.ReadImage("test.nii")
np.testing.assert_equal(np.array(test_image_read.GetDirection()),np.array(test_image.GetDirection()))
np.testing.assert_equal(np.array(test_image_read.GetDirection()),np.array(directions_serial))
#ok as expected. Now lets manually define a resampling space based on test_image
#first - obtain a transformparametermap - this is a litte detaour
elastix = sitk.ElastixImageFilter()
elastix.SetParameterMap(elastix.GetDefaultParameterMap("rigid",1))
elastix.SetMovingImage(test_image)
elastix.SetFixedImage(test_image)
elastix.Execute()
transformparametermap_template = elastix.GetTransformParameterMap()
# ok, we have a parametermap. The directions of this map are identical to the fixed image by design, but the flattening of the direction matrix was by column order
directions_flat_from_transform_file = np.array([float(v) for v in transformparametermap_template[0]["Direction"]])
np.testing.assert_equal(directions_flat_from_transform_file,np.array(directions_serial))
# FAILS - ordering is not identical
#so when interfacting with transformparameter maps, the flattened arrays use column ordering as opposed to row-ordering for the simple itk images
re_ordered_directions_from_transform_file = directions_flat_from_transform_file.reshape((3,3),order="F").flatten(order="C")
np.testing.assert_equal(re_ordered_directions_from_transform_file,np.array(directions_serial))