I’m trying to flip a 3d image using AffineTransform (I have to support multiple possible modifications to a 3d image, like rotation and flipping, so I’m trying to represent them as matrices) and it seems that when I apply a flipping affine transform [1,0,0,0,1,0,0,0,-1] - to flip around Z axis, the image direction is not modified. If I use sitk.Flip([False, False, True]) instead, the resulting image direction is correct (z axis is -1). Do I need to update the image direction manually after applying affine transform?
import SimpleITK as sitk
image = sitk.Image(3, 3, 3, sitk.sitkInt16)
image.SetDirection([1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0])
image.SetPixel(0,0,0, 5)
image.SetPixel(0,1,0, 5)
image.SetPixel(0,2,0, 4)
image.SetPixel(1,0,0, 5)
image.SetPixel(1,1,0, 5)
image.SetPixel(1,2,0, 4)
image.SetPixel(2,0,0, 5)
image.SetPixel(2,1,0, 5)
image.SetPixel(2,2,0, 4)
image.SetPixel(0,0,1, 6)
image.SetPixel(0,1,1, 6)
image.SetPixel(0,2,1, 6)
image.SetPixel(1,0,1, 6)
image.SetPixel(1,1,1, 6)
image.SetPixel(1,2,1, 6)
image.SetPixel(2,0,1, 6)
image.SetPixel(2,1,1, 6)
image.SetPixel(2,2,1, 6)
image.SetPixel(0,0,2, 10)
image.SetPixel(0,1,2, 10)
image.SetPixel(0,2,2, 10)
image.SetPixel(1,0,2, 10)
image.SetPixel(1,1,2, 10)
image.SetPixel(1,2,2, 10)
image.SetPixel(2,0,2, 10)
image.SetPixel(2,1,2, 10)
image.SetPixel(2,2,2, 10)
image.SetOrigin([0,0,0])
print("--------- Original -------------")
print("Direction: {}".format(image.GetDirection()))
print(sitk.GetArrayFromImage(image))
print("--------- Flip via AffineTransform -------------")
affineTrans = sitk.AffineTransform(3)
affineTrans.SetMatrix([1,0,0,0,1,0,0,0,-1])
affineTrans.SetCenter([1,1,1])
flipped = sitk.Resample(image, affineTrans)
print("Direction: {}".format(flipped.GetDirection()))
print(sitk.GetArrayFromImage(flipped))
print("--------- Flip via sitk.Flip ---------------")
flipped = sitk.Flip(image, [False,False,True])
print("Direction: {}".format(flipped.GetDirection()))
print(sitk.GetArrayFromImage(flipped))
results:
--------- Original -------------
Direction: (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
[[[ 5 5 5]
[ 5 5 5]
[ 4 4 4]]
[[ 6 6 6]
[ 6 6 6]
[ 6 6 6]]
[[10 10 10]
[10 10 10]
[10 10 10]]]
--------- Flip via AffineTransform -------------
Direction: (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
[[[10 10 10]
[10 10 10]
[10 10 10]]
[[ 6 6 6]
[ 6 6 6]
[ 6 6 6]]
[[ 5 5 5]
[ 5 5 5]
[ 4 4 4]]]
--------- Flip via sitk.Flip ---------------
Direction: (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0)
[[[10 10 10]
[10 10 10]
[10 10 10]]
[[ 6 6 6]
[ 6 6 6]
[ 6 6 6]]
[[ 5 5 5]
[ 5 5 5]
[ 4 4 4]]]