From 84bd49508148be77449c1288d52972cb0e9cc6f3 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Tue, 16 Jul 2019 13:17:01 +0100
Subject: [PATCH] RF: Adjust matrix correctly (again) for >3D images. Adjust
 pixdims of higher dimensions

---
 fsl/scripts/resample_image.py | 13 +++++++++++++
 fsl/utils/image/resample.py   |  7 ++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/fsl/scripts/resample_image.py b/fsl/scripts/resample_image.py
index 81b329ce4..b2ef4b9c0 100644
--- a/fsl/scripts/resample_image.py
+++ b/fsl/scripts/resample_image.py
@@ -196,6 +196,19 @@ def main(argv=None):
         xform = None
 
     resampled = fslimage.Image(resampled, xform=xform, header=hdr)
+
+    # Adjust the pixdims of the
+    # higher dimensions if they
+    # have been resampled
+    if len(resampled.shape) > 3:
+
+        oldPixdim = args.input.pixdim[3:]
+        oldShape  = args.input.shape[ 3:]
+        newShape  = resampled .shape[ 3:]
+
+        for i, (p, o, n) in enumerate(zip(oldPixdim, oldShape, newShape), 4):
+            resampled.header['pixdim'][i] = p * o / n
+
     resampled.save(args.output)
 
     return 0
diff --git a/fsl/utils/image/resample.py b/fsl/utils/image/resample.py
index 0047444b4..91e79e6c0 100644
--- a/fsl/utils/image/resample.py
+++ b/fsl/utils/image/resample.py
@@ -210,7 +210,12 @@ def resample(image,
     # might not return a 4x4 matrix, so we
     # make sure it is valid.
     if matrix.shape != (4, 4):
-        matrix = np.vstack((matrix[:3, :4], [0, 0, 0, 1]))
+        rotmat         = matrix[:3, :3]
+        offsets        = matrix[:3, -1]
+        matrix         = np.eye(4)
+        matrix[:3, :3] = rotmat
+        matrix[:3, -1] = offsets
+
     matrix = transform.concat(image.voxToWorldMat, matrix)
 
     return data, matrix
-- 
GitLab